Hello, I wrote a C# plugin to do the following: for each account in the database, I count each case/incident record related to that account. Then I change the title of each case, adding a number at the end. Example: Account X has 3 related cases, CaseA, CaseB, CaseC. This plugin changes the names of the cases to CaseA-1, CaseB-2, CaseC-3.
It runs post-operation when I update an specific field on the Account entity. I just did this to make the plugin run once. Once the names are changed I won't need to use the plugin anymore so I chose any trigger/field. But something weird is happening. It seems the plugin is firing and in the trace log it starts tracing at the middle of a tracing line in the code, either ignoring what was before or something else I don't quite understand. I'll leave the code and the trace log below.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
namespace CasesAccountCount
{
public class CasesAccountCount : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService organizationService = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new Guid?(((IExecutionContext)context).UserId));
var entity = GetEntityFromContext(context, tracingService);
tracingService.Trace($"Hello");
QueryExpression query = new QueryExpression("account");
query.ColumnSet = new ColumnSet("name");
var completeAccountCollection = RetrieveAllRecords(organizationService, query);
tracingService.Trace($"Number of acounts: {completeAccountCollection.Count()}");
foreach (var a in completeAccountCollection)
{
tracingService.Trace($"1");
var accountid = a.Attributes["accountid"];
tracingService.Trace($"Account ID: {accountid}");
QueryExpression queryExpression = new QueryExpression("incident");
queryExpression.ColumnSet.AddColumns("customerid", "ticketnumber");
queryExpression.Criteria = new FilterExpression();
queryExpression.Criteria.AddCondition("customerid", ConditionOperator.Equal, accountid);
queryExpression.PageInfo.ReturnTotalRecordCount = true;
tracingService.Trace($"Querying account related incidents");
EntityCollection entityCollection = organizationService.RetrieveMultiple((QueryBase)queryExpression);
tracingService.Trace(string.Format($"Found {entityCollection.TotalRecordCount} incidents"));
var count = 0;
foreach (Entity e in (Collection<Entity>)entityCollection.Entities)
{
count++;
e.Attributes["ticketnumber"] = e.Attributes["ticketnumber"] + "-" + count.ToString();
organizationService.Update(e);
}
}
List<Entity> RetrieveAllRecords(IOrganizationService service, QueryExpression q)
{
var pageNumber = 1;
var pagingCookie = string.Empty;
var result = new List<Entity>();
EntityCollection resp;
do
{
if (pageNumber != 1)
{
q.PageInfo.PageNumber = pageNumber;
q.PageInfo.PagingCookie = pagingCookie;
}
resp = service.RetrieveMultiple(q);
if (resp.MoreRecords)
{
pageNumber++;
pagingCookie = resp.PagingCookie;
}
//Add the result from RetrieveMultiple to the List to be returned.
result.AddRange(resp.Entities);
}
while (resp.MoreRecords);
return result;
}
}
private Entity GetEntityFromContext(IPluginExecutionContext localContext, ITracingService tracingService)
{
var target = localContext.InputParameters["Target"] as Entity;
var entity = new Entity(target.LogicalName);
if (localContext.PreEntityImages.ContainsKey("PreImage"))
{
var preImage = localContext.PreEntityImages["PreImage"];
foreach (var preImageAttribute in preImage.Attributes)
{
tracingService.Trace($"PreImage attribute found: Key: {preImageAttribute.Key}, Value: {preImageAttribute.Value?.ToString()}");
entity[preImageAttribute.Key] = target.Contains(preImageAttribute.Key) ? target[preImageAttribute.Key] : preImageAttribute.Value;
}
}
foreach (var targetAttribute in target.Attributes)
{
tracingService.Trace($"Target attribute found: Key: {targetAttribute.Key}, Value: {targetAttribute.Value?.ToString()}");
entity[targetAttribute.Key] = targetAttribute.Value;
}
return entity;
}
}
}
Trace Log in D365:
9a247-d6bf-eb11-bacc-000d3ac183ef //it seems it starts at the middle of the "Account ID" trace log, why?
Querying account related incidents
Found 0 incidents
1
Account ID: 8257d17e-28bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 39f4e8ae-aebf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 1d34b319-b0bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 2df3ab87-b1bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 6f857462-b2bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 6ca4d5e0-b3bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 4e9530b0-e2bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: ac2b99c5-e9bf-eb11-bacc-000d3ac18904
Querying account related incidents
Found 0 incidents
1
Account ID: 977ce613-4e79-ec11-8d21-000d3ac1f832
Querying account related incidents
Found 0 incidents
1
Account ID: 14ed2f05-8c30-e911-867a-00155d001ae4
Querying account related incidents
Found 1 incidents
1
Account ID: e77b1b5c-c041-e911-867a-00155d00247b
Querying account related incidents
Found 0 incidents
1
Account ID: 57f87c81-ca44-e911-867a-00155d00247b
Querying account related incidents
Found 0 incidents
1
Account ID: f64886c9-b245-e911-867a-00155d00247b
Querying account related incidents
Found 0 incidents
1
Account ID: d0b1ab7e-b62f-e911-9461-00155d003b21
Querying account related incidents
Found 0 incidents
1
Account ID: a89cd8ba-b62f-e911-9461-00155d003b21
Querying account related incidents
Found 0 incidents
1
Account ID: 57027927-b72f-e911-9461-00155d003b21
Querying account related incidents
Found 0 incidents
1
Account ID: 52a808be-bd2f-e911-9461-00155d003b21
Querying account related incidents
Found 0 incidents
1
Account ID: 6682fb7f-be2f-e911-9461-00155d003b21
Querying account related incidents
Found 0 incidents
1
Account ID: 09ee4f01-7b46-e911-867a-00155d003b39
Querying account related incidents
Found 1 incidents
1
Account ID: 33163905-8346-e911-867a-00155d003b39
Querying account related incidents
Found 0 incidents
1
Account ID: 627eadba-8346-e911-867a-00155d003b39
Querying account related incidents
Found 0 incidents
1
Account ID: e6d0ec09-8449-e911-867a-00155d003b39
Querying account related incidents
Found 1 incidents
1
Account ID: b425e17e-8e50-e911-867a-00155d003b39
Querying account related incidents
Found 0 incidents
1
Account ID: c49d0da3-b550-e911-867a-00155d003b39
Querying account related incidents
Found 0 incidents
1
Account ID: b4450b76-cd50-e911-867a-00155d003b39
Querying account related incidents
Found 0 incidents
1
Account ID: 8745b48b-cf50-e911-867a-00155d003b39
Querying account related incidents
Found 0 incidents
1
Account ID: 936b419f-9f4b-e111-bb8d-00155d03a715
Querying account related incidents
Found 2 incidents
1
Account ID: 404a34ec-d112-eb11-8441-00155d42a45f
Querying account related incidents
Found 0 incidents
1
Account ID: 9bb66d59-5359-ea11-b698-00155d42b7da
Querying account related incidents
Found 0 incidents
1
Account ID: d2f667d2-5a59-ea11-b698-00155d42b7da
Querying account related incidents
Found 0 incidents
1
Account ID: cf3b4ef8-3307-eb11-b5d9-00155d42bba6
Querying account related incidents
Found 0 incidents
1
Account ID: d3eec4d8-2acc-ea11-bf21-00155d72f14f
Querying account related incidents
Found 0 incidents
1
Account ID: 325a50df-2bcc-ea11-bf21-00155d72f14f
Querying account related incidents
Found 0 incidents
1
Account ID: 5dd60261-2ccc-ea11-bf21-00155d72f14f
Querying account related incidents
Found 0 incidents
1
Account ID: d4647875-e8cc-ea11-bf21-00155d72f14f
Querying account related incidents
Found 0 incidents
1
Account ID: fdc93611-49c5-ea11-bf21-00155df68220
Querying account related incidents
Found 0 incidents
1
Account ID: 096d756b-92c6-ea11-bf21-00155df68220
Querying account related incidents
Found 0 incidents
1
Account ID: e9c17afc-e6c1-ea11-bf21-00155df68235
Querying account related incidents
Found 0 incidents
1
Account ID: 9ca4dc0a-e9c1-ea11-bf21-00155df68235
Querying account related incidents
Found 0 incidents
1
Account ID: 659ff9ab-18d0-ea11-bf21-00155df68de8
Querying account related incidents
Found 0 incidents
1
Account ID: 1ebae8cc-fdd0-ea11-bf21-00155df68de8
Querying account related incidents
Found 0 incidents
1
Account ID: e3da12a6-fed0-ea11-bf21-00155df68de8
Querying account related incidents
Found 0 incidents
1
Account ID: 27155757-00d1-ea11-bf21-00155df68de8
Querying account related incidents
Found 0 incidents
1
Account ID: 62a70ac0-e63e-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: 2187784e-5e44-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: faa418e0-4749-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: cc339c1d-004a-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: 04ba4a1b-b84a-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: 394909d0-654f-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 1 incidents
1
Account ID: 353562b9-1f50-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: 6f3aae52-2350-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: e048885f-2e50-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: ccd17253-c559-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: f378dc32-1d5c-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: 43b2e1d6-9e61-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: e94255ce-a061-eb11-8fed-00155df6f9ef
Querying account related incidents
Found 0 incidents
1
Account ID: 7c36f384-5ca0-ea11-8b71-00155df6ffe1
Querying account related incidents
Found 0 incidents
1
Account ID: 1ab350b1-1554-ea11-b699-00155df70f51
Querying account related incidents
Found 1 incidents
1
Account ID: aae009bd-5886-eb11-8ced-00155df73456
Querying account related incidents
Found 0 incidents
1
Account ID: 67aa1742-3dee-eb11-b76b-00155df7e2ad
Querying account related incidents
Found 0 incidents
1
Account ID: f1f8ea73-43cd-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 62b11d04-46cd-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 74f05acb-05ce-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 4a12e3dc-06ce-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 55c6f91f-18ce-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: b5e425e0-e8ce-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 0f35c112-7dcf-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: b8127925-65d0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 2d339e98-78d0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 8718c9fb-7dd0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 67346e67-7ed0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 98fc4763-7fd0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 6544e1c2-7fd0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 073b6820-80d0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 370badb0-84d0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: b502f20e-86d0-eb11-bacc-002248364db5
Querying account related incidents
Found 0 incidents
1
Account ID: 858cabb0-4ad4-eb11-bacc-00224836e449
Querying account related incidents
Found 0 incidents
1
Account ID: 082d0a1d-83d4-eb11-bacc-00224836e449
Querying account related incidents
Found 0 incidents
1
Account ID: 66d4a04d-84d4-eb11-bacc-00224836e449
Querying account related incidents
Found 0 incidents
1
Account ID: 4c5553cc-1ed5-eb11-bacc-00224836e449
Querying account related incidents
Found 1 incidents
1
Account ID: 47ffc67b-4bd4-eb11-bacc-00224836e983
Querying account related incidents
Found 0 incidents
1
Account ID: 40d7b273-5300-ec11-94f0-00224836ffb5
Querying account related incidents
Found 0 incidents
1
Account ID: 7e3568ca-5600-ec11-94f0-00224836ffb5
Querying account related incidents
Found 0 incidents
1
Account ID: aa0d7d06-1f01-ec11-94f0-00224836ffb5
Querying account related incidents
Found 1 incidents
1
Account ID: 974bfe7d-2cdb-eb11-bacb-002248376d0e
Querying account related incidents
Found 0 incidents
1
Account ID: 976e1cd3-f026-ec11-b6e6-0022483774bb
Querying account related incidents
Found 0 incidents
1
Account ID: 63e34190-df3d-ec11-8c62-00224837ccc6
Querying account related incidents
Found 0 incidents
1
Account ID: b8a8e175-3037-ec11-8c64-00224837d93f
Querying account related incidents
Found 0 incidents
1
Account ID: 520f18bc-0f68-ec11-8943-00224837da53
Querying account related incidents
Found 0 incidents
1
Account ID: 3413f47f-239b-ec11-b401-002248d30c9e
Querying account related incidents
Found 0 incidents
1
Account ID: 113207eb-2c9b-ec11-b401-002248d30c9e
Querying account related incidents
Found 0 incidents
1
Account ID: 96509d44-ba9b-ec11-b401-002248d30c9e
Querying account related incidents
Found 0 incidents
1
Account ID: 6d665bfd-f69a-ec11-b401-002248d30d55
Querying account related incidents
Found 0 incidents
1
Account ID: b1c60210-3cd0-4748-8066-1c8605eb8739
Querying account related incidents
Found 0 incidents
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156