We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

how to fetch payload field data from hr case table

Prathamesh Cha1
Tera Contributor

Hi all,

 

I am trying to fetch 'expected target date' and 'Top affected orgknot' in notification but it is coming as undefined can you pls tell me, i tried with "JSON.stringify(obj)" and also without it but still not working please check. 

 

PrathameshCha1_0-1698743080302.png

 

 

var obj = {};
var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
gr.addQuery('sys_id', 'cb4ff94c1b9a31d01eeafc07cb4bcb0c');
gr.query();
while(gr.next())
{
    obj = gr.payload;
    gs.print(gr.payload.expected_target_date)
    gs.print(obj)
    //JSON.stringify(obj);
    gs.print(obj.expected_target_date)

}
 gs.print(gr.payload.expected_target_date)
var payload = {"expected_target_date":"2023-11-14","opened_for":"023894189726a1907ecb7a500153af0b","subject_person":"e55baca5db47e110e37999fbd396193c","top_affected_orgknot":"b619537d1b1d71901eeafc07cb4bcb2b","what_do_you_want_to_change":"fd486951db6f6190e37999fbd3961927"}

gs.print(payload.expected_target_date)


 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron

@Prathamesh Cha1 Update your script as follows.

 

var obj = {};
var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
gr.addQuery('sys_id', 'cb4ff94c1b9a31d01eeafc07cb4bcb0c');
gr.query();
while(gr.next())
{
    obj = JSON.parse(gr.payload); //parse the JSON here.
    gs.print(obj.expected_target_date);   

}
 gs.print(gr.payload.expected_target_date)
var payload = {"expected_target_date":"2023-11-14","opened_for":"023894189726a1907ecb7a500153af0b","subject_person":"e55baca5db47e110e37999fbd396193c","top_affected_orgknot":"b619537d1b1d71901eeafc07cb4bcb2b","what_do_you_want_to_change":"fd486951db6f6190e37999fbd3961927"}

gs.print(payload.expected_target_date);

Hope this helps.

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron

@Prathamesh Cha1 Update your script as follows.

 

var obj = {};
var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
gr.addQuery('sys_id', 'cb4ff94c1b9a31d01eeafc07cb4bcb0c');
gr.query();
while(gr.next())
{
    obj = JSON.parse(gr.payload); //parse the JSON here.
    gs.print(obj.expected_target_date);   

}
 gs.print(gr.payload.expected_target_date)
var payload = {"expected_target_date":"2023-11-14","opened_for":"023894189726a1907ecb7a500153af0b","subject_person":"e55baca5db47e110e37999fbd396193c","top_affected_orgknot":"b619537d1b1d71901eeafc07cb4bcb2b","what_do_you_want_to_change":"fd486951db6f6190e37999fbd3961927"}

gs.print(payload.expected_target_date);

Hope this helps.

Ankur Bawiskar
Tera Patron

@Prathamesh Cha1 

you cannot directly parse the json object by simply using GlideRecord object

you need to store the value in a variable and then parse the json object

this should work for you

var gr = new GlideRecord('sn_hr_core_case_workforce_admin');
gr.addQuery('sys_id', 'cb4ff94c1b9a31d01eeafc07cb4bcb0c');
gr.query();
if (gr.next()) {
    var obj = {};
	obj = gr.payload;
    gs.info(obj.expected_target_date);
    var payload = {
        "expected_target_date": "2023-11-14",
        "opened_for": "023894189726a1907ecb7a500153af0b",
        "subject_person": "e55baca5db47e110e37999fbd396193c",
        "top_affected_orgknot": "b619537d1b1d71901eeafc07cb4bcb2b",
        "what_do_you_want_to_change": "fd486951db6f6190e37999fbd3961927"
    }
    gs.info(payload.expected_target_date);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

 

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader