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

Parsing JSON array of Objects response

Ram117
Kilo Sage

Hi Experts,

I am trying to parse the below JSON response and am having difficulty in updating into the work_notes field of the incident record,

[{
        "click_uri": "https://htqa-dreamevent.cs49.force.com/Help_DocContent?id=sf.wave_overview&release=222.5.0&language=th_th",
        "docs_abstract": "Get access to more advanced analytics dashboards built on Einstein Analytics. ... Salesforce admins can evaluate the effectiveness of Einstein features, such as Einstein Lead Scoring and Einstein A...",
        "rank": "72.23634",
        "title": "Einstein Analytics for Sales Cloud Einstein"
    }, {
        "click_uri": "https://htqa-dreamevent.cs49.force.com/Help_DocContent?id=sf.bi_app_sales_analytics_dashboard_forecast&release=222.5.0&language=en_us",
        "docs_abstract": "Sales Analytics Forecast Dashboard ... View quota attainment and forecast for each member of the team, and fine-tune the forecast by reviewing opportunities by stage in a selected period. ... Defau...",
        "rank": "70.937096",
        "title": "Sales Analytics Forecast Dashboard"
    }, {
        "click_uri": "https://htqa-dreamevent.cs49.force.com/Help_DocContent?id=sf.bi_app_sales_wave&release=222.5.0&language=en_us",
        "docs_abstract": "The Sales Analytics app brings the power of Analytics to Sales Cloud on any device that supports Analytics. ... With intuitive visualizations based on your Salesforce data, Sales Analytics lets you...",
        "rank": "70.50628",
        "title": "Sales Analytics App"
    }, {
        "click_uri": "https://htqa-dreamevent.cs49.force.com/Help_DocContent?id=sf.bi_app_sales_analytics_dashboard_home&release=222.5.0&language=en_us",
        "docs_abstract": "Default Behavior and Recommended Options ... Get an overview of your sales business for your current fiscal year, including comparisons to the previous year and the team’s progress against goals.",
        "rank": "70.35449",
        "title": "Sales Analytics Home Dashboard"
    }
]

Below are the lines of code after retrieving the JSON response to loop the response Body.

            if (_status == 200) {

                gs.addInfoMessage('ResponseBody length is ' + responseBody.length); // array of Objects is responseBody
                var _lng = responseBody.length;
               
                for (var idx = 0; idx < _lng; idx++) {

                    for (var key in responseBody[idx]) {

                        current.work_notes += key + " : " + responseBody[idx][key];

                    }

                    current.work_notes += "\n";

                }

                current.update();

 

Incident record work notes is getting updated not in the right format. 

 

find_real_file.png

 

Need some guidance.

Thx

ram.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ram,

So all the details from json should be populated with new line character in between the values?

please use below script; set the work_notes at the end and test once

if (_status == 200) {

                gs.addInfoMessage('ResponseBody length is ' + responseBody.length); // array of Objects is responseBody
                var _lng = responseBody.length;
                var val = '';
                for (var idx = 0; idx < _lng; idx++) {

                    for (var key in responseBody[idx]) {

                        val += key + " : " + responseBody[idx][key];

                    }

                    val += "\n";

                }
current.work_notes = val;
current.update();

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

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

View solution in original post

16 REPLIES 16

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

What you do this:

if (_status == 200) {

gs.addInfoMessage('ResponseBody length is ' + responseBody.length); // array of Objects is responseBody
var _lng = responseBody.length;

for (var idx = 0; idx < _lng; idx++) {

for (var key in responseBody[idx]) {

current.work_notes += key + " : " + responseBody[idx][key]+"\n";

}
}

current.update();


Thanks,
Ashutosh

Thank you for the reply Ashutosh.

I had tried this and by assigning it to work_notes, the update on the incident is coming exactly the way in my screenshot.

Hi,

How you want it can you show?

Thanks,
Ashutosh

Below is the format.