PostImage – Plugin Message In MS CRM 2011

Sample Plugin in to calculate the Discount

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using System.Runtime.Serialization;

using System.ServiceModel;

 

namespace Offerdiscountpercentage

{

public class discountpercentage : IPlugin

{

public void Execute(IServiceProvider serviceProvider)

{

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 

Entity entity;

EntityCollection ec = new EntityCollection();

// Obtain the target business entity from the input parameters.

 

if (context.PostEntityImages.Contains(“PostImage”) && context.PostEntityImages[“PostImage”] is Entity)

{

entity = (Entity)context.PostEntityImages[“PostImage”];

}

else

{

return;

}

IOrganizationService service = null;

Guid oppId = Guid.Empty;

try

{

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

service = serviceFactory.CreateOrganizationService(context.UserId);

 

EntityReference oppLookup = (EntityReference)entity.Attributes[“opportunityid”];

oppId = oppLookup.Id;

 

QueryExpression qe = new QueryExpression(“quote”);

ColumnSet set = new ColumnSet(new string[] { “statecode” });

qe.ColumnSet = set;

 

ConditionExpression cond = new ConditionExpression(“statecode”, ConditionOperator.Equal, 1);

ConditionExpression cond2 = new ConditionExpression(“opportunityid”, ConditionOperator.Equal, oppId);

FilterExpression fl = new FilterExpression(LogicalOperator.And);

 

fl.Conditions.Add(cond);

fl.Conditions.Add(cond2);

 

qe.Criteria.AddFilter(fl);

ec = service.RetrieveMultiple(qe);

}

catch (Exception ex)

{

throw ex;

}

 

if (ec.Entities.Count > 1)

{

throw new InvalidPluginExecutionException(“One offer is already activated , you are not allowed to activate another offer”);

}

else

{

int status = ((OptionSetValue)(entity.Attributes[“statecode”])).Value;

if (status == 1)

{

if (entity.Attributes.Contains(“new_discount”) && entity.Attributes.Contains(“new_finalamount”))

{

try

{

Entity ObjOpportunity = new Entity(“opportunity”);

ObjOpportunity.Attributes=new AttributeCollection();

ObjOpportunity.Attributes.Add(“new_discount”,Convert.ToInt32(entity.Attributes[“up_discount”].ToString()));

ObjOpportunity.Attributes.Add(“new_finalamount1”, new Money(((Money)entity.Attributes[“new_finalamount”]).Value));

ObjOpportunity.Id = oppId;

service.Update(ObjOpportunity);

}

catch (Exception ex)

{

throw ex;

}

}

}

}

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s