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

JSON.stringfy() not working on Background script

Aruna Sree Yela
Tera Guru

Hi,

 

JSON.stringfy() was working if I directly pass JSON object to a variable. If I try it with glide record that's not working. 

 

For your ref PFA:

 

Scenario 1:

Passing JSON object directly to a variable:

var str = {"u_supply_planner_field_1":[{"original_value":"abc","changed_value":"ABC"}]};
var res = JSON.stringify(str);

gs.print(res);

var parsedData = JSON.parse(res);

gs.print(parsedData.u_supply_planner_field_1[0].original_value);

Output:

ArunaSreeYela_1-1708440982075.png

 

Scenario 2:

 

Gliding Record:

var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
gs.print(caseR.short_description);
var jsonObj = caseR.u_json_output;
gs.print(jsonObj);
var res = JSON.stringify(jsonObj);
gs.print(res);
var parsedData = JSON.parse(jsonObj);
gs.print(parsedData);
gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}

 

Output:

ArunaSreeYela_2-1708441183570.png

 

 

Thanks in advance 🙂

2 ACCEPTED SOLUTIONS

saurabh_dubey
Kilo Sage

Hi @Aruna Sree Yela ,

 

Use this...

 

 

var jsonObj = {}; // declare empty object here...
var res = {} ; // declare your res here
var parsedData = {} // declare your objects here...
var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
gs.print(caseR.short_description);
jsonObj = caseR.u_json_output;
gs.print(jsonObj);
res = JSON.stringify(jsonObj);
gs.print(res);
parsedData = JSON.parse(jsonObj);
gs.print(parsedData);
gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}

 

 

So whats happening here is the 

var jsonObj = caseR.u_json_output;

it is declaring the jsonObj again so try to declare it outside the if // Refer the above code...

 

If my solution helps you do hit the like button and accept the solution.

 

Thanks and Regards,

Saurabh Dubey. 

View solution in original post

palanikumar
Giga Sage
Giga Sage

From the response I understand the data is store in table as stringified version. Also observerved that your string has additional semicolum at the end. So, your code should be below:

var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
    gs.print(caseR.short_description);
    var jsonStr = caseR.u_json_output.toString();
    gs.print(jsonStr);
    //var res = JSON.stringify(jsonObj);
    //gs.print(res);
    if(jsonStr.substring(jsonStr.length-1) == ";"){ // Check if the string has semicolon at the end
        jsonStr = jsonStr.slice(0, -1); // remove the last charactor
    }
    var parsedData = JSON.parse(jsonStr);
    gs.print(parsedData);
    gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}
Thank you,
Palani

View solution in original post

7 REPLIES 7

Thanks a lot @saurabh_dubey . These will be helpful for my learnings.

 

I just have a small query. The code was running fine in BG script but not on the workflow. what may be the reason.

 

BG Script:

 

 

var jsonObj = {}; // declare empty object here...
var res = {} ; // declare your res here
var parsedData = {} // declare your objects here...
var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001050');
caseR.query();
if(caseR.next()){
gs.print(caseR.short_description);
jsonObj = caseR.u_json_output;
gs.print(jsonObj);
res = JSON.stringify(jsonObj);

parsedData = JSON.parse(jsonObj);

gs.print("changed value: " + parsedData.u_supplier_ship_date[0].changed_value);
gs.print("changed value: " + parsedData.u_po_cancel[0].changed_value);
}

 

 

 BG Output:

ArunaSreeYela_0-1708506367961.png

If I try the same code in workflow, its not returning any value.

 

My requirement is If the changed value of u_supplier_ship_date is not empty then the if condition should say yes and then create a task. If its empty it should say no, do nothing...And the same applies for u_po_cancel. But in my workflow both are returning yes.

 

Workflow Run Script:

 

var jsonObj = {}; // declare empty object here...
var res = {}; // declare your res here
var parsedData = {} // declare your objects here...
var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001050');
caseR.query();
if (caseR.next()) {
    gs.print(caseR.short_description);
    jsonObj = caseR.u_json_output;
    gs.print(jsonObj);
    res = JSON.stringify(jsonObj);
    parsedData = JSON.parse(jsonObj);
    workflow.scratchpad.supplier_ship_date = parsedData.u_supplier_ship_date[0].changed_value;
    workflow.scratchpad.po_cancel = parsedData.u_po_cancel[0].changed_value;
}

 

  Workflow IF condition:

 

answer = ifScript();

function ifScript() {

    if (workflow.scratchpad.supplier_ship_date != " ") {

        return 'yes';

    } else {
        return 'no';
    }
}

 

 

@palanikumar Could you also have a look over this please.

 

Thanks:)

Hi,

 

Can you check whether below code works

var jsonObj = {}; // declare empty object here...
var res = {}; // declare your res here
var parsedData = {} // declare your objects here...
var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001050');
caseR.query();
if (caseR.next()) {
    gs.print(caseR.short_description);
    jsonObj = caseR.u_json_output.toString();
    gs.print(jsonObj);
    parsedData = JSON.parse(jsonObj);
    workflow.scratchpad.supplier_ship_date = parsedData.u_supplier_ship_date[0].changed_value;
    workflow.scratchpad.po_cancel = parsedData.u_po_cancel[0].changed_value;
}
Thank you,
Palani

No luck @palanikumar ! 

 

For both the cases If condition was triggering to yes.

This is the JSON I'm passing for 'no' condition

 

{"u_supplier_ship_date":[{"original_value":"HI","changed_value":" "}],
"u_po_cancel":[{"original_value":"hi","changed_value":" hello"}]}

 

Am I correct here?

 

Thanks