How to fetch single record using script "get rest message" from another instance?

Prathamesh Chav
Tera Contributor

Hi Team,

 

I am trying to fetch the record and I want it dynamically, so whenever I am selecting the record in instance 2, I want all the data of that record from instance 1.

 

I am using rest message for this and in get method I am trying to fetch the single record.

I am able to fetch all the records but I want single record only.

 

PrathameshChav_3-1739341576042.png

 

 

any changes I need to do in script?

 

I am able to pass the query parameters but not able to see them in the script?

 

PrathameshChav_2-1739341442050.png

 try { 
 var r = new sn_ws.RESTMessageV2('PDI Int Get data', 'Default GET');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}

 

Can anyone help me in this?

 

Thank you

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Prathamesh Chav 

you need to use Retrieve records API and pass the query

AnkurBawiskar_0-1739342232827.png

 

Use this to get only 1 record

AnkurBawiskar_1-1739342258248.png

 

You will also get code sample on how to consume it. this will help you when you consume it from other instance

AnkurBawiskar_2-1739342329947.png

 

No need of REST Message in other instance, directly use the above script and give correct authentication details.

I believe this much information is sufficient for you to start and enhance it further

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

Community Alums
Not applicable

Hi @Prathamesh Chav 

To fetch single record from other instance, we have make our endpoint unique, make modification in REST Message with the GET method with following endpoint.

https://instance1.service-now.com/api/now/table/{table_name}/{sys_id}

 

View solution in original post

6 REPLIES 6

Hi tried applying query but still it is fetching all record,

 

PrathameshChav_2-1739356612088.png

 

 

following is the script 

@Community Alums @Lhora Alvarez 

can you check

 

Calling this script include

 

PrathameshChav_0-1739356467453.png

getPdiRecord: function() {
  var recordSys = this.getParameter('userId');

try {
 var r = new sn_ws.RESTMessageV2('PDI Int Get data', 'Default GET');
 r.setStringParameterNoEscape('sys_id', "ff4c21c4735123002728660c4cf6a758");

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}

    if (httpStatus == 200) {
        var statusAPI = "success";
        var num = responseBody;
        return JSON.stringify(num)
    } else{
        var statusAPI2 = "API failed";
    }

},

    type: 'sampleUtils'
});
 
 
Client script:-
PrathameshChav_1-1739356514555.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var email = g_form.getValue('emailUser');

    //Call script include
    var ga = new GlideAjax('global.sampleUtils');   //Scriptinclude
    ga.addParam('sysparm_name', "getPdiRecord"); //Method
    ga.addParam('userId',email); //Parameters
    ga.getXMLAnswer(getResponse);
   
    function getResponse(response){
        var res = JSON.parse(response);
        g_form.addInfoMessage(res);
       
   
    }
   
}
 
here is the get method
 
PrathameshChav_3-1739356653358.png

 

Lhora Alvarez
Tera Sage

@Prathamesh Chav 


I am able to pass the query parameters but not able to see them in the script?


You need to define the values you pass as variables, which is why they aren't showing in the script.

Variables should be in the format ${variable_name}.

After entering the variables in the endpoint or parameters, click "Auto-generate variables" in the related links.

This will generate and display the variable records in the Variable Substitutions related list.

Please refer to this documentation: https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/integrate/outbound-rest/concept/c...