locked
How to inject a change into a Pre Create on an Entity RRS feed

  • Question

  • When a new entry is created on an Entity in CRM I need to run a process and update a field as it saves on Create.  I've read I can't use a PreImage on Create since it hasn't saved to the database, so how would this work?  Example, a user fills out the Contact form on CRM 2013 (on premises) and saves, then the plugin runs, queries a SQL database, then updates one of the items in the Contact Entity with the information.  I tried doing this on Post Update, but we have plugins that run on Update that I don't want to run when this data is updated. Seems simpler just to pull it on Create and save it then. 

    Is this possible?  I'm very new to CRM and this is one of my first plugins to write, if that matters :)

    Thanks.

    Sam

    Tuesday, September 16, 2014 9:53 PM

All replies

  • images are used to get the fields of the entity before or after the actual action is performed.

    new attributes can be added to the entity using attributes.add method.

    regards

    Jithesh http://mscrmshop.blogspot.com.au/2010/10/crm-2011-plugin-tutorial.html?m=1

    Tuesday, September 16, 2014 11:06 PM
  • Hello,

    You can write a pre create plugin and inject your attribute to entity propertybag so that it will take your changes before going to the database like below

     entity.Attributes.Add("accountnumber", rndgen.Next().ToString()); //this is a sample to generate random autoid for account


    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, September 17, 2014 5:00 AM
    Moderator
  • Hello,

    You can write a pre create plugin and inject your attribute to entity propertybag so that it will take your changes before going to the database like below

     entity.Attributes.Add("accountnumber", rndgen.Next().ToString()); //this is a sample to generate random autoid for account

    Thanks for the reply, but do you have any examples in doing this?  Since I can't use the PreImage on Create I'm unsure how to reference the entity to add the attribute.  

    Wednesday, September 17, 2014 2:32 PM
  • images are used to get the fields of the entity before or after the actual action is performed.

    new attributes can be added to the entity using attributes.add method.

    regards

    Jithesh http://mscrmshop.blogspot.com.au/2010/10/crm-2011-plugin-tutorial.html?m=1

    Thanks Jithesh for the reply, but this URL seems to create the new record for the entity which I'm not sure applies to what I'm doing.  When the user clicks Save on the form when a new Contact is created the plugin should already get all the data from the CRM forum, I need to add an attribute to this instead of creating a new one.  
    Wednesday, September 17, 2014 2:34 PM
  • You don't need to use image here, you can simply get your entity from input property bag like below is the sample code from SDK, download SDK and check for SampleCode\CS\Plug-ins\

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    if (entity.LogicalName == "account") 
                    {
                        // An accountnumber attribute should not already exist because
    if (entity.Attributes.Contains("accountnumber") == false)
    {
                            // Create a new accountnumber attribute, set its value, and add
               Random rndgen = new Random();
                            entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
    }
    }
    }


    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Wednesday, September 17, 2014 2:57 PM
    Wednesday, September 17, 2014 2:56 PM
    Moderator
  • mahi has already provided you a solution.basically this is how it works.

    1. You create a plugin and register it to a message -Crete in this case.

    2. When ever a create operation is triggered, platform service calls the executre method of the plugin.

    3. Inside the plugin, you get an attribute collection - of the fields added or modified.

    4. If you want to inject new fields, you add the new attribute to the context.InputParameters["Target"] property collection.

    5. If you do don't want a field, set that attribute to null.

    6. If you want no change- remove the attributes from collection.

    same for create and update plugin.

    hth.

    Jithesh

    Wednesday, September 17, 2014 8:09 PM