Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Transform Data Broker Server Script for Timeline

Annette Kitzmil
Tera Guru

Hello,

So I could use some assistance with my script for a timeline component I am adding in a scoped app.  Here are the details:

  1. Create a timeline that has 5 dates that represent the various time of an event. 
    • The individual records all have these dates, so it is a matter of retrieving them.  I have created a Lookup Data Resource and set up return fields for the 5 different dates and set the condition for the sys id of the record that is being opened.
  2. Create a Transform Data Resource which is set up as follows:
    • Properties have been set up for all 5 dates which then brings them into the transform data resource so they can all be bound to the results of the lookup data resource that contains the individual dates.
    • Look Up Data Resource Results screen shot.
    • AnnetteKitzmil_0-1764689138622.png

       

    • Screenshot showing the Transform Data Resource that binds the above look up data resource output to the properties that have been defined:
    • AnnetteKitzmil_1-1764689272484.png

       

    • These are the properties that have been defined in the TDR:
      • [{

        "name": "accountOpenDate",
        "label": "Account Open Date",
        "mandatory": true,
        "defaultValue": "John Doe",
        "fieldType": "String"
        },
        {
        "name": "firstReviewDate",
        "label": "First Review Date",
        "mandatory": true,
        "defaultValue": "John Doe",
        "fieldType": "String"
        },
        {
        "name": "warningLetter",
        "label": "30-Day Letter",
        "mandatory": true,
        "defaultValue": "John Doe",
        "fieldType": "String"
        },
        {
        "name": "accountClosureDate",
        "label": "Account Closure Date",
        "mandatory": true,
        "defaultValue": "John Doe",
        "fieldType": "String"
        },
        {
        "name": "closedAt",
        "label": "Case Closed",
        "mandatory": true,
        "defaultValue": "John Doe",
        "fieldType": "String"
        }

        ]

    • As per the screen shot above, the problem currently is that the script is returning an output with the timelineData object, but nothing further.  Here is the latest script (older one and details below), can you tell me what is missing here that needs to be updated so I am getting back the array of dates as expected?
    • function transformSipTimeline(input) {
          var records = input || [];    
          var returnArray = [];

          for (var i in dateFields) {
              var myObj = {
                  name: "Timeline",
                  icon: "starFill",
                  label: "Dates for Record " + (records[i].number || ""),
                  events: []
              };

              var dateFields = [{
                      field: "account_open_date",
                      icon: "starFill",
                      description: "Account Open Date (Date Account Opened)"
                  },
                  {
                      field: "first_review_date",
                      icon: "eye-fill",
                      description: "First Review Date"
                  },
                  {
                      field: "warning_letter",
                      icon: "triangle-exclamation-fill",
                      description: "30 Day Letter"
                  },
                  {
                      field: "account_closure_date",
                      icon: "close-fill",
                      description: "Account Closure Date"
                  },
                  {
                      field: "closed_at",
                      icon: "circle-close_fill",
                      description: "Case Closure"
                  }
              ];

             
               for (var j in dateFields) {
                  var fieldName = dateFields[j].field;
                  var fieldValue = records[i][fieldName];
                  if (fieldValue) {
                      myObj.events.push({
                          label: dateFields[j].description,
                          date: fieldValue // Use as-is
                      });
                  }
              }

              returnArray.push(myObj);
          }

          return {

              timelineData: returnArray

          };
      }
  3. With the script that is shown below, I see the output array timelineData with five elements which makes sense based on the array for the dateFields. The problem is that the 5 elements aren't the dateFields array, instead it is the object repeated for each element. What needs to be done so the dateFields are returned instead? 
function transformSipTimeline(input) {
var records = input || [];
var returnArray = [];

for (var i in records) {
    var myObj = {
        name: "Timeline",
        icon: "starFill",
        label: "Dates for Record " + (records[i].number || ""),
        events: []
    };

    var dateFields = [
        { field: "account_open_date", description: "Account Open Date (Date Account Opened)" },
        { field: "first_review_date", description: "First Review Date" },
        { field: "warning_letter", description: "30 Day Letter" },
        { field: "account_closure_date", description: "Account Closure Date" },
        { field: "closed_at", description: "Case Closure" }
    ];

    for (var j in dateFields) {
        var fieldName = dateFields[j].field;
        var fieldValue = records[i][fieldName];
        if (fieldValue) {
            myObj.events.push({
                label: dateFields[j].description,
                date: fieldValue // Use as-is
            });
        }
    }

    returnArray.push(myObj);
}

return {
    timelineData: returnArray
};
}
 
  •  Screenshot showing the transform data resource output with the script above.
  • AnnetteKitzmil_2-1764689916254.png

     

Any help resolving the issue that will allow me to set this up for the various dates to come in as an output that can be used with the timeline would be greatly appreciated.

 

Thanks,

 

0 REPLIES 0