Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Automating Workflows

(0) ShareShare
ReportReport
Posted on by 1,695

365 On premises

I have crated a workflow which queries an external SQL database and inserts records into a custom crm entity 

I am wondering if I can schedule this workflow to execute at 4 specific times in the day rather than manually running it

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,961 Moderator on at
    RE: Automating Workflows

    Hi,

    Can you please try below code to connect to dynamics crm.

    public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
            {
                try
                {
                    ClientCredentials credentials = new ClientCredentials();
                    credentials.UserName.UserName = UserName;
                    credentials.UserName.Password = Password;
                    Uri serviceUri = new Uri(SoapOrgServiceUri);
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                    proxy.EnableProxyTypes();
                    _orgservice = (IOrganizationService)proxy;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while connecting to CRM "   ex.Message);
                    Console.ReadKey();
                }
            }

    Also, see below github sample code to execute workflow programatically.

    https://github.com/microsoft/PowerApps-Samples/blob/master/cds/orgsvc/C#/Workflow/Workflow/ExecuteWorkflow.cs

    Please mark my answer verified if i were helpful

  • Suggested answer
    Gus Gonzalez Profile Picture
    Gus Gonzalez 27,113 on at
    RE: Automating Workflows

    Hey Pete,

    I think that you could leverage the solution I describe in this post: community.dynamics.com/.../scheduling-recurring-workflows-in-microsoft-dynamics-crm-2011-online-and-on-premise

    I know it's old but it still works, trust me.

  • Pete_N Profile Picture
    Pete_N 1,695 on at
    RE: Automating Workflows

    Hi Roma,

    Thank you for your help

    Can you explain how to hard code the credentials for domain/user & password

  • Suggested answer
    Roma Gupta Profile Picture
    Roma Gupta 725 on at
    RE: Automating Workflows

    Hi Pete

    Let's segregate issues with the code & try to run. Could you please do following and share the results:

    1. Hard code credentials for testing purposes & run the console

    2. On Workflow  Request do this & remove lines 13-15. Execute & test.

    ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()

         {

             WorkflowId = Guid.Parse("fa69cfbc-f14e-4dfc-a098-d1cd339cac62"),

             EntityId = Guid.Parse("566AC746-91E3-EA11-9171-0050560105A7")

         };

    Do Share the results

    Regards

    Roma

  • Pete_N Profile Picture
    Pete_N 1,695 on at
    RE: Automating Workflows

    HI,

    Thank you for your reply. I have tried to create a console app using the tutorials, but cannot seem to get it to work.

    If I'm right in my understanding then the console App basically acts as if a record was selected and the workflow executed. Based on this understanding I came up with the following code from various posts. The workflow executes a SQL query on a legacy database and creates records in CRM from the results. If i manually run the workflow it works perfectly, however although I'm getting no errors, when running the code below, nothing happens and I don't know where to go from here. Would it be the credentials , should I be using a domain name and password and if so how ?

        Sub Main()
    
            Try
                Dim Credentials As ClientCredentials = New ClientCredentials()
    
                Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials
                Dim OrganizationUri As Uri = New Uri("https://xxx.xxxxxxx.co.uk:444/XRMServices/2011/Organization.svc")
                Dim HomeRealmUri As Uri = Nothing
    
    
                Dim serviceProxy As OrganizationServiceProxy = New OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, Nothing)
                ' Workflow Guid
                Dim _workflowId = New Guid("fa69cfbc-f14e-4dfc-a098-d1cd339cac62")
                ' existing record Guid
                Dim _entityId = New Guid("566AC746-91E3-EA11-9171-0050560105A7")
    
                Dim request As ExecuteWorkflowRequest = New ExecuteWorkflowRequest() With {
                        .WorkflowId = _workflowId,
                  .EntityId = _entityId}
                Dim response As ExecuteWorkflowResponse = CType(serviceProxy.Execute(request), ExecuteWorkflowResponse)
    
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
    
        End Sub

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,961 Moderator on at
    RE: Automating Workflows

    Hi,

    One other option is to use bulk delete to schedule your workflow.

    more details can be found in below article.

    debajmecrm.com/.../

    Please mark my answer verified if i were helpful

  • Suggested answer
    Roma Gupta Profile Picture
    Roma Gupta 725 on at
    RE: Automating Workflows

    Hi Pete

    There is no OOB way to do this. You either move your entire logic to console application and schedule it on task scheduler.

    OR, you could make use of configuration entity (custom CRM entity) where all such additional workflows could have your run time marked.

    Regards

    Roma

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Automating Workflows

    There's not a simple way to do this OnPremise. The 2 main options are:

    1. You could create a custom entity with a datetime field to store the next execution time. In you workflow, create or update a record and set the time of next execution to e.g. the Process Execution time + 6 hours, and have a workflow with a wait condition based on this field to fire your workflow. One issue with this approach is that, if the workflow ever fails, it wont automatically restart
    2. Create a console application that call the CRM API to run the workflow (using the ExecuteWorkflow message). You could create a Schedule Task in Windows to run this on a schedule

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans