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 ..


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

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. 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

  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. 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

Leave a reply to cccam line Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.