Tuesday 13 May 2014

CRM 2013 Register Custom Action as an Advance Custom Message

Action in CRM 2013 is a great feature.

In my previous posts, I have talked about Action :

All about Custom Action

Then I also have many example to utilize Action to cater my idea extending xRM Platform.
Now, I am explaining Custom Action as Custom Message that enable for developer to Register as a Step in Plugin Registration Tool!
This is very useful when you want to do integration or calling a server side complex business logic, for example Calculation and Validation.

But, there is a doubt that in Steps, you can only do simple thing, nothing to do? Then, how?
These standard steps of CRM 2013 Action are very limited and nothing much different with other Workflow.

 

image

 

If you think that Available Steps in Action is not be able to accommodate what you want, then? Then, Customize it, do advance customization on it.

Now, first let me guide you. I give you example how to extend Custom Action as Custom Message, I don’t want to give a complex sample now, just give you a concept to understand.

I give you sample how to do calculation with Formula : Money1 + Money2 = MoneySum.

Two input arguments : Money 1 and Money 2

One outpur argument : Money Sum

 

1. Create an Action (You can refer to my above links, and also from that link you can see any links to talk about Action in CRM 2013)

Create your own Message : SimpleCalculation, for example (and give some prefix to indicate it)

(Remember, this is will be your custom message, a verb, a message, that you will use as your universal code contract to be registered to be used by any custom code, just make sure you give them a proper name).

Define your argument as well

 

image

 

2. Why no steps?

I want to let you know that you also can create your logic instead using those standard steps.

3. Activate it.

4. Generate an Early Bound Class to get your action in your class library

Please refer to this article :

 

Generate Custom Action as Early Bound

 

You also can use Early Bound and Late Bound, but for easier way, I use Early Bound as a sample.

5. Create a Plugin Class and put this code :

public class Action_SimpleCalculation : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
#region must to have

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

// Create service with context of current user
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

//create linq context
KonicaBaseContext baseContext = new KonicaBaseContext(service);

//create tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

#endregion

//To get access to the image of the Quote record
EntityReference entityRef = context.InputParameters["Target"] as EntityReference;

//To read the input parameter
Money money1 = context.InputParameters["Money1"] as Money;
Money money2 = context.InputParameters["Money2"] as Money;

Money sum = new Money(money1.Value + money2.Value);

//using this as a response output
context.OutputParameters["MoneySum"] = sum;
}
}

6. Register Your Class as Plugin using Plugin Registration Tool


(You might have to refresh your Plugin Registration Tool until you can see your Action as Message)


image


Register your step :


image


7. Call your Action from your Code


image


8. Pass to your Execution Code your own parameter


image


For example : 2000 and 5000, and pass your Quote Id as Target Entity (if you have this specific entity when you create your Action)


Then, your Total Amount will be : 7000 from 2000 + 5000
Throw an error message for capturing your Total Amount.


Let’s test it, run your code!


9. Test your code by triggering your custom code (In this case I call my Action after my Quote updated)


Here is the result :


image


Here, I don’t put any logic in my Plugin, I just using one single point, that is Custom Action.


Next time, my logic has been changed, I don’t need to change my plugin logic or any custom code about my calculation, because I only implement my code in this Action.


Hope it is informative for you!

No comments:

Post a Comment

Aileen Gusni

Aileen Gusni