want to know whats wrong in my code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2025 02:26 AM
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.
```
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2025 02:36 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2025 03:03 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2025 03:15 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2025 04:23 AM
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.