- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 12:32 AM
Hi Team,
I have one requirnment to share an Incident details created on one servicenow instance that should create on another servicenow instance.
Any sample code ??
Thanks
Snow Learner
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 04:52 AM
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 07:34 AM
Hi,
you can use Business Rule (async BR on Insert or update based on your requirnment).
Assuming async BR--> on Insert:-
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 11:23 PM
Can you try the below also :--
// Set the REST endpoint URL
var restEndpoint = "https://XXXXX.service-now.com/api/now/table/incident?sysparm_query=&sysparm_display_value=false&sysp..." + limit + "&sysparm_offset=" + offset;
// Set the authentication profile
var r = new sn_ws.RESTMessageV2();
r.setEndpoint(restEndpoint);
r.setHttpMethod('GET');
r.setRequestHeader('Content-Type', 'application/json');
r.setAuthenticationProfile('basic', "c8ac221adb1209900034150505961988"); // create your own basic profile
// Execute the REST API call
try {
var response = r.execute();
} catch(e) {
gs.log("ERROR: Sending REST API " + e + "\n\n" + restEndpoint, "DATA_MIGRATION_LOG");
}
// Parse the response
if (!response.haveError()) {
var parsed = JSON.parse(response.getBody());
var results = parsed.result;
// Loop through the results and create a new Incident on the target instance
results.forEach(function(item) {
var gr = new GlideRecord('incident');
gr.initialize();
// Set the Incident fields
for (var key in item) {
if (key == "sys_id") {
gr.setNewGuidValue(item[key]);
} else if (key != "sys_tags") {
gr.setValue(key, item[key]);
}
}
// Insert the new Incident
gr.autoSysFields(false);
gr.setWorkflow(false);
var recordSysId = gr.insert();
// Log any errors
if (!recordSysId) {
gs.log("NOT INSERTED: incident." + item.sys_id + " : " + gr.getLastErrorMessage(), "DATA_MIGRATION_LOG");
}
});
} else {
var responseBody = response.getBody();
var message = responseBody + "\nStatus Code: " + response.getStatusCode() + "\nError Code: " + response.getErrorCode() + "\nError Message: " + response.getErrorMessage();
gs.log("ERROR: responce REST API " + restEndpoint + "\n\n" + message, "DATA_MIGRATION_LOG");
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 04:52 AM
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 07:34 AM
Hi,
you can use Business Rule (async BR on Insert or update based on your requirnment).
Assuming async BR--> on Insert:-
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 11:23 PM
Can you try the below also :--
// Set the REST endpoint URL
var restEndpoint = "https://XXXXX.service-now.com/api/now/table/incident?sysparm_query=&sysparm_display_value=false&sysp..." + limit + "&sysparm_offset=" + offset;
// Set the authentication profile
var r = new sn_ws.RESTMessageV2();
r.setEndpoint(restEndpoint);
r.setHttpMethod('GET');
r.setRequestHeader('Content-Type', 'application/json');
r.setAuthenticationProfile('basic', "c8ac221adb1209900034150505961988"); // create your own basic profile
// Execute the REST API call
try {
var response = r.execute();
} catch(e) {
gs.log("ERROR: Sending REST API " + e + "\n\n" + restEndpoint, "DATA_MIGRATION_LOG");
}
// Parse the response
if (!response.haveError()) {
var parsed = JSON.parse(response.getBody());
var results = parsed.result;
// Loop through the results and create a new Incident on the target instance
results.forEach(function(item) {
var gr = new GlideRecord('incident');
gr.initialize();
// Set the Incident fields
for (var key in item) {
if (key == "sys_id") {
gr.setNewGuidValue(item[key]);
} else if (key != "sys_tags") {
gr.setValue(key, item[key]);
}
}
// Insert the new Incident
gr.autoSysFields(false);
gr.setWorkflow(false);
var recordSysId = gr.insert();
// Log any errors
if (!recordSysId) {
gs.log("NOT INSERTED: incident." + item.sys_id + " : " + gr.getLastErrorMessage(), "DATA_MIGRATION_LOG");
}
});
} else {
var responseBody = response.getBody();
var message = responseBody + "\nStatus Code: " + response.getStatusCode() + "\nError Code: " + response.getErrorCode() + "\nError Message: " + response.getErrorMessage();
gs.log("ERROR: responce REST API " + restEndpoint + "\n\n" + message, "DATA_MIGRATION_LOG");
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/