Inbound Integration

abjaffrey
Giga Guru

Hi all,

 

I tried to executed this code by following the steps mentioned from the learn path in servicenow.

The expected outcome is to generate the city based on IP but its not generating

 

BR:

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
     try {
 var r = new sn_ws.RESTMessageV2('x_1582528_iplookup.IPInfo', 'GetIPInfo');
 r.setStringParameterNoEscape('specific_field', 'city');
 r.setStringParameterNoEscape('ipaddress', 'current.ip_address');

//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;
}

var responseObj = JSON.parse(responseBody);
current.city = responseObj.city;
gs.info('City = ' + current.city);
current.update();

catch(err)
{
    gs.info("Uncaught error : " + err);
    var message = ex.message;
}

})(current, previous);
 
The log shows this
abjaffrey_0-1733744662204.png

 

9 REPLIES 9

Abhay Kumar1
Giga Sage

@abjaffrey You can make sure below steps with avoiding using variable outside of try block and remove duplicate catch block,

 

You're passing current.ip_address as a string parameter to the REST Message. If the ip_address field is empty, this could result in an undefined value being sent.

Correctly validate the current.ip_address before using it.

 

responseBody is declared within the try block but used outside it, potentially causing an error if the try block fails.

 

There’s a second catch(err) block after the main code, which is syntactically incorrect.

 

Avoid calling current.update() within a synchronous Business Rule. It can cause recursion and performance issues.

 

Hope this will help you.

 

 

When i run the script below, im getting the city value in BG scripts.

 

try {
var r = new sn_ws.RESTMessageV2('x_1582528_iplookup.IPInfo', 'GetIPInfo');
r.setStringParameterNoEscape('specific_field', 'city');
r.setStringParameterNoEscape('ipaddress', '8.8.8.8');

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

//var responseObj = JSON.parse(responseBody);
//current.city = responseObj.city;
gs.info(responseBody);
//gs.info('City = ' + current.city);
//current.update();

Hi @abjaffrey 

Update the following code line in the BR. 

Query -> BR running on which table and what is trigger condition?

 

var responseObj = JSON.parse(responseBody);
gs.info('City = ' + responseObj.city); // This should print City value from responseObj
current.city = responseObj.city;
//gs.info('City = ' + current.city);
current.update();

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Not working,

 

The BR is running on a custom created table with 2 fields where if we enter the ip and submit the city field should be filled.

its an async with update & Insert

im able to print responseObj.city but the value is not getting assigned in the city fields