locked
How to verify the owner is changed or not before Updating a entity record - CRM 2011 RRS feed

  • Question

  • Hi,

    I want to verify whether the owner has been changed before updating a record. Do i need to look for owner in input paraeters or i should use PreImage?

    Could any body help me out by providing the code snippet?

    Thanks in Advance


    Sreeni Pavalla

    Thursday, January 24, 2013 5:17 AM

Answers

  • In order to check if the owner has changed; you will need to register your plug-in on Assign message and Pre-operation execution pipeline.

    And then you will get the user using the following syntax

    if (context.InputParameters.Contains("Asignee") && context.InputParameters["Asignee"] is EntityReference)
    {
    	EntityReference user = (EntityReference)context.InputParameters["Assignee"];
    }

    Hope this will help you!


    Piyush

    Thursday, January 24, 2013 5:57 AM
  • Hi Sreeni,

    Change this line of code in your plugin,

    var selectedEntity=(Entity)context.InputParametes["Target"];

    to

    var selectedEntityRef=(EntityReference)context.InputParametes["Target"];

    and then you have to retrieve the selectedEntity from the this EntityReference's Id using your service.

    In plugin's that gets fired on assign message we can't get the Entity from the InputParameters but an EntityReference to it. 


    Thank you, DT


    Thursday, January 24, 2013 2:50 PM

All replies

  • In order to check if the owner has changed; you will need to register your plug-in on Assign message and Pre-operation execution pipeline.

    And then you will get the user using the following syntax

    if (context.InputParameters.Contains("Asignee") && context.InputParameters["Asignee"] is EntityReference)
    {
    	EntityReference user = (EntityReference)context.InputParameters["Assignee"];
    }

    Hope this will help you!


    Piyush

    Thursday, January 24, 2013 5:57 AM
  • Hi,

    In case of create plugin you can check for input parameter property bag, if the value is present in input property bag it means some has changed this value, but in case of update you need to use pre and post image to make sure if user has changed the owner value, it may be that some will select some different user and then again change it back to same user.

    HTH


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, January 24, 2013 6:54 AM
    Moderator
  • Hi,

    In case of create plugin you can check for input parameter property bag, if the value is present in input property bag it means some has changed this value, but in case of update you need to use pre and post image to make sure if user has changed the owner value, it may be that some will select some different user and then again change it back to same user.

    HTH


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    It is definitely not needed to use pre or post image in this case. When an update occurs, only updated fields would be passed to plugin through context. InputParameters. Hence, you could check with this:

    if (context.InputParameters.contains("ownerid"))

    {

    rest of the code

    }

    This will goes through if, if the update contains change of the ownerid.

    Thursday, January 24, 2013 7:13 AM
    Moderator
  • Piyush,

    I have created Message as "Assign" and tried assigning record to some one else. But the plugin is not at all getting fired.

    I have added Update Step.

    In Post Event

    in Pre-Event

    Any other suggestions


    Sreeni Pavalla


    Thursday, January 24, 2013 1:24 PM
  • Hi Sreeni,

    Change this line of code in your plugin,

    var selectedEntity=(Entity)context.InputParametes["Target"];

    to

    var selectedEntityRef=(EntityReference)context.InputParametes["Target"];

    and then you have to retrieve the selectedEntity from the this EntityReference's Id using your service.

    In plugin's that gets fired on assign message we can't get the Entity from the InputParameters but an EntityReference to it. 


    Thank you, DT


    Thursday, January 24, 2013 2:50 PM
  • Hi DT,

    What difference it makes in this context? But still i tried with your suggestion. it is throwing error as it is not able to cast.

    Thank you,


    Sreeni Pavalla

    Thursday, January 24, 2013 3:54 PM
  • Piyush,

    I have created Message as "Assign" and tried assigning record to some one else. But the plugin is not at all getting fired.

    I have added Update Step.

    In Post Event

    in Pre-Event

    Any other suggestions


    Sreeni Pavalla


    You will not get the "ownerid" anywhere in the inputparameters, but instead if the plugin fires on Pre-Operation, Assign message then your input parameters check should be something similar to following

    if (context.InputParameters.Contains("Asignee") && context.InputParameters["Asignee"] is EntityReference)
    {
    	EntityReference user = (EntityReference)context.InputParameters["Assignee"];
    }

    the EntityReference user in above code is the new owner that was assigned to the record. You don't need to look for the ownerid anywhere. Just do what you needed to do with this reference.

    Also, looking at your code for this plugin you are casting context.InputParameters["Target"] as Entity but this will fail as the "Target" here would be of type EntityReference, so you'll need to correct your code accordingly.

    HTH, let me know if you have any questions.


    Please don't forget to mark the answer, if it helps. Piyush


    • Edited by Piyush Paliwal Friday, January 25, 2013 3:31 AM Format changed
    • Proposed as answer by Mandar M Joshi Friday, January 25, 2013 10:38 AM
    Friday, January 25, 2013 3:29 AM
  • Hi Piyush,

    Thanks for the reply.

    message Assign on Pre-Operation of account/contact is not at all initiating the plugin.                                                                                                     And i have tried the code to cast

     var selectedEntityRef=(EntityReference)context.InputParametes["Target"];

    But it is throwing error which is casting error.

    Any other suggestions.


    Sreeni Pavalla

    Friday, January 25, 2013 3:59 AM
  • Can you paste your complete plugin code so that I can review it.

    Please don't forget to mark the answer, if it helps. Piyush

    Friday, January 25, 2013 4:01 AM