Tuesday, 13 May 2014

CRM 2013 Utilize Custom Action as Dynamics Error Message

Many times during my project development and implementation, I have requirement to Alert user if they have to input some field, those some fields sometimes is not mandatory during the Form, but it is mandatory for our Logic.

For example, in the Account entity creation or update.

- In Contact entity, City is not mandatory for some accounts type.

- If Contact Role is an Influencer, then their address will be very important to you, including City, State, and Country to complete your Addresses.

- If Contact Role is an Owner, then same as well, their complete address will be mandatory.

- If Contact Roles is not an Influencer nor an Owner then, Address fields will be only Optional or Recommendation.

So, you cannot make the Address fields as Mandatory for all of Situation, yes, you can achieve this by using JS or Business Rules, but remember that if you Form script, then let’s say user do inserting using backend such as other application or using excel import wizard, then you are forced to use Plugin to validate. Lets say you have to implement this in Lead, Account, and Contact, then this is applied to Create and Update Messages, right.

So, what possible situation is by creating a plugin that calls this :

image_thumb1

And I put them to all of my coding.
That’s fine, yes that’s Okay, and it worked.

Then what happened if my user said : Hi, I don’t want to use that messages.
Often, developer use their own way and words to spread an alert, that is not acceptable by some business users because this is till blur and unclear.

So, let’s say first you say : “Please input ‘blabla’ Value!!”, then user said, please change to : “‘blabla’ Value Cannot be Blank”, then change again to more polite command : “Hi Sir/Madam, Please don’t forget to fill the ‘blabla’ Value. Thanks”, then what will you do?

Okay, you have to change all of your Code, it is fine since you just copy paste, but you have various word for ‘blabla’ right? It can be City, State, Country, Fax, Mobile Phone, Membership ID, or whatever you have. So, yes, this is sometimes annoying.

Then, what I did in CRM 2011 is I create an entity, similar to Configuration entity to store my own Error Messages

Key Name
Key Value
Parameter Type
Description
CityBlank
Please input ‘City’ Value
Error Message
Error message if City Blank
StateBlank
Please input ‘State Value
Error Message
Error message if State Blank
CountryBlank
Please input ‘Country Value
Error Message
Error message if Country Blank

Imagine you have many fields, then you have to change all of the Key Value.
It is okay, maybe you can utilize the string.format Message to make the City, State, Country, etc as dynamic value.

But, do you know that in CRM 2013, you can utilize custom action to make your own Error Message more dynamic? This is the more elegant idea to work with Action to get some dynamic Error Message.
If you have known familiar with Action, please refer to this link :

http://aileengusni.blogspot.com/2014/05/crm-2013-let-me-introduce-nice-new_2810.html
http://aileengusni.blogspot.com/2014/05/crm-2013-custom-action-as-configuration.html

Here are the steps :

1. Create an Action

Create-custom-action1_thumb2

2. Define your Arguments

Input : FieldName – String – Required – Input
Output : MessageOutput – String – Required – Output

Define Argument custom action 2_thumb

3. Define your Steps by Assigning the Value

Set-Message-Output-3_thumb2

In this step, using your Input Argument you can create a Dynamic Output, using that FieldName input argument as dynamic value, based on what you pass as input.

4. After define your arguments and Activate your Custom Action

Activate-CA-4_thumb4

5. Using .NET in Plugin you can call your Custom Action

Early Bound

coding-to-call2_thumb3

Late Bound
Call-custom-action-late-bound_thumb3

Call that action in your Plugin
coding-to-call1_thumb2

Here is the code
//EARLY BOUND
        public string ExecuteCustomAction_PleaseInputValue(IOrganizationService service, string strFieldName)
        {
            tfp_Action_PleaseInputValueRequest request = new tfp_Action_PleaseInputValueRequest()
            {
                FieldName = strFieldName
            };

            tfp_Action_PleaseInputValueResponse response = service.Execute(request) as tfp_Action_PleaseInputValueResponse;
            //Processing of response
            return response.MessageOutput;
        }

 //LATE BOUND
        public string ExecuteCustomAction_PleaseInputValue(IOrganizationService service, string strInputParameter, string strInputParameterValue, string strOutputParameter)
        {
            OrganizationRequest req = new OrganizationRequest("tfp_Action_PleaseInputValue");
                    req[strInputParameter] = strInputParameterValue;
                    //execute the request
                    OrganizationResponse response = service.Execute(req);
            return response[strOutputParameter].ToString();
        }

For Reference :

http://a33ik.blogspot.co.il/2013/10/custom-actions-walkthrough-for-net-and.html

To generate Action in Early Bound Class :

http://aileengusni.blogspot.com/2014/05/crm-2013-generate-custom-action-as.html

6. Here is your result

When City is Blank

Business-Process-output-5_thumb4

7. Here if you want to change your Error Message, just change it :

- Go back to your Action
- Deactivate first

image_thumb[1]

- Change the steps

image_thumb[3]

8. Here is your result

image_thumb[6]

Enjoy it!

For your information, for importing Action it can be a big deal in CRM 2013, at least until Rollup 2
Please refer to this :

http://aileengusni.blogspot.com/2014/05/crm-2011crm-2013-import-solution-error.html

Hope it helps!

5 comments:

My Name is..