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

HI,

I tried something like this:

var abc = '[{"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"}]';
var xyz;
var gr = JSON.parse(abc);


for (var idx = 0; idx <gr.length; idx++) {
gs.print(JSON.stringify(gr[idx]));
}



Got this result for you:

find_real_file.png

Thanks,
Ashutosh

Thank you very much Ashutosh for your replies.

Made it work . Only change is, assignment is done to a variable and then at the end to work_notes. Followed @Ankur Bawiskar  suggestion.

HI,

Glad it worked. Close the thread and mark answer correct.

Thanks,Aashutosh

Thanks for the update. Would you mind marking my response as Correct & 👍

Helpful if I am able to resolve your query? thanks

- Ankur Bawiskar

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

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