Monday 6 March 2017

sample plugin code MSCRM create and update

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;

using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Query;

using System.ServiceModel;

namespace MscrmOnlinePlugin



{

public class AccountUpdate : IPlugin



{

public void Execute(IServiceProvider serviceProvider)



{

// Extract the tracing service for use in debugging sandboxed plug-ins.

// If you are not registering the plug-in in the sandbox, then you do

// not have to add any tracing service related code.

ITracingService tracingService =

(ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.

IPluginExecutionContext context = (IPluginExecutionContext)

serviceProvider.GetService(typeof(IPluginExecutionContext));

// 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"];

Entity entPost = (Entity)context.PostEntityImages["postImage"];

// Verify that the target entity represents an entity type you are expecting.

// For example, an account. If not, the plug-in was not registered correctly.

if (entity.LogicalName != "account" )

return;

// Obtain the organization service reference which you will need for

// web service calls.

IOrganizationServiceFactory serviceFactory =

(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try



{

if (context.MessageName.ToLower() == "create")



{

Guid parentAcc = new Guid("C3390F45-D5FF-E611-8116-C4346BDCFDE1");

entity["websiteurl"] = "google@gmail.com";

entity["parentaccountid"] = new EntityReference("account", parentAcc);

entity["address1_addresstypecode"] = new OptionSetValue(1);

// service.Update(entity);



}

if (context.MessageName.ToLower() == "update")



{

Guid parentAcc = new Guid("C3390F45-D5FF-E611-8116-C4346BDCFDE1");

Entity account = new Entity();



account.Id = entity.Id;

account.LogicalName = entity.LogicalName;

account["websiteurl"] = "google@gmail.com";

account["parentaccountid"] = new EntityReference("account", parentAcc);

account["address1_addresstypecode"] = new OptionSetValue(1);



service.Update(account);

 

}

if (context.MessageName.ToLower() == "update")



{

if (entPost.Attributes.Contains("address1_addresstypecode"))



{

if (((OptionSetValue)entPost.Attributes["address1_addresstypecode"]).Value == 1)



{

Entity account = new Entity();



account.Id = entPost.Id;

account.LogicalName = entPost.LogicalName;

account["address1_addresstypecode"] = new OptionSetValue(2);



service.Update(account);

}

}

}

}

catch (FaultException<OrganizationServiceFault> ex)



{

throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);



}

catch (Exception ex)



{

tracingService.Trace("MyPlugin: {0}", ex.ToString());

throw;



}

}

}

}

}


No comments:

Post a Comment