Why line break not coming inside a string when I am pushing?

Manikantahere
Tera Contributor

I am processing a third-party response according to the requirements. However, as shown below, I am sending the required parameters to the array as a string, but I want each parameter printed on a separate line.

 

But its not happening instead I am getting as shown in second pic. why?
comments-1.pngcomments-2.png

2 ACCEPTED SOLUTIONS

Abhijit4
Mega Sage

Hi @Manikantahere 

 

Do not use JSON.stringify at the end, it will make \n as character rather than line break.

 

So, try with below code, it will work:

 

var currentdate = new GlideDate();
    var request = new sn_ws.RESTMessageV2("Power Automate Dataverse API", "Get Approval Responses");
    request.setStringParameter("envName", gs.getProperty("power_automate.environmentName"));
    request.setStringParameter("AppName", gs.getProperty("power_automate.applicationName"));
    request.setStringParameter("ModifiedOn", currentdate);
    response = request.execute(); // Trigger or Execute the rest message targeted to get the approval responses.
    gs.log("Power Automate dataverse - endpoint is: " + request.getEndpoint());
    httpResponseStatus = response.getStatusCode();
    var responseBody = response.getBody(); // get the approval responses as a body upon successful execution of the rest message 
    var httpStatus = response.getStatusCode();
    var response_json = JSON.parse(responseBody);
    gs.log("Power Automate dataverse -status code is :" + httpStatus);
    gs.log("Power Automate dataverse - response body is :" + responseBody);
    for (i = 0; i < response_json.value.length; i++) { // looping the body to process the all the approval responses
        var notes = "";
        var approval_tasks = new GlideRecord("sc_task");
        approval_tasks.addEncodedQuery("state=2^sys_id=" + response_json.value[i].msdyn_flow_approval_category); // get the task where state equals to Inprogress with corresponding sysID
        approval_tasks.query();
        if (approval_tasks.next()) {
                if (response_json.value[i].msdyn_flow_approval_result == 'Approve') { // check if the approval request status is approved
                    approval_tasks.setValue('state', '3'); //set the sc task state to closed completed
                } else if (response_json.value[i].msdyn_flow_approval_result == 'Reject') { // check if the approval request status is rejected
                    approval_tasks.setValue('state', '7'); //set the sc task state to closed rejected

                }
                notes=
                    "Approver Name : " +response_json.value[i].msdyn_flow_approvalrelationship_approvalresponses[0]["_ownerid_value@OData.Community.Display.V1.FormattedValue"]+"\n"+
                    "Approval Status : " +response_json.value[i].msdyn_flow_approvalrelationship_approvalresponses[0].msdyn_flow_approvalresponse_response+"\n"+
                    "Comments : " +response_json.value[i].msdyn_flow_approvalrelationship_approvalresponses[0].msdyn_flow_approvalresponse_comments; //Push approver name, response and comments to array
				approval_tasks.work_notes = notes;  //update task worknotes with notes array
                approval_tasks.update();

 

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

View solution in original post

@Manikantahere 

it's notes data so no need to use stringify, update this line

approval_tasks.work_notes = notes.toString();

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

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Manikantahere 

are you pushing that to work notes or any journal field?

if yes then you need to give the values in [code][/code] and then use <br/> to give new line

can you give complete requirement? with which system you are integrating? are you connecting to another ServiceNow instance?

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

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

Yes I am pushing the value to work notes.

 

yup I gave "[code]<br />[/code]" working but same not working if I want to join the values with one line gap in array.? "[code]<n/>[/code] does this won't work?

@Manikantahere 

share the complete code and what's your expected outcome?

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

    var currentdate = new GlideDate();
    var request = new sn_ws.RESTMessageV2("Power Automate Dataverse API", "Get Approval Responses");
    request.setStringParameter("envName", gs.getProperty("power_automate.environmentName"));
    request.setStringParameter("AppName", gs.getProperty("power_automate.applicationName"));
    request.setStringParameter("ModifiedOn", currentdate);
    response = request.execute(); // Trigger or Execute the rest message targeted to get the approval responses.
    gs.log("Power Automate dataverse - endpoint is: " + request.getEndpoint());
    httpResponseStatus = response.getStatusCode();
    var responseBody = response.getBody(); // get the approval responses as a body upon successful execution of the rest message 
    var httpStatus = response.getStatusCode();
    var response_json = JSON.parse(responseBody);
    gs.log("Power Automate dataverse -status code is :" + httpStatus);
    gs.log("Power Automate dataverse - response body is :" + responseBody);
    for (i = 0; i < response_json.value.length; i++) { // looping the body to process the all the approval responses
        var notes = [];
        var approval_tasks = new GlideRecord("sc_task");
        approval_tasks.addEncodedQuery("state=2^sys_id=" + response_json.value[i].msdyn_flow_approval_category); // get the task where state equals to Inprogress with corresponding sysID
        approval_tasks.query();
        if (approval_tasks.next()) {
                if (response_json.value[i].msdyn_flow_approval_result == 'Approve') { // check if the approval request status is approved
                    approval_tasks.setValue('state', '3'); //set the sc task state to closed completed
                } else if (response_json.value[i].msdyn_flow_approval_result == 'Reject') { // check if the approval request status is rejected
                    approval_tasks.setValue('state', '7'); //set the sc task state to closed rejected

                }
                notes.push(
                    "Approver Name : " +response_json.value[i].msdyn_flow_approvalrelationship_approvalresponses[0]["_ownerid_value@OData.Community.Display.V1.FormattedValue"]+"[code]<br />[/code]"+
                    "Approval Status : " +response_json.value[i].msdyn_flow_approvalrelationship_approvalresponses[0].msdyn_flow_approvalresponse_response+"[code]<br />[/code]"+
                    "Comments : " +response_json.value[i].msdyn_flow_approvalrelationship_approvalresponses[0].msdyn_flow_approvalresponse_comments
                ); //Push approver name, response and comments to array
				approval_tasks.work_notes = JSON.stringify(notes);  //update task worknotes with notes array
                approval_tasks.update();


I am expecting the work notes should populate as below

Manikantahere_0-1746617159000.png