Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An error is being thrown because service is not yet defined. It needs to be defined before you can call service.Create.</p> <p>The following is some code that I use for plugins that you might find useful. Seems a bit simpler than your example.</p> <p>EDIT: I have modified the code to show a Create and an Update</p> <pre><code>using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; namespace PluginSample { public class ContactSample : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Get the context IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); try { IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); if (context.MessageName == "Create") { // Creates a contact Entity contact = new Entity("contact"); contact.Attributes.Add("firstname", "SomeName"); contact.Attributes.Add("lastname", "SomeSurname"); service.Create(contact); } else if (context.MessageName == "Update") { if (context.InputParameters.Contains("Target") &amp;&amp; context.InputParameters["Target"] is Entity) { // Update contact Entity contact = new Entity("contact"); contact.Id = new Guid("EBFB262C-5EFF-E211-8BEB-1CC1DEEAE7EC"); contact.Attributes.Add("firstname", "Name Changed"); service.Update(contact); } } } catch (Exception generalException) { throw new InvalidPluginExecutionException("Plugin Failed - Execute :-(", generalException); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload