Tuesday, September 17, 2013

CRM 2013 – Create Custom Actions

The new version of Microsoft Dynamics CRM includes a feature called Custom Actions. In this post I want to show the process of creating an Action in the CRM UI and access it over a Console Application.

The Problem to solve:

Create a Custom Action, that creates a task or a phonecall for an existing account record, based on the “Industry” Optionset. It should also be possible to add the description of the task in the Request.

1. Create the Custom Action

In this example we create the action in the CRM UI. It is also possible to create it in Code (only OnPremise).

  • Navigate to Settings –> Processes

processes

  • Click “New” and set “CreateSalesActivities” as Process name
  • Set “Action” as Category
  • Select “New blank process”
  • Set “Account” as Entity
  • Click “OK”

action_screen_basic

  • Add the input parameter for Description

action_screen_input

  • Configure Ruleset

rules_for_action 

action_screen_ruleset_call

Accounting –> Create PhoneCall

action_screen_ruleset_task

Others –> Create Task

  • Save the Custom Action
  • Activate the Custom Action

activate_action 

2. Execute the Custom Action

For executing the Request i have created a Console Application. You can check the sources of my PluginQuickDeploy-Project when you need support creating it.

Executing Accounting-Case:

   1: private static void ExecuteCustomActionAccounting()
   2: {
   3:     IOrganizationService service = GenerateService();
   4:     Entity account = new Entity("account");
   5:     account.Attributes.Add(new KeyValuePair<string, object>("name",
   6:         string.Format("Account generated at {0}", DateTime.Now.ToShortTimeString())));
   7:     account.Attributes.Add(new KeyValuePair<string, object>("industrycode", 
   8:         new OptionSetValue(1)));
   9:     Guid accountId = service.Create(account);
  10:  
  11:     OrganizationRequest req = new OrganizationRequest("new_CreateSalesActivities");
  12:     EntityReference accountReference = new EntityReference("account", accountId);
  13:     req.Parameters.Add(new KeyValuePair<string, object>("Target", accountReference));
  14:     req.Parameters.Add(new KeyValuePair<string, object>("Description", "Text from Request"));
  15:  
  16:     OrganizationResponse resp = service.Execute(req);
  17: }

Executing Default-Case:



   1: private static void ExecuteCustomActionDefault()
   2: {
   3:     IOrganizationService service = GenerateService();
   4:     Entity account = new Entity("account");
   5:     account.Attributes.Add(new KeyValuePair<string, object>("name", 
   6:         string.Format("Account generated at {0}", DateTime.Now.ToShortTimeString())));
   7:     Guid accountId = service.Create(account);
   8:  
   9:     OrganizationRequest req = new OrganizationRequest("new_CreateSalesActivities");
  10:     EntityReference accountReference = new EntityReference("account", accountId);
  11:     req.Parameters.Add(new KeyValuePair<string, object>("Target", accountReference));
  12:     req.Parameters.Add(new KeyValuePair<string, object>("Description", "Text from Request"));
  13:  
  14:     OrganizationResponse resp = service.Execute(req);
  15: }


3. Testing


After execution of the both requests, a phonecall is generated and attached to the first account, a task is generated and attached to the second account. The description is passed by the request.


Created Call:


callCreated Task:


 task



Additional Information - Output Parameters


It is also possible to retrieve information from the custom action. Therefore you have to define Output-Parameters and set them in the rule wizard. In this example the Custom Action returns the Entity Reference of the created task.


return_value


assign_value_step


assign_value_rule


responseThe feature is very interesting and i look forward to use it in one of my next projects (lets hope CRM 2013 will be available and stable soon).


cheers

5 comments:

  1. Markus i tried ur code but am not able trigger the plug-in on requirement am not understanding where the prob exactly help me as soon as...

    ReplyDelete
  2. am Trying to trigger ur code on online crm will it works?
    Actions will work on online ?

    ReplyDelete
  3. Hi Ajith,

    the custom action should also work online. You just have to enable the trigger-application (console application) for online access. You can find details in the sdk or here http://www.c-sharpcorner.com/UploadFile/aravindbenator/crm-2011-online/.

    cheers

    ReplyDelete
  4. Hello!
    While searching for CRM Actions new feature,i came across this blog and it was helpful.
    But i don't want to use the PluginQuickDeploy tool.Jus want to use the same functionality within a console Application.Can you please help me through this?

    Thanks,
    Manu.

    ReplyDelete
  5. Hi Manasa,

    the PluginQuickDeploy is a console application. I mentioned it as a demo for creating a console application that is able to talk to the CRM Organization Endpoint.

    If you need information regarding this, check out the code at http://pqd.codeplex.com/SourceControl/latest

    You don't have to use the tool itself but you can use the structure of the application to implement your own logic in the Visual Studio project.

    Best wishes,
    Markus

    ReplyDelete