want to know whats wrong in my code

mishrarakes
Tera Contributor

I am learning about serviceNow integeration , and fetching api from outside api using serviceNow Outbound Integration using request and made a new field on want to add  the api details using script in Business rule , but i don't know why its not working , I am attaching the important screenshots here can someone help.

 

 

```

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

    // Add your code here

try {
 var r = new sn_ws.RESTMessageV2('x_1332747_iplook_0.ipLookup', 'Default GET');

  r.setQueryParameter('ip',current.enter_your_ip);
 var response = r.executeAsync();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();

 var responseObject = JSON.parse(responseBody);


 current.country = responseObject.country_code3;
 current.city = responseObject.city;
 current.currency = responseObject.currency.name;
 
 current.update();
}
catch(ex) {
 var message = ex.message;
}

})(current, previous);
```
 
fyi : referred techwithpri servicenow integration playlist
5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@mishrarakes 

unless your API gives correct JSON response you can't parse and update the fields

see what you got in logs for response body

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

    // Add your code here

    try {
        var r = new sn_ws.RESTMessageV2('x_1332747_iplook_0.ipLookup', 'Default GET');
        r.setQueryParameter('ip', current.enter_your_ip);
        var response = r.executeAsync();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        gs.info('Response body' + responseBody);
        var responseObject = JSON.parse(responseBody);
        current.country = responseObject.country_code3;
        current.city = responseObject.city;
        current.currency = responseObject.currency.name;

        current.update();
    } catch (ex) {
        var message = ex.message;
    }

})(current, previous);

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

Robert H
Mega Sage

Hello @mishrarakes ,

 

Please replace 

 var response = r.executeAsync();

with

var response = r.execute();

 

Please also print the values of httpStatus and responseBody to the log to verify you are getting the expected response.

 

Regards,

Robert

Nishant8
Giga Sage

Hello @mishrarakes, Can you please explain what's not working here? Yes, you can analyze whether you receive proper response. Also, is there any specific reason of using current.update() in your business rule? can you remove this if not required and then try?

 

Regards,

Nishant

mishrarakes
Tera Contributor

Hello @Nishant8 I want to update my field value which i have created for city , country ,and currency which you can see on my code for each ip which i will enter and get city ,country and currency from api call, for this as I am inserting a new data, so I am using Business rule with script, but after each ip on reloading/saving i did not get those values in my fields, may be something wromg in my code thats the issue.