Basic Plugin Code in D365 using C#
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
namespace apy.CWA.Arcese.Security
{
public class PostOperationCreateQuote : IPlugin
{
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);
Entity entity = null;
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
if (context.MessageName == "Update")
{
entity = (Entity)context.PostEntityImages["QuotePostImage"];
}
for (int i = 0; i < count; i++)
{
Entity quote = new Entity("quote");
// Decimal
if (entity.Attributes.Contains(" apy_ldmloadingmeters"))
{
quote[" apy_ldmloadingmeters"] = entity.GetAttributeValue<decimal>(" apy_ldmloadingmeters");
}
// Currency/Money
if (entity.Attributes.Contains(" apy_multirequestamount"))
{
quote[" apy_multirequestamount"] = entity.GetAttributeValue<Money>(" apy_multirequestamount");
}
// Date and Time
if (entity.Attributes.Contains(" apy_proposedpickupdatetime"))
{
quote[" apy_proposedpickupdatetime"] = entity.GetAttributeValue<DateTime>(" apy_proposedpickupdatetime");
}
// String/Single Line Text
if (entity.Attributes.Contains(" apy_externalnotes"))
{
quote[" apy_externalnotes"] = entity.GetAttributeValue<string>(" apy_externalnotes");
}
// Int/Whole Number
if (entity.Attributes.Contains(" apy_noofspots"))
{
quote[" apy_noofspots"] = entity.GetAttributeValue<int>(" apy_noofspots");
}
// Boolean/Two Options
if (entity.Attributes.Contains(" apy_ismultispot"))
{
quote[" apy_ismultispot"] = entity.GetAttributeValue<bool>(" apy_ismultispot");
}
// Option Set
if (entity.Attributes.Contains(" apy_transittime_multiselect"))
{
OptionSetValueCollection locationField = (OptionSetValueCollection)entity.GetAttributeValue<OptionSetValueCollection>(" apy_transittime_multiselect");
quote[" apy_transittime_multiselect"] = new OptionSetValueCollection(locationField);
}
// Option Set
if (entity.Attributes.Contains(" apy_servicetype"))
{
int? st = GetOptionSetValue(" apy_servicetype", entity);
if (st != null)
{
quote[" apy_servicetype"] = new OptionSetValue((int)st);
}
}
//Lookup
if (entity.Contains("customerid"))
{
quote["customerid"] = new EntityReference(entity.GetAttributeValue<EntityReference>("customerid").LogicalName, entity.GetAttributeValue<EntityReference>("customerid").Id);
}
quote[" apy_parentquote"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
service.Create(quote); //create new record
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
public int? GetOptionSetValue(string key, Entity result)
{
int? value = null;
if (result != null && result.Attributes.Contains(key)
&& result.Attributes[key] != null)
{
OptionSetValue os = null;
if (result.Attributes[key] is AliasedValue)
{
os = (OptionSetValue)((AliasedValue)result.Attributes[key]).Value;
}
else
{
os = (OptionSetValue)result[key];
}
if (os != null)
{
value = Convert.ToInt32(os.Value);
}
}
return value;
}
}
}
Comments
Post a Comment