title: Plugin code to update another entity when case is created mscrm 2011
body: <p>Im new with plugin. my problem is, When the case is created, i need to update the case id into ledger. What connect this two is the leadid. in my case i rename lead as outbound call.</p>
  
  <p>this is my code. I dont know whether it is correct or not. Hope you guys can help me with this because it gives me error. I manage to register it. no problem to build and register but when the case is created, it gives me error.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.IO;
  using System.ServiceModel;
  using System.ServiceModel.Description;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Query;
  using Microsoft.Xrm.Sdk.Messages;
  using Microsoft.Xrm.Sdk.Client;
  using System.Net;
  using System.Web.Services;
  
  
  /*
   * Purpose: 1) To update case number into lejar
   *         
   * Triggered upon CREATE message by record in Case form.
   */
  namespace UpdateLejar
  {
      public class UpdateLejar : IPlugin
      {
          /*public void printLogFile(String exMessage, String eventMessage, String pluginFile)
          {
              DateTime date = DateTime.Today;
              String fileName = date.ToString("yyyyMdd");
              String timestamp = DateTime.Now.ToString();
  
              string path = @"C:\CRM Integration\PLUGIN\UpdateLejar\Log\" + fileName;
              //open if file exist, check file..
              if (File.Exists(path))
              {
                  //if exist, append
                  using (StreamWriter sw = File.AppendText(path))
                  {
                      sw.Write(timestamp + " ");
                      sw.WriteLine(pluginFile + eventMessage + " event:  " + exMessage);
                      sw.WriteLine();
                  }
  
              }
  
              else
              {
                  //if no exist, create new file
                  using (StreamWriter sw = File.CreateText(path))
                  {
                      sw.Write(timestamp + " ");
                      sw.WriteLine(pluginFile + eventMessage + " event:  " + exMessage);
                      sw.WriteLine();
                  }
              }
          }*/
  
          public void Execute(IServiceProvider serviceProvider)
          {
  
              ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
              IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
  
              IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
              IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
  
              //for update and create event
              if (context.InputParameters.Contains("Target") &amp;&amp;
                  context.InputParameters["Target"] is Entity)
              {
                  // Obtain the target entity from the input parmameters.
                  Entity targetEntity = (Entity)context.InputParameters["Target"];
  
                  // Verify that the entity represents a connection.
                  if (targetEntity.LogicalName != "incident")
                  {
                      return;
                  }
  
                  else
                  {
                      try
                      {
  
                          //triggered upon create message
                          if (context.MessageName == "Create")
                          {
                              Guid recordid = new Guid(context.OutputParameters["incidentid"].ToString());
  
                              EntityReference app_inc_id = new EntityReference();
                              app_inc_id = targetEntity.GetAttributeValue&lt;EntityReference&gt;("new_outboundcalllid");
                              Entity member = service.Retrieve("new_lejer", ((EntityReference)targetEntity["new_outboundcallid"]).Id, new ColumnSet(true));
                              //DateTime createdon = targetEntity.GetAttributeValue&lt;DateTime&gt;("createdon");
  
                              if (app_inc_id != null)
                              {
                                  if (targetEntity.Attributes.Contains("new_outboundcallid") == member.Attributes.Contains("new_outboundcalllistid_lejer"))
                                  {
  
  
  
                                      member["new_ringkasanlejarid"] = targetEntity.Attributes["incidentid"].ToString();
                                      service.Update(member);
                                  }
                              }
  
                          }
  
                          tracingService.Trace("Lejar updated.");
                      }
  
  
                      catch (FaultException&lt;OrganizationServiceFault&gt; ex)
                      {
                          //printLogFile(ex.Message, context.MessageName, "UpdateLejar plug-in. ");
                          throw new InvalidPluginExecutionException("An error occurred in UpdateLejar plug-in.", ex);
                      }
  
                      catch (Exception ex)
                      {
                          //printLogFile(ex.Message, context.MessageName, "UpdateLejar plug-in. ");
                          tracingService.Trace("UpdateLejar: {0}", ex.ToString());
                          throw;
                      }
                  }
              }
          }
      }
  }
  </code></pre>