Wednesday, May 2, 2007

Calling a Workflow Rule from a button

Here's a handy bit of code for calling a Workflow rule from a custom button in CRM. Put the code below into the Page_Load handler of an aspx page. In this example I call the aspx page and pass an Account record GUID (accountId) which is fed into the workflow rule.

// CRM Service Setup
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.Url = "http://server/mscrmservices/2006/crmservice.asmx";

// Setup the request
string accountId;
accountId = Request.QueryString["accountId"];
ExecuteWFProcessRequest request = new ExecuteWFProcessRequest();

// Triggers Workflow process
request.ProcessId = new Guid("{E4206735-0379-4DE5-A453-AC7A7DA1462B}");
request.EntityMoniker = new Moniker();
request.EntityMoniker.Id = new Guid(accountId);
request.EntityMoniker.Name = EntityName.account.ToString();

// Execute the request
ExecuteWFProcessResponse response = (ExecuteWFProcessResponse) service.Execute(request);

No comments: