Sunday, October 2, 2011

Create Custom Workflow for Close an Opportunity in MSCRM 4.0

Step1: Create a "Workflow Active Library" project in Visual Studio. I am using name "CloseOpportunity"

Step2: Take reference of "microsoft.crm.sdk" and "microsoft.crm.sdktypeproxy" in the project.

Step3: Write following code in "CloseOpportunity" namespace.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using System.IO;

namespace CloseOpportunities
{
    [CrmWorkflowActivity("Close Opportunities", "My Custom Workflow Activities")]
       public partial class ExecuteCloseOpportunities: SequenceActivity
       {
              public ExecuteCloseOpportunities()
              {
                     InitializeComponent();
              }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            // Get the context service.
            IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
            IWorkflowContext context = contextService.Context;

            // Use the context service to create an instance of CrmService.
            ICrmService crmService = context.CreateCrmService(true);

            Lookup objLookup = new Lookup();
            objLookup.Value = context.PrimaryEntityId;
            objLookup.name = "Opportunity";
            opportunityclose objopportunityclose = new opportunityclose();
            objopportunityclose.opportunityid = objLookup;

            //Use for Lose an Opportunity
            LoseOpportunityRequest objLoseOpportunityRequest = new LoseOpportunityRequest();
            objLoseOpportunityRequest.OpportunityClose = objopportunityclose;
            objLoseOpportunityRequest.Status = -1;
            LoseOpportunityResponse objLoseOpportunityResponse = (LoseOpportunityResponse)crmService.Execute(objLoseOpportunityRequest);

            //Use for Win an Opportunity
            /*WinOpportunityRequest objWinOpportunityRequest = new WinOpportunityRequest();
            objWinOpportunityRequest.OpportunityClose = objopportunityclose;
            objWinOpportunityRequest.Status = -1;
            WinOpportunityResponse objWinOpportunityResponse = (WinOpportunityResponse)crmService.Execute(objLoseOpportunityRequest);*/

            return ActivityExecutionStatus.Closed;
        }
       }
}

Step4: Create/Add a Strong Name Key for "CloseOppoertuniy" project.

Step5: Rebuild and Compile the "CloseOpportunity".

Step6: Copy "CloseOpportunity.dll" and "CloseOpportunity.pbd" and pase in "\Microsoft Dynamics CRM\Server\bin\assembly" folder on MS-CRM server.

Step7: Register "CloseOpportunity.dll" for your organization through Plugin registeration tool.

Step8: Create new "Close Opportunity" On Demand Workflow from your CRM Web Client.



Step9: Publish that "Close Opportunity" workflow.

Step10: Open "Sales->Opportunities" from your CRM Web Client. Select those opportunities which you want to close. Click on "Run Workflow" button and select "Close Opportunity" workflow and enjoy..........