I need to sum total, of duration filed value in entity BookingrecurecBooking
when user click save button move that total sum value to customize filed Total Duration to Work Order entity
i tried with java script code onSave envent of BookingrecurecBooking entity:
// JavaScript source code
function UpdateWODuration() {
if (Xrm.Page.getAttribute("msdyn_workorder").getValue() != null) {
var WOidValue = Xrm.Page.getAttribute("msdyn_workorder").getValue()[0].id;
var fetchXmlQ = [
"<fetch distinct='false' mapping='logical' aggregate='true'>" +
" <entity name='bookableresourcebooking'>" +
" <attribute name='duration' alias='Duration' aggregate='sum' />" +
" <filter type='and'>" +
" <condition attribute='msdyn_workorder' operator='eq' value='" + WOidValue + "'/>" +
" </filter>" +
" </entity>" +
"</fetch>"
].join("");
var Records = encodeURIComponent(fetchXmlQ);
if (Records.length > 0) {
var entity = {};
entity.new_totalduration = Records[0].attributes['Duration'].value;
var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/msdyn_workorders(" + WOidValue + ")", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Success - No Return Data - Do Something
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
}
}
it retrieve one row have that summation
Check if there is any record in retrieve or not,
throw new InvalidPluginExecutionException(result.Entities.Count)
I've done it already as you see
But the same result
code is not execute too
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
Entity WorkOrderEntity = new Entity("msdyn_workorder");
if (entity.LogicalName == "bookableresourcebooking")
{
if (entity.Contains("msdyn_workorder"))
{
if (entity.Attributes["msdyn_workorder"] != null)
{
Guid WOidValue = ((EntityReference)entity.Attributes["msdyn_workorder"]).Id;
var fetchXml =
"<fetch distinct='false' mapping='logical' aggregate='true'>" +
" <entity name='bookableresourcebooking'>" +
" <attribute name='duration' alias='Duration' aggregate='sum' />" +
" <filter type='and'>" +
" <condition attribute='msdyn_workorder' operator='eq' value='" + WOidValue + "'/>" +
" </filter>" +
" </entity>" +
"</fetch>";
var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
decimal TotalValue = 0;
try
{
foreach (var val in result.Entities)
{
TotalValue += (decimal)((AliasedValue)val["Duration"]).Value;
}
WorkOrderEntity.Id = WOidValue;
WorkOrderEntity.Attributes["new_totalduration"] = TotalValue;
service.Update(WorkOrderEntity);
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.ToString());
}
}
}
}
}
Go for a plugin then.
In fact there is no error
but the code is not executed like it does not exist !!
Are you getting any error?
It will be good if you write plugin to achieve this.
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,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156