hello everyone, i need some help.
i have been tasked with creating a plugin that will assign ownership of an account based on the accounts zip code when an account is created.
i figured the easiest way to do this would be to use a plug-in that fires when an account is created.
i created a new entity called new_FSMUpdate, which contains 3 fields
new_FSM: Lookup field
new_zip: single line of text
below is the code i have so far, im kind of stuck here as ive never developed a plugin before and not sure where to go from here
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SET_MSA_Plugin
{
public class setdefaultcredit : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//get plugin execution context
//check to make sure in right stage
//check message name
//check primary entity
var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
if (context.Stage != 20)
throw new InvalidPluginExecutionException("Must run as pre operation stage 20");
if (context.MessageName != "Create")
throw new InvalidPluginExecutionException("registered for " + context.MessageName + " Only create is supported");
if (context.PrimaryEntityName != "account")
throw new InvalidPluginExecutionException("Registered for " + context.PrimaryEntityName + "entity and only account is supported");
//call the tracing service
var tracingService = serviceProvider.GetService(typeof(ITracingService)) as ITracingService;
tracingService.Trace("HEllo from the plug-in");
//plugin execution context
Entity account = context.InputParameters["Target"] as Entity;
//call tracing service
tracingService.Trace("Get zip code form account");
//get the zipcode from account
string zipCode = string.Empty;
if (account.Contains("address1_postalcode"))
zipCode = account.GetAttributeValue<string>("address1_postalcode");
//use service factory
IOrganizationServiceFactory serviceFactory =
serviceProvider.GetService(typeof(IOrganizationServiceFactory))
as IOrganizationServiceFactory;
//call to createorganizationservice
IOrganizationService service = serviceFactory.CreateOrganizationService(null);
}
}
}
i know i need to find the GUID of the owner from the FSMUpdate entity and set it to the owner for the account, but i am not sure how to retrieve or set a lookup field.
any help would be greatly appreciated.