Skip to main content

Notifications

Dynamics 365 Community / Forums / Field Service forum / Sum Total Duration Fil...
Field Service forum

Sum Total Duration Filed and Pass it sum to Work Order Entity ?

Posted on by Microsoft Employee

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));


}

}
}

Categories:
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Sum Total Duration Filed and Pass it sum to Work Order Entity ?

    it retrieve  one row have that summation

  • Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Sum Total Duration Filed and Pass it sum to Work Order Entity ?

    Check if there is any record in retrieve or not,

    throw new InvalidPluginExecutionException(result.Entities.Count)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Sum Total Duration Filed and Pass it sum to Work Order Entity ?

    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());

                               }

                           }

                       }

                   }

               }

  • Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Sum Total Duration Filed and Pass it sum to Work Order Entity ?

    Go for a plugin then.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Sum Total Duration Filed and Pass it sum to Work Order Entity ?

    In fact there is no error

    but the code is not executed like it does not exist !!

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Sum Total Duration Filed and Pass it sum to Work Order Entity ?

    Are you getting any error?

    It will be good if you write plugin to achieve this.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans