title: Setting values to fields
body: <p>I created a plugin counting the number of records of an entity related to another one, if there is more than one record, I retrieve the last record. If that record is inactive and a specific field's value >0, then I add the value of this field to the newly created one...But can't get it to work...</p>
  
  <p>Any help would be great !</p>
  
  <p><strong>Edit:</strong> 
  The plugin is registered to "new_lignecontrat" wich contains the attributes "new_unitesutilisees" and "new_unitesrestantes".</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Ok solved it ! All I needed was simply to get an EntityReference for the lookup field and rearrange a bit my code...</p>
  
  <p>Thanks for your help !</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Query;
  using System.Web;
  using System.Collections;
  using Microsoft.Crm.Sdk.Messages;
  using Microsoft.Xrm.Sdk.Messages;
  
  namespace ClassLibrary1
  {
      public class StatusContrat : IPlugin
      {         
          public void Execute(IServiceProvider serviceProvider)
          {
              // Instanciation des services
              IPluginExecutionContext context     = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
              IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
              IOrganizationService service        = factory.CreateOrganizationService(null);
  
              Entity statusEntité = (Entity)context.InputParameters["Target"];
  
                      // Récupération des lignes de contrat liées au contrat courant
                  FetchExpression fetch   = new FetchExpression("&lt;fetch distinct='false' mapping='logical'&gt;" +
                 "&lt;entity name='new_contrats'&gt;" +
                 "&lt;link-entity name='new_lignecontrat' alias='nombreligne' from='new_contratsid' to='new_contratsid'&gt;" +
                 "&lt;/link-entity&gt;" +
                 "&lt;/entity&gt;" +
                 "&lt;/fetch&gt;");
  
                  EntityCollection lines = service.RetrieveMultiple(fetch);
  
                      // Vérification qu'il y a au moins une ligne de contrat associée
                      if (lines.Entities.Count &gt; 0)
                      {   
                          if (lines.Entities.Last().GetAttributeValue&lt;OptionSetValue&gt;("statecode").Value == 1)
                          {
                             if (lines.Entities.Last().GetAttributeValue&lt;float&gt;("new_unitesrestantes")&lt;0)
                             {
                                var unitesRestantes = (statusEntité.GetAttributeValue&lt;float&gt;("new_unitesrestantes")) + (lines.Entities.Last().GetAttributeValue&lt;float&gt;("new_unitesrestantes"));
                                var unitesUtilisee =  (statusEntité.GetAttributeValue&lt;float&gt;("new_unitesutilisees")) - (lines.Entities.Last().GetAttributeValue&lt;float&gt;("new_unitesutilisees"));
  
                                  statusEntité ["new_unitesutilisees"] = unitesUtilisee;
                                  statusEntité ["new_unitesrestantes"] = unitesRestantes;
  
                                  service.Update(statusEntité);
                             }
                          }
                      }
                      else
                      {
  
                          statusEntité["new_unitesutilisees"] = "0";
                          statusEntité["new_unitesrestantes"] = statusEntité["new_unitestotales"];
  
                          service.Update(statusEntité);
  }                        
  
           }
        }
     }
  </code></pre>