none
Can't update totalcampaignactivityactualcost in Campaign Entity, Dynamics CRM RRS feed

  • Question

  • Hello, i can't update totalcampaignactivityactualcost and actualcost of entity campaign, i used fetch to calculate SUM of campaignactivity

    using project.plugin; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace project.plugins { public class TotalActivityCostPlug : 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)); Entity preImageEntity = new Entity(); Guid CampagneID = Guid.Empty; //The InputParameters collection contains all the data passed in the message request. try { 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"]; if (entity.LogicalName != "campaignactivity") return; if (entity.Attributes.Contains("regardingobjectid")) { CampagneID = ((EntityReference)entity.Attributes["regardingobjectid"]).Id; } } } if (messageName == "UPDATE") { if (context.PreEntityImages.Contains("Image") && context.PreEntityImages["Image"] is Entity) { preImageEntity = context.PreEntityImages["Image"]; if (preImageEntity.Attributes.Contains("regardingobjectid")) { CampagneID = preImageEntity.GetAttributeValue<EntityReference>("regardingobjectid").Id; } } } if (messageName == "DELETE") { if (context.PreEntityImages.Contains("Image") && context.PreEntityImages["Image"] is Entity) { preImageEntity = context.PreEntityImages["Image"]; if (preImageEntity.Attributes.Contains("regardingobjectid")) { CampagneID = preImageEntity.GetAttributeValue<EntityReference>("regardingobjectid").Id; } } } tracingService.Trace(messageName); } decimal TotalActicityCost = 0; TotalActicityCost = FetchResult(CampagneID, service); // Updating Parent Entity Entity campagneobj = new Entity("campaign"); campagneobj.Id = CampagneID; campagneobj.Attributes.Add("totalcampaignactivityactualcost", new Money(TotalActicityCost)); service.Update(campagneobj); //} } catch (Exception ex) { tracingService.Trace(ex.Message); } } private static decimal FetchResult(Guid CampagneID, IOrganizationService service) { string FetchActivityCampagne = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'> <entity name='campaignactivity'> <attribute name='actualcost' alias='totalservicecostamount_sum' aggregate='sum' /> <link-entity name='campaign' from='campaignid' to='regardingobjectid' alias='aa'> <filter type='and'> <condition attribute='campaignid' operator='eq' value='{0}' /> </filter> </link-entity> </entity> </fetch>"; decimal TotalValue = 0; FetchActivityCampagne = string.Format(FetchActivityCampagne, CampagneID); EntityCollection result = (EntityCollection)service.RetrieveMultiple(new FetchExpression(FetchActivityCampagne)); if (result.Entities.Count > 0) { foreach (var c in result.Entities) { decimal aggregate2 = ((Money)((AliasedValue)c.Attributes["totalservicecostamount_sum"]).Value).Value; TotalValue = aggregate2; } } return TotalValue; } } }





    Wednesday, December 18, 2019 10:57 AM

Answers

  • Hi hassen MEdden,

    Sorry you have posted in a wrong forum, this forum exclusively for MS EPM solutions like (Project server, Project Online & MS Project etc..). 

    Please re-post in a Dynamics CRM forum, someone will help you.


    Rajkumar Allepu (MCP) , | https://sharepointprojectserver.com ** Please mark it as answer if my answer would resolved your issue.

    Thursday, December 19, 2019 6:04 AM