user3240560
user3240560

Reputation: 360

How to get hidden field value in crm plugin?

I have a hidden field in crm form and setting value for hidden field on save button click using JavaScript.

I am trying to select the hidden filed value in c# plugin code on postcaseCreate event, but getting Key is not found in dictionary error, Can anyone tell me what I am missing here.

if (localContext.PluginExecutionContext.InputParameters.Contains("Target")
    && localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
{
    // Obtain the target entity from the input parmameters.
    caseEntityObj = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
    string productIds = caseEntityObj.FormattedValues["my_hiddenfiedld"].ToString();
    if (caseEntityObj == null) return;
}

Upvotes: 1

Views: 1226

Answers (3)

user3240560
user3240560

Reputation: 360

Thanks Josh,Andril,

I created two steps in plugin code like postCaseCreate,PostCaseupdate event and one postImage and then I am getting value like below on postCase create event

string productIds = caseEntityObj["my_hiddenfiedld"].ToString();

For update getting the value from image. It is working fine. thanks a lot guys.

 Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
            productIds = postImageEntity.Attributes["my_hiddenproducts"].ToString();

Upvotes: 0

Josh Painter
Josh Painter

Reputation: 4111

The "Target" input parameter has only the attributes that were submitted to the framework. The system forms only submit attributes that contain changed data (or do not equal default values) as an optimization. If you created your own client UpdateRequest or CreateRequest and only submitted a few attributes, then your plugin's Target collection would only contain those few attributes as well.

In your case, I'm guessing that your hidden field isn't changing on an update and so it isn't included in your Target attribute collection.

If your plugin logic will always need to know the current value of a field regardless of whether it is included in the submitted attribute collection, you need to register a PreImage. Registering a PreImage tells CRM that you always need to know the current value of a certain field during this plugin. This is the value of the field before the current action.

Docs on PreImages: https://msdn.microsoft.com/en-us/library/gg309673.aspx#bkmk_preandpost

Pseudo code:

  1. Use .Contains() to check Target attribute collection for attribute name.
  2. If true, get value of attribute from Target attributes as this is the actual change just submitted by client.
  3. If false, use .Contains() to check PreImage attribute collection for attribute name.
  4. If true, get value of attribute from PreImage as this is the most recent value from the database.

Hope that helps!

Upvotes: 1

Andrew Butenko
Andrew Butenko

Reputation: 5446

Try to replace line

string productIds = caseEntityObj.FormattedValues["my_hiddenfiedld"].ToString();

with line

string productIds = caseEntityObj["my_hiddenfiedld"].ToString();

Why do you want to use FormattedValues? Is hidden field optionset? If yes and you need to get correspond text you will have to retrieve attribute using RetrieveAttribute message and get text of optionset from response.

Upvotes: 1

Related Questions