Unable to parse JSON for a field in REST message using setRequestBody method

GD11
Tera Expert

We are unable set parameters in request Body.

error message:

GD11_0-1752158380774.png

logs are printing fine.

 
 

value of body input_data={"request":{"subject":"Product Notice: The status of MOTORCYCLEACCIDENT.COM has changed.","description":"Product Notice: The status of MOTORCYCLEACCIDENT.COM has changed.\n\nYou can see this change in your account or get more details inside. ‌ ‌ ‌ ‌ ‌ ‌ \n\n ‌ ‌ ‌ ‌ ‌ ‌ ‌

 

rest message function.

 createIncident: function(current) {

        var subject = current.short_description.toString();
        var snTicketSysId = current.sys_id.toString();
        var catValue = current.category.getDisplayValue();
        var subCatValue = current.subcategory.getDisplayValue();
        var caller = current.caller_id.getDisplayValue();
        var affectedUser = current.u_affected_user.getDisplayValue();
        var description = current.description.toString();
        var snTicketNumber = current.number.toString();
        var assignmentGroup = current.assignment_group.getDisplayValue();

        // var currentPriority = [];
        // currentPriority.push(current.priority.getDisplayValue());
        // gs.log('priority value ' + currentPriority.toString());
        // currentPriority = currentPriority.toString().trim();
        // var priorityValue = currentPriority.split('-');
        // priorityValue = priorityValue[1];
        // gs.log('priority value 1 ' + priorityValue[1]);

        var requestObj = {};
        requestObj = { // code to generate payload for all variables required.
            "request": {
                "subject": subject,
                "description": description,
                "requester": {
                    "email_id": "Amynta.SDP@fulcrumdigital.com"
                },
                "status": {
                    "name": "New"
                },
                "udf_fields": {
                    "udf_char32": snTicketSysId,
                    "udf_char44": assignmentGroup,
                    "udf_char42": caller,
                    "udf_char22": affectedUser,
                    "udf_char21": snTicketNumber
                },
                "template": {
                    "name": "***_Cloud_Incidents"
                },
            }
        }
        gs.log('function being called ');
        var inputData = "input_data="+ JSON.stringify(requestObj);
        //inputData = JSON.stringify(inputData);
        gs.log('value of body ' + inputData);

        try {
            var r = new sn_ws.RESTMessageV2('***Service Desk Integration', 'Create Request and Incident');
            r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            r.setRequestHeader('accept', 'application/vnd.manageengine.sdp.v3+json');
            r.setRequestHeader("Authorization", "Bearer " + gs.getProperty('Manage.Engine.SDP.AccessToken'));
            r.setAuthentication("oauth2", "Manage Engine Service Desk Integration default_profile");
            //r.setStringParameterNoEscape('input_data', JSON.stringify(requestObj));
            r.setRequestBody(inputData);           

            var response = r.execute();
            var responseBody = response.getBody();
            gs.log('value of response body ' + responseBody);
            var incidentResBody = JSON.parse(responseBody);
             var httpStatus = response.getStatusCode();
            gs.log('value of status ' + httpStatus);
        } catch (ex) {
            var message = ex.message;
            gs.log('value of error message ' + message);
        }
 
 
 
 
 

 

 

 

 

7 REPLIES 7

Try this:

function createIncident(current) {
   var subject = current.short_description.toString();
   var snTicketSysId = current.sys_id.toString();
   var catValue = current.category.getDisplayValue();
   var subCatValue = current.subcategory.getDisplayValue();
   var caller = current.caller_id.getDisplayValue();
   var affectedUser = current.u_affected_user.getDisplayValue();
   var description = current.description.toString();
   var snTicketNumber = current.number.toString();
   var assignmentGroup = current.assignment_group.getDisplayValue();
   var requestObj = {
       "request": {
           "subject": subject,
           "description": description,
           "requester": {
               "email_id": "Amynta.SDP@fulcrumdigital.com"
           },
           "status": {
               "name": "New"
           },
           "udf_fields": {
               "udf_char32": snTicketSysId,
               "udf_char44": assignmentGroup,
               "udf_char42": caller,
               "udf_char22": affectedUser,
               "udf_char21": snTicketNumber
           },
           "template": {
               "name": "***_Cloud_Incidents"
           }
       }
   };
var inputData = "input_data="+ JSON.stringify(requestObj);
   gs.log('value of body: ' + inputData);
   try {
       var r = new sn_ws.RESTMessageV2('***Service Desk Integration', 'Create Request and Incident');
       r.setRequestHeader("Content-Type", "application/json"); 
       r.setRequestHeader("accept", "application/vnd.manageengine.sdp.v3+json");
       r.setRequestHeader("Authorization", "Bearer " + gs.getProperty('Manage.Engine.SDP.AccessToken'));
       r.setAuthentication("oauth2", "Manage Engine Service Desk Integration default_profile");
       r.setRequestBody(inputData); 
       var response = r.execute();
       var responseBody = response.getBody();
       gs.log('value of response body: ' + responseBody);
       var incidentResBody = JSON.parse(responseBody);
       var httpStatus = response.getStatusCode();
       gs.log('value of status: ' + httpStatus);
   } catch (ex) {
       gs.log('value of error message: ' + ex.message);
   }
}

Hope that helps!

Hi @Vishal Jaswal @Ankur Bawiskar 

 I am still getting error below:

value of response body: {"response_status":{"status_code":4000,"messages":[{"status_code":4001,"field":"input_data","type":"failed","message":"Value not present for input_data"}],"status":"failed"}}

 

is this happening because they required all these values to be passed for 'input_data' parameter? i tried removing input_data field from the rest message method still i am getting same error message.

 

Hello @GD11 

I believe you should try it first in the background script to test it out. Here is the background script without input_data

(function() {
  
   var subject = "Product Notice: The status of MOTORCYCLEACCIDENT.COM has changed.";
   var snTicketSysId = "a1b2c3d4e5f67890abcdef1234567890";
   var caller = "John Doe";
   var affectedUser = "Jane Smith";
   var description = "Product Notice: The status of MOTORCYCLEACCIDENT.COM has changed.\n\nYou can see this change in your account or get more details inside.";
   var snTicketNumber = "INC0012345";
   var assignmentGroup = "Service Desk";
   var requestObj = {
       "request": {
           "subject": subject,
           "description": description,
           "requester": {
               "email_id": "Amynta.SDP@fulcrumdigital.com"
           },
           "status": {
               "name": "New"
           },
           "udf_fields": {
               "udf_char32": snTicketSysId,
               "udf_char44": assignmentGroup,
               "udf_char42": caller,
               "udf_char22": affectedUser,
               "udf_char21": snTicketNumber
           },
           "template": {
               "name": "***_Cloud_Incidents"
           }
       }
   };
   // Convert to JSON string for request body
   var inputData = JSON.stringify(requestObj);
  
   gs.print('Raw JSON:\n' + inputData);
   gs.print('Formatted JSON:\n' + JSON.stringify(requestObj, null, 2));
   try {
       var r = new sn_ws.RESTMessageV2('Service Desk Integration', 'Create Request and Incident'); // Correct this if required
       r.setRequestHeader("Content-Type", "application/json");
       r.setRequestHeader("accept", "application/vnd.manageengine.sdp.v3+json");
       r.setRequestHeader("Authorization", "Bearer " + gs.getProperty('Manage.Engine.SDP.AccessToken'));
       r.setAuthentication("oauth2", "Manage Engine Service Desk Integration default_profile");
       r.setRequestBody(inputData);
       var response = r.execute();
       var responseBody = response.getBody();
       var httpStatus = response.getStatusCode();
       gs.print('Response Status: ' + httpStatus);
       gs.print('Response Body:\n' + responseBody);

       var parsedResponse;
       try {
           parsedResponse = JSON.parse(responseBody);
           gs.print('Parsed JSON Response:\n' + JSON.stringify(parsedResponse, null, 2));
       } catch (parseErr) {
           gs.print('Error parsing response JSON: ' + parseErr.message);
       }
   } catch (ex) {
       gs.print('Exception occurred: ' + ex.message);
   }
})();

Hope that helps!