The CreatorCon Call for Content is officially open! Get started here.

How can convert date format from YYYY-MM-DD to MM/DD/YYYY in Flow Designer Action

chandan15
Tera Contributor

Hi,

Below script I am using in flow designer Action to create JSON payload .Getting data from Time_card and creating a record, and again taking those info & converting into JSON format. But in servicenow it is in yyyy-mm-dd format, how ever the integration with which tool, that is supporting  mm/dd/yyyy format.

In the line 53, I have used the script to convert but, I'm getting error, the same script I have tried in BR, it is working.

Can anyone please help me on this to detect my error !!

 

(function execute(inputs, outputs) {
    var reqJson = {};
    var tcq = inputs.tcq;
    var obj = {};
    //var gr = tcq;
	
    var gr = new GlideRecord('time_card'); // Getting info from this table
    gr.addEncodedQuery("user.u_fieldglass_enabled_user=true");
    gr.query();

    while (gr.next()) {
        if (!obj[gr.user + '']) {
            obj[gr.user + ''] = {};
        }
        if (!obj[gr.user + ''][gr.week_starts_on + '']) {
            obj[gr.user + ''][gr.week_starts_on + ''] = {};
        }
        var arr = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
        for (i = 0; i < arr.length; i++) {
            var temp = Number(obj[gr.user + ''][gr.week_starts_on + ''][arr[i]]);
            obj[gr.user + ''][gr.week_starts_on + ''][arr[i]] = temp ? temp + Number(gr[arr[i]]) : Number(gr[arr[i]]);
        }
    }
    var data_array = [];
    for (var x in obj) {
        for (var y in obj[x]) {
            var rolgr = new GlideRecord('u_fieldglass_time_card_rollup');//creating record in thins field
            rolgr.initialize();
            rolgr['u_week_start_date'] = y; //
            rolgr['u_user'] = x;
            rolgr['u_sunday'] = obj[x][y]['sunday'];
            rolgr['u_saturday'] = obj[x][y]['saturday'];
            var sys_id = rolgr.insert();
            data_array.push(sys_id.toString());
        }
    }
    if (rolgr.length > 0) {
        throw new Error("Missing mandatory fields" + " " + rolgr);
    }
    gs.log("SAP array" + data_array.toString());

    var optarr = [];
    var gr1 = new GlideRecord("u_fieldglass_time_card_rollup");//getting record and converting into JSON payload
    gr1.addQuery("sys_id", "IN", data_array.toString());
    gr1.query();
    while (gr1.next()) {
        var obj1 = {};
        obj1.user = gr1.u_user.getDisplayValue().toString();
        obj1.Sat_Hrs = gr1.u_saturday.toString();
        obj1.Sun_Hrs = gr1.u_sunday.toString();

        var dt = gr1.u_week_start_date; //Added to convert date format
        var gd = new GlideDate(); //Added to convert date format
        gd.setValue(dt); //Added to convert date format
        var gc = gd.getByFormat("MM/dd/yyyy"); //Added to convert date format

        obj1.Week_Start_Date = gc; //Date should show by following above script

        optarr.push(obj1);
    }
    gs.log("SAP__" + optarr.length);
    gs.log("SAP__" + JSON.stringify(optarr));
    gs.log("SAP__" + (optarr.toString()));

    var optc = JSON.stringify(optarr);
    outputs.optc = optc;

})(inputs, outputs);
1 ACCEPTED SOLUTION

Replace the below line...It works..

obj1.Week_Start_Date = gd.getByFormat("MM/dd/YYYY");

 

View solution in original post

5 REPLIES 5

Abhijit4
Mega Sage

Hi Chandan,

I don't see any problem here. I would suggest to run this script in background script(with some modification if required) and check if 'gc' variable is getting any value or not.

I am suspecting below line is assigning empty value to dt variable which causing dc to be empty.

var dt = gr1.u_week_start_date;

Here is the tested example for one of the incident date field which is working fine: 

find_real_file.png

Output :

find_real_file.png

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP