Executing CRM Workflow programmatically from an external application. CRM 4.0 using ExecuteWorkflowRequest

There is a method named ExecuteWorkflowRequest using which we can execute our workflow programmatically. We had a requirement to find all the opportunities which haven’t been modified for past 30 days and to decrease their probability attribute value by 10.

Now the thing over here was that there wasn’t any specific event against which we could have fired the above workflow. So we thought of writing an application which than we could scheduled, which will periodically run the above workflow

This is how we implemeneted it within a windows application

private void Form1_Load(object sender, EventArgs e){

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.OrganizationName = “organizationName”;

//0 – AD

//1 – Passport

//2 – Form Authentication

token.AuthenticationType = 0;

CrmService crmService = new CrmService();

crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

crmService.CrmAuthenticationTokenValue = token;

crmService.Url = http://servername:port/mscrmservices/2007/crmservice.asmx”;

try{

// Create an ExecuteWorkflow request.

ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();

//Assign the ID of the workflow you want to execute to the request.

// use this query to get the id select parentworkflowid,name,* from dbo.Workflow

// id is the parentworkflowid

request.WorkflowId = new Guid(“21B9528D-D13D-4B93-9F91-FA7468D3C82C”);

// We want to run it against all the opportunity which are in open state

ArrayList OpportunityGuids = GetOpportunityGuids(crmService);

foreach (String oppGuid in OpportunityGuids){

//Assign the ID of the entity to execute the workflow on to the request.

request.EntityId = new Guid(oppGuid);

ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)crmService.Execute(request);}

// Execute the workflow. }

catch (SoapException ex){

// write in log}

catch (Exception ex){

// write in log} }

private ArrayList GetOpportunityGuids(CrmService crmService){

// using QueryByAttribute to retrieve all the opportunity having statuscode as 1 i.e. Open

QueryByAttribute myOppQuery = new QueryByAttribute();

myOppQuery.Attributes = new String[] { “statuscode” };

myOppQuery.Values = new String[] {“1”};

ColumnSet myCols = new ColumnSet();

myOppQuery.ColumnSet = myCols;

myOppQuery.EntityName = EntityName.opportunity.ToString();

WindowsFormsApplication2.CrmSdk.BusinessEntityCollection myOppCollection= crmService.RetrieveMultiple(myOppQuery);

ArrayList opportunityGuids = new ArrayList();

foreach (WindowsFormsApplication2.CrmSdk.BusinessEntity opp in myOppCollection.BusinessEntities ){

opportunity myOpp = (opportunity)opp;

opportunityGuids.Add(myOpp.opportunityid.Value.ToString()); }

return opportunityGuids;

}

Bye ..


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

8 thoughts on “Executing CRM Workflow programmatically from an external application. CRM 4.0 using ExecuteWorkflowRequest”

  1. hi Nishant

    yes u r right,
    i have made the feedback form in ASP.NET name feedback.aspx, yes user can be internal or external both have to enter the feedback about the event from the link that they recieved from mail.
    now the problem is how to integrate it
    feild information are:
    name:
    email:
    feedback related to:
    comments:
    rate the event:

    link are sending to the list of user, or those how attend the event, via mail.

    i have already create the custom entity in MS CRM named feedback and a feedback form and the fields are same as above in both. is it sufficent.
    more over if u send feedback back form or how to integrate it in mS CRM code i will use that only. plz rply as soon as possible

    thanks
    manish

    Like

  2. hi Nishant

    actually i m doing some functionality about the feedback form in MS CRM
    i need feedback form in Event management it is used for attendee’s feedback, a link is send through mail, and attendee’s can enter the feedback from the feedback form and that data is populate in MS CRM Feedback entity. i made a entity of same field those are in feedback form, i want that whn all the entity is filled in form and then attendee submit the form all the data attendee enter populate in MS CRM.

    is this information is sufficent to u or u required more, i hope u will soon rply to my mail. i m in very tense stage plz help me out.

    thanks
    manish

    Like

  3. Thank you for an excelent article.

    I need the oposit of what you have done.

    I have a need to programaticaly cancele a workflow that is in wait state for an opprtunity from an update plug-in. Do you know how do go about this?

    avner

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s