locked
CRM 2013 - C# Plugin - Update field value from plugin RRS feed

  • Question

  • Hi

    I have the following C# Plugin that is retrieving data from a Grid on my Issue Entity

    The data is being retrieved via a FetchXML Query

    The end result is that I have a int called Total which holds the data I want

    I now want to put the data into a field in CRM called "new_businessstring" however when the save button is clicked and the plugin is executed the data isn't showing in the field

    Here is all of my code

    public class CostBusiness : IPlugin
        {
    
            string costToBusiness;
            int total;
    
            public void Execute(IServiceProvider serviceProvider)
            {
    
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
    
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    
    
    
                // 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"];
    
    
    
    
                    //Retrieve Products Details
                    Guid orderID = (Guid)((Entity)context.InputParameters["Target"]).Id;
                    //FetchXML Query will retrieve product details from different entityes, Link-entity is where the different entites used are bought in
                    //Condition Attribute is used to only select the products that is in the Grid on the Order Form
    
                    string fetchxml =
                    @"<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                            <entity name='new_issue'>
                                <attribute name='new_issueid'/>
                                <attribute name='new_name'/>
                                <attribute name='createdon'/>
                                    <order descending='false' attribute='new_issueid'/>
                                    <filter type='and'> 
                                        <condition attribute='new_issueid' operator='eq' value='" + orderID + @"'/> 
                                    </filter> 
                                    <link-entity name='new_costtobusiness' alias='ab' to='new_issueid' from='new_issue_costid'>
                                        <attribute name='new_coststring'/>                                             
                                    </link-entity>
                            </entity>
                      </fetch>";
    
                    EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));
                    {
                        if (result != null && result.Entities.Count > 0)
                        {
                            List<int> _product = new List<int>();
    
                            foreach (Entity _entity in result.Entities)//loop through every record
                                {
                                    
                                   //costToBusiness = _entity.Attributes["new_coststring"].ToString();
                                    costToBusiness = ((AliasedValue)_entity.Attributes["ab.new_coststring"]).Value.ToString();
                                    total = (total) + Convert.ToInt32(costToBusiness);           
                                }
    
                            if(entity.Attributes.Contains("new_businessstring"))
                            {
                                entity.Attributes["new_businessstring"] = total;
                               
                            }
                            else
                            {
                                entity.Attributes.Add("new_businessstring", total);
                                
                            }
                                                  
                        }
    
                    }
    
                }
            }

    The problem is to do with

     if(entity.Attributes.Contains("new_businessstring"))
                            {
                                entity.Attributes["new_businessstring"] = total;
                               
                            }
                            else
                            {
                                entity.Attributes.Add("new_businessstring", total);
                                
                            }
                                     

    Or am I missing something else within the code?

    Thanks, Shaun


    S.Harrison

    Wednesday, July 30, 2014 9:33 AM

Answers

  • Hi Shaun,

    You have register your plugin on post operation. Your plugin code will execute after your record is saved. Therefore the value is not reflecting

    You need to register your plugin on pre-operation event or else you can call service.update() to update that field.

    HTH!

    Wednesday, July 30, 2014 9:56 AM
  • Hi Shaun, 
    "My plugin is working on a Update message and post-operation and synchronous if this information is any help to you" - that's the problem, update your plugin step to pre-operation or pre-validation stage.



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    Wednesday, July 30, 2014 9:58 AM

All replies

  • Hi Shaun,

    Can you debug the code and check if the total variable has any value.

    Also, what is the type of "new_businessstring" field? if it is of string type then try below code. You do not need to check if entity contains "new_businessstring" field. Only below statement will work the set value.

    entity.Attributes["new_businessstring"] = total.ToString();

    HTH!



    Wednesday, July 30, 2014 9:43 AM
  • Hi,

    Please ensure that plugin's step is registered on Pre-Operation or Pre-Validation stage.


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    Wednesday, July 30, 2014 9:50 AM
  • I used throw new InvalidPluginExecutionExeption to check that Total does have a value and it does

    The field "new_businessstring" is a text line so string should work but still no luck (I previously tried my code with .ToString() )

    My plugin is working on a Update message and post-operation and synchronous if this information is any help to you

    Thanks, Shaun


    S.Harrison

    Wednesday, July 30, 2014 9:52 AM
  • Hi Shaun,

    You have register your plugin on post operation. Your plugin code will execute after your record is saved. Therefore the value is not reflecting

    You need to register your plugin on pre-operation event or else you can call service.update() to update that field.

    HTH!

    Wednesday, July 30, 2014 9:56 AM
  • Hi Shaun, 
    "My plugin is working on a Update message and post-operation and synchronous if this information is any help to you" - that's the problem, update your plugin step to pre-operation or pre-validation stage.



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    Wednesday, July 30, 2014 9:58 AM
  • Thanks both for your help!

    Shaun


    S.Harrison

    Wednesday, July 30, 2014 10:07 AM