locked
CRM 2011 plugin, Update cutom entity empty fields RRS feed

  • Question

  • I am trying to use plugin to update custom entity records. I registered the plugin in 'Update', post-operation, synchronous. I wish if I launch then save the form, those empty fields will be populated with calculated values. Some of the empty attributes are hidden fields, some are visible.  The problem is, Entity entity = (Entity)context.InputParameters["Target"]; only contains the attributes to be updated.  (Entity)context.PreEntityImages["PreImage"]; only contains the attributes contain data.  Where are those attributes, which do not contain data?  How do I update them?
    Wednesday, March 26, 2014 12:30 AM

Answers

  • Hi Chicago,

    Here is an example from the SDK that might assist you in updating fields that are not on the entity context. See the bit of code on "accountnumber". Hope this helps.

    for the complete code sample, see SDK\SampleCode\CS\Plug-ins\AccountNumberPlugin.cs

    // The InputParameters collection contains all the data passed in the message request.
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        // Obtain the target entity from the input parameters.
    	Entity entity = (Entity)context.InputParameters["Target"];
        if (entity.LogicalName == "account") 
        {
            // An accountnumber attribute should not already exist because it is system generated.
    		if (entity.Attributes.Contains("accountnumber") == false)
    		{
                // Create a new accountnumber attribute, set its value, and add the attribute to the entity's attribute collection.
    			Random rndgen = new Random();
                entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
    		}
    		else 
    		{
                throw new InvalidPluginExecutionException("The account number can only be set by the system.");
    		}					
    	}
    }


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by chicago bq Wednesday, March 26, 2014 9:22 PM
    Wednesday, March 26, 2014 12:45 AM

All replies

  • Hi Chicago,

    Here is an example from the SDK that might assist you in updating fields that are not on the entity context. See the bit of code on "accountnumber". Hope this helps.

    for the complete code sample, see SDK\SampleCode\CS\Plug-ins\AccountNumberPlugin.cs

    // The InputParameters collection contains all the data passed in the message request.
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        // Obtain the target entity from the input parameters.
    	Entity entity = (Entity)context.InputParameters["Target"];
        if (entity.LogicalName == "account") 
        {
            // An accountnumber attribute should not already exist because it is system generated.
    		if (entity.Attributes.Contains("accountnumber") == false)
    		{
                // Create a new accountnumber attribute, set its value, and add the attribute to the entity's attribute collection.
    			Random rndgen = new Random();
                entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
    		}
    		else 
    		{
                throw new InvalidPluginExecutionException("The account number can only be set by the system.");
    		}					
    	}
    }


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by chicago bq Wednesday, March 26, 2014 9:22 PM
    Wednesday, March 26, 2014 12:45 AM
  • Hi, Eric-

    Thanks for the quick response.  Is ("accountnumber")  an existing attribute?  I have to have these empty fields, 'Sales1', 'Sales2', Sales3' created in design time, because they are displayed in a subgrid in another entity.  What I do is actually to populate these attributes in database table in the entity where they reside.  There are hidden fields in the entity form.

    Wednesday, March 26, 2014 2:40 AM
  • Hi,

    You can implement your requirement using workflow or plugin, but you need think if this is a one time update or you want to do it always, if it's a one time update you can a console application and simply use below code

    Entity ABC=new Entity();

    ABC[idattributename]=keyfieldguid; //you can retrieve this field from entity in another retrieve command

    ABC[yourfieldsname]=somevalue;

    service.update(ABC);

    But if you need to do it on very entity create/update then you can go for plugin, and create preupdate plugin add those fields in entity property bag (they don't need to be there already, you will be adding them as above example provided by Eric)


    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, March 26, 2014 3:17 AM
    Moderator
  • Yes, "accountnumber" is just an example, just replace it with your hidden field schema names. It will work regardless if the attributes are on the form or hidden on the form or not on the form at all.

    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Wednesday, March 26, 2014 3:23 AM
  • Hi, Eric and Mahender-

    Yes, entity.Attributes.Add("new_fy12sales", fy12sales); works even if "new_fy12sales" does not exist in Entity entity = (Entity)context.InputParameters["Target"];

    Now, another problem.  I registered the plugin as "Update", synchronous, either pre-operation or post-operation.  Since I launch my form from subgrid of another entity, user could change field, click 'Save', or change no field, click 'Save'.  My plugin works well if user changes field on form.  However, it does not kick in if user does not change field on the form and click 'Save'.

    So, how do I make the same plugin run if users launches form, changes no field and click 'Save'.

    Wednesday, March 26, 2014 9:38 PM
  • Hello,

    Save operation will be initiated only when crm framework will find using IsDirty, which will return returns a boolean value indicating if there are unsaved changes to the attribute value or not, so if you want to initiate your plugin even if user is not changing any field, you need to set some field may be you can create a flag and set it using javascript on form onload, like below

    Xrm.Page.getAttribute("yourfieldname").setValue(true);

    so it will initiate your plugin, But make sure if user will try to close form without saying it will prompt them if the want to save or cancel their changes, so it may be a confusion for them because they have not done any changes.


    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.

    Thursday, March 27, 2014 12:25 AM
    Moderator
  • Hi,

    Thanks for your explanation.  I may force one of my fields to be a required field.

    Thursday, March 27, 2014 1:46 PM