locked
Problem retrieving the postimage in the SetState PreStage Plugin(The given key was not present in the dictionary) RRS feed

  • Question

  • I have a plugin for SetStatePreStage

    I would like to get the statuscode before deacivate my entity(I have the statuscode:Close,Closed-reffer to other company and ...).I would like to perform some validation based on these values.

    When I try to deactive the entity and I pick the closed status, Preimage in the setPrestage give the previous statuscode which is Open(I want Close).

    Using the context.InputParameters.Properties["statuscode"] I can get the correct integer value of statuscode but I need the name associate to it(for example close not the integer value which is 1). As a result I changed my image registeration to include both preimage and postimage. Please see the image below

     

    when I run the below statement with the preimage it returns "Open"

    const string entityName = "new_ohsassignment";

    DynamicEntity preimage = (DynamicEntity)context.PreEntityImages.Properties[entityName];

    string statusCode = ((Status)preimage.Properties["statuscode"]).name;

    But when I run the below statement with the postimage to get the Close using teh below statement

    DynamicEntity postimage = (DynamicEntity)context.PostEntityImages.Properties[entityName];

    string statusCode = ((Status)postimage.Properties["statuscode"]).name;

    it throughs me an error as postimage does not contain anything.(The given key was not present in the dictionary )

    I am not sure if for the message SetStateDynamicEntity for Pre Stage if I can have the PostImage or not?

    If I can use the postImage, do you have any idea what I did wrong  in the above steps?And if I can not use the PostImage how can I get the the name of statuscode

    Any idea would be very much appreciated.

    Note:I am using the CRM 4

    Thanks,

     

     

     



    • Edited by Maryam_r Friday, August 26, 2011 5:03 PM
    Friday, August 26, 2011 4:51 PM

Answers

  • Hi,

     

    If your plugin is running for one entity (not running on many entity status change) then you can use the status reason integer value or else you have to retrieve enntity metadata using CRM Metadata service RetrieveEntityRequest, in plugin you can get the metadata service using

    PluginContext.CreateMetadataService();  
    

     

    You can get the status reason label using the Entity Metadata request using the metadata service, find also below the sample code:

     

     

    RetrieveEntityResponse ts_ResponseRequest = null;

    ts_RetrieveRequest.LogicalName = "<entityname>";

    ts_RetrieveRequest.EntityItems = EntityItems.IncludeAttributes;

    ts_ResponseRequest = (RetrieveEntityResponse)crmMetadataService.Execute(ts_RetrieveRequest);

    // You can get the attribute meta data using ts_ResponseRequest.EntityMetadata.Attributes 

    // You may also need to iterate to get the Status Reason attribute metadata (then you can get the label value on the base of integer 

     

     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Jehanzeb.Javeed Friday, August 26, 2011 6:52 PM
    • Marked as answer by Maryam_r Tuesday, August 30, 2011 3:12 PM
    Friday, August 26, 2011 6:51 PM
  • Thanks much for the quick respond.

     

    The reason I wanted the image is that I need the name of 'Status Reason' not State.

    The context gives me

    inactive value for State

    but for status reason it returns the value not the name using the following statement

    string statusCode = context.InputParameters.Properties["status"].ToString();
    statusCode Result: 2 or 3

    I needed the name not the integer value.

    Is there any way to retrieve the text(name) of the status Reason instead of the integer value of it?  

    At first I thought I could use the postimage.But it seems that PostImage is not available in the setstate Prestage.So I need to use another way.

     

    Thanks agian for your help.

    • Marked as answer by Maryam_r Friday, August 26, 2011 8:08 PM
    Friday, August 26, 2011 6:10 PM

All replies

  • Hi,

    Why do you need Pre-images and Post-images (it will be available after the operation in Post-Operation stage) , if you want to register for the Pre-Operation stage then you can use the plugin code i,e,

    public void Execute(IPluginExecutionContext context)
    

    {

    // In case of SetState and SetStateDynamicEntity message InputParameter

    // would contain EntityMoniker parameter

    Moniker entity = null;

    if (context.InputParameters.Properties.Contains("EntityMoniker") &&

    context.InputParameters.Properties["EntityMoniker"] is Moniker)

    {

    entity = (Moniker)context.InputParameters.Properties["EntityMoniker"];

    // Get the state to which record is to be changed

    // If Active the record is being activated

    // If Inactive the record is being deactivated

    string  state=

    (string)context.InputParameters.Properties[ParameterName.State];

    // Verify that the entity represents an account.

    if (entity.Name == EntityName.contact.ToString() && state=="Active")

    {

        throw new InvalidPluginExecutionException("Record can't be activated");

    }

    }

    }


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Friday, August 26, 2011 5:01 PM
  • Thanks much for the quick respond.

     

    The reason I wanted the image is that I need the name of 'Status Reason' not State.

    The context gives me

    inactive value for State

    but for status reason it returns the value not the name using the following statement

    string statusCode = context.InputParameters.Properties["status"].ToString();
    statusCode Result: 2 or 3

    I needed the name not the integer value.

    Is there any way to retrieve the text(name) of the status Reason instead of the integer value of it?  

    At first I thought I could use the postimage.But it seems that PostImage is not available in the setstate Prestage.So I need to use another way.

     

    Thanks agian for your help.

    • Marked as answer by Maryam_r Friday, August 26, 2011 8:08 PM
    Friday, August 26, 2011 6:10 PM
  • Hi,

     

    If your plugin is running for one entity (not running on many entity status change) then you can use the status reason integer value or else you have to retrieve enntity metadata using CRM Metadata service RetrieveEntityRequest, in plugin you can get the metadata service using

    PluginContext.CreateMetadataService();  
    

     

    You can get the status reason label using the Entity Metadata request using the metadata service, find also below the sample code:

     

     

    RetrieveEntityResponse ts_ResponseRequest = null;

    ts_RetrieveRequest.LogicalName = "<entityname>";

    ts_RetrieveRequest.EntityItems = EntityItems.IncludeAttributes;

    ts_ResponseRequest = (RetrieveEntityResponse)crmMetadataService.Execute(ts_RetrieveRequest);

    // You can get the attribute meta data using ts_ResponseRequest.EntityMetadata.Attributes 

    // You may also need to iterate to get the Status Reason attribute metadata (then you can get the label value on the base of integer 

     

     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Jehanzeb.Javeed Friday, August 26, 2011 6:52 PM
    • Marked as answer by Maryam_r Tuesday, August 30, 2011 3:12 PM
    Friday, August 26, 2011 6:51 PM
  • Many thanks for your quick respond and useful help. This was the solution I was looking for.

    I was not sure if there is an easier way or not but it seems that the solution is Meta data .

     

    Thanks,

     

    Maryam

    Friday, August 26, 2011 8:09 PM
  • Hi,

    Have you found any of my reply useful? i hope that you will Mark my Post also as Answer.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Friday, August 26, 2011 9:53 PM
  • Hi Javeed,

     

    Yes it was definitely useful and I have lareday both vote for it and marked it as an answer.

     

    Thanks again,

     

    Maryam

    Tuesday, August 30, 2011 3:13 PM