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

@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

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