Hi all I've had another play and i will admit i am new to all of this. From what i understand some code like below is what i'm trying to do. Ive changed what i think i needed to change namewise and added the steps as detailed in the orgianl code discussion Postcreate, Postupdate buti still can't seem to figure it out could one of you nice people point me in the right direction.
My fields and entitys are as follows
Parent Ent -quotes
Child - quotedetailsgrid
Column with in gris is - new_Itemweight
Field on parent for where i want the data is - new_totalweight
This is the code i'm trying to use
public class SumWeight : 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);
if (Context.PostEntityImages.Contains("PostImage") && Context.PostEntityImages["PostImage"] is Entity)
{
Entity Weight = (Entity)Context.InputParameters["Target"];
var WeightTot = (EntityReference)Weight.Attributes["quotedetailsGrid"];
decimal Total = FetchResult(WeightTot.Id, Service);
// Updating Parent Entity
Entity Quote = new Entity("quote");
Quote.Id = Weight.Id;
Quote["TestWeight"] = 10;
Service.Update(Quote);
}
}
private static decimal FetchResult(Guid quantity, IOrganizationService service)
{
string value_sum = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
<entity name='quotedetailsGrid'>
<attribute name='ItemWeight' alias='ItemWeight_sum' aggregate='sum'/>
<filter type='and'>
<condition attribute='ItemWeight' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>";
decimal TotalValue = 0;
value_sum = string.Format(value_sum, quantity);
EntityCollection value_sum_result = (EntityCollection)service.RetrieveMultiple(new FetchExpression(value_sum));
foreach (var c in value_sum_result.Entities)
{
decimal aggregate2 = ((int)((AliasedValue)c.Attributes["ItemWeight_sum"]).Value);
TotalValue = aggregate2;
}
return TotalValue;
}
}
}
may thanks for your help
Cheers
Dan