Using Actions in Microsoft Dynamics CRM 2015


About Actions: Microsoft Dynamics CRM Actions allows us to define our own custom events/messages, similar to existing standard events like createupdate, and delete. Actions are designed using the CRM process editor or using the SDK and we can define parameters (both input and output) for these actions just like we have parameters for standard messages, for example if you want to use a Create message then it takes an entity object as parameter and returns a record's GUID as output. Once activated we can also register a plugin on it.

You can get more details about the action here.

Action Example: We will show how to create and call actions. Actions provide an option to define various types of input and output parameters, but here we will show use of an Entity and EntityReference Parameter. So let's see an example of creating a Contact from an Account, a very simple example for the demo. When creating the contact we want to use information like name, email and telephone from the account entity. Now to pass this information we have two options, we can either pass all these fields from the account to the contact one by one or we can simply pass an account entity object so that the required field can be collected from there. So let's create an action where we will use two input types of parameters, Entity and EntityReference. From the entity we will use the preceding fields and using entityreference we will get it of the sales person (we have created a custom lookup field on contact entity), using the following procedure to design the action.

  • Navigate to Settings->Process->New.

  • Use the following details and click on OK:

    1. Process name: ActionDemo.
    2. Category: Action.
    3. Entity: None(global).
    4. Type: New blank process.

  • Add an Input parameter of Entity type for ParentAccount record like the following:

  • Add another input parameter of type EntityReference to get the Salesperson.

Now let's say we want to use these parameters to create our contact record, so use the following procedure to create the contact record using these parameters:

  • Click on Add Step and the select Create Record step under the drop down.

  • Select Contact under the Create drop down as in the following and click on Set Properties.

  • Now we will be using our Entity object input parameter to fetch a different property and will be setting it in the contact record.

  • Select the field that you want to set, for example we want to set the Account Name, so click on the Account Name field and click on OK after setting the following options from the Form Assistant as in the following:

  • Similarly we can select other fields, such as Email, Mobile, First name and Last name from the account entity object as in the following:

  • Now select the Sales Person field and select out second parameter from the Form assistant as in the following. (Note: we have created a custom lookup for the Sales Person field in the Contact entity.) Click on Save and Close.

Now we need to activate our action and we can call it using server-side and client-side code and using another workflow and dialogs as well (a new feature in CRM 2015). We can use the following code to call our action:
  1. //we have written method to get CRM service object      
  2. IOrganizationService service = connection.GetCRMService();      
  3. //retrieve account record using that we want to create contact      
  4. Entity accountObj = service.Retrieve("account"new Guid("699D9F32-9B10-E511-80FD-C4346BAD3138"), new ColumnSet(true));      
  6. //Create organization Request object and pass action name as parameter      
  7. OrganizationRequest Req = new OrganizationRequest("new_actiondemo");      
  8. Req["ParentAccount"] = accountObj; //passing our first parameter      
  9. Req["Salesperson"] = new EntityReference("systemuser"new Guid("90322292-C8EC-4A72-8B32-B79DEF7DD31C"));      
  10. //use execute method to call action      
  11. OrganizationResponse Respons = service.Execute(Req);      
After execution of the code we can see in the CRM the contact record is created by the action as in the following:

So in the preceding example we showed that if we need to pass multiple parameters then we can use an Entity object instead of passing a field value one by one and the entity reference is used for passing a single entity reference instance.

HIMBAP | Contact US