Plugin Trigger
· Create or
Update of Quote Product record
Plugin logic
When a
Quote Product record is created or updated, override the default price with the
following formula.
Formula: IF(Discount is null, ProductPrice, ProductPrice –
(ProductPrice*(Discount/100)))
ProductPrice
= PriceListItem.Amount
Discount
= Account.sts_discount
Code:
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm;
using Microsoft.Xrm.Sdk.Query;
namespace Car_O_Liner_QuoteDiscount
{
public class QuoteDiscount : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
#region Declare Variables
Entity preImageEntity = new Entity();
Guid QuoteID = Guid.Empty;
Guid AccountID = Guid.Empty;
Guid PricelistObjID = Guid.Empty;
decimal PerDiscount = 0;
Guid ProductID = Guid.Empty;
decimal ProductAmt = 0;
Boolean ispriceoverridden;
//decimal priceperunit = 0;
Guid QuoteProductID = Guid.Empty;
Money sumafterDiscount = new Money();
#endregion
try
{
// Create of New Quote Product
if (context.Depth == 1)
{
string messageName = context.MessageName.ToUpper();
if (messageName == "CREATE")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
QuoteProductID = entity.Id;
if (entity.LogicalName != "quotedetail")
return;
if (entity.Attributes.Contains("quoteid"))
{
QuoteID = ((EntityReference)entity.Attributes["quoteid"]).Id;
ProductID = ((EntityReference)entity.Attributes["productid"]).Id;
ispriceoverridden = (Boolean)entity.Attributes["ispriceoverridden"];
}
}
}
// Update of Quote Detail
if (messageName == "UPDATE")
{
if (context.PreEntityImages.Contains("Image") && context.PreEntityImages["Image"] is Entity)
{
preImageEntity = context.PreEntityImages["Image"];
QuoteProductID = preImageEntity.Id;
if (preImageEntity.Attributes.Contains("quoteid"))
{
QuoteID = preImageEntity.GetAttributeValue<EntityReference>("quoteid").Id;
ProductID = ((EntityReference)preImageEntity.Attributes["productid"]).Id;
}
}
}
}
// Get Potential Customer & Price List ID
QueryExpression query = new QueryExpression
{
EntityName = "quote",
ColumnSet = new ColumnSet("customerid", "pricelevelid")
};
query.Criteria.AddCondition("quoteid", ConditionOperator.Equal, QuoteID);
EntityCollection result = service.RetrieveMultiple(query);
if (result.Entities.Count > 0)
{
foreach (Entity ent in result.Entities)
{
if (ent.Attributes.Contains("customerid"))
{
// get a reference to the customer
EntityReference customerId = ent.GetAttributeValue<EntityReference>("customerid");
AccountID = customerId.Id;
}
if (ent.Attributes.Contains("pricelevelid"))
{
//get a ref to the price list
EntityReference PriceListId = ent.GetAttributeValue<EntityReference>("pricelevelid");
PricelistObjID = PriceListId.Id;
}
}
}
// Fetch Xml to Get % Discount form account
string FetchAccount = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='sts_discount' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='accountid' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>";
FetchAccount = string.Format(FetchAccount, AccountID);
EntityCollection FetchAccountResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(FetchAccount));
if (FetchAccountResult.Entities.Count > 0)
{
foreach (Entity ent in FetchAccountResult.Entities)
{
if (ent.Attributes.Contains("sts_discount"))
{
PerDiscount = (decimal)ent.Attributes["sts_discount"];
}
}
}
else
{
// FetchXml to Get % Discount form Contact-account if User select contact in potential customer lookup
string FetchContactAccount = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='account'>
<attribute name='name' />
<attribute name='sts_discount' />
<order attribute='name' descending='false' />
<link-entity name='contact' from='parentcustomerid' to='accountid' alias='ah'>
<filter type='and'>
<condition attribute='contactid' operator='eq' value='{0}' />
</filter>
</link-entity>
</entity>
</fetch>";
FetchContactAccount = string.Format(FetchContactAccount, AccountID);
EntityCollection FetchContactAccountResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(FetchContactAccount));
if (FetchContactAccountResult.Entities.Count > 0)
{
foreach (Entity ent in FetchContactAccountResult.Entities)
{
if (ent.Attributes.Contains("sts_discount"))
{
PerDiscount = (decimal)ent.Attributes["sts_discount"];
}
else
{
PerDiscount = 0;
}
}
}
}
// Get Amt From Price List Item
string FetchPriceListAmt = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='pricelevel'>
<attribute name='name' />
<attribute name='pricelevelid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='pricelevelid' operator='eq' value='{0}' />
</filter>
<link-entity name='productpricelevel' from='pricelevelid' to='pricelevelid' alias='as'>
<attribute name='amount' alias='ProductAmt' />
<filter type='and'>
<condition attribute='productid' operator='eq' value='{1}' />
</filter>
</link-entity>
</entity>
</fetch>";
FetchPriceListAmt = string.Format(FetchPriceListAmt, PricelistObjID, ProductID);
EntityCollection FetchPriceListAmtResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(FetchPriceListAmt));
if (FetchPriceListAmtResult.Entities.Count > 0)
{
foreach (Entity ent in FetchPriceListAmtResult.Entities)
{
if (ent.Attributes.Contains("ProductAmt"))
{
ProductAmt = ((Money)((AliasedValue)ent.Attributes["ProductAmt"]).Value).Value;
}
}
}
if (PerDiscount == 0)
{
sumafterDiscount = new Money(ProductAmt);
}
else
{
sumafterDiscount = new Money(ProductAmt - ((ProductAmt) * (PerDiscount / 100)));
}
// Update the Quote Prodcuct
Boolean IsUpdated = false;
Entity quotedetailObj = new Entity("quotedetail");
quotedetailObj.Attributes.Add("quotedetailid", QuoteProductID);
quotedetailObj.Attributes.Add("ispriceoverridden", true);
quotedetailObj.Attributes.Add("priceperunit", sumafterDiscount);
if (!IsUpdated)
{
IsUpdated = true;
}
if (IsUpdated)
{
try
{
service.Update(quotedetailObj);
}
catch (Exception ex)
{
tracingService.Trace("Exception generated :" + ex.Message);
}
}
}
catch (Exception ex)
{
tracingService.Trace(ex.Message);
}
}
}
}
No comments:
Post a Comment