Skip to content

Commit

Permalink
Add methods to calculate rollup field
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Finlinson committed Jan 26, 2024
1 parent 101f3dc commit 9b35889
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Extensions/EntityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
Expand Down Expand Up @@ -175,5 +176,25 @@ public static T CoalesceEntityAttributes<T>(this T baseEntity, T entity) where T

return combined.ToEntity<T>();
}

/// <summary>
/// Calculate rollup field for entity
/// </summary>
/// <param name="entity">Entity to calculate</param>
/// <param name="columnName">Column to calculate</param>
/// <param name="service">Organization service</param>
/// <returns>Calculate rollup response</returns> <summary>
public static object CalculateRollup(this Entity entity, string columnName, IOrganizationService service)
{
var request = new CalculateRollupFieldRequest
{
Target = entity.ToEntityReference(),
FieldName = columnName
};

var response = (CalculateRollupFieldResponse)service.Execute(request);

return response.Entity[columnName];
}
}
}
25 changes: 23 additions & 2 deletions Extensions/EntityReferenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
Expand All @@ -21,7 +22,7 @@ public static T ToEntity<T>(this EntityReference reference, ColumnSet columns, I
{
return null;
}

return (T)service.Retrieve(reference.LogicalName, reference.Id, columns);
}

Expand All @@ -41,5 +42,25 @@ public static RetrieveEntityResponse GetTableMetadata(this EntityReference refer

return (RetrieveEntityResponse)service.Execute(request);
}

/// <summary>
/// Calculate rollup field for entity
/// </summary>
/// <param name="entity">Entity to calculate</param>
/// <param name="columnName">Column to calculate</param>
/// <param name="service">Organization service</param>
/// <returns>Calculate rollup response</returns> <summary>
public static object CalculateRollup(this EntityReference entity, string columnName, IOrganizationService service)
{
var request = new CalculateRollupFieldRequest
{
Target = entity,
FieldName = columnName
};

var response = (CalculateRollupFieldResponse)service.Execute(request);

return response.Entity[columnName];
}
}
}

0 comments on commit 9b35889

Please sign in to comment.