- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Need to set an state value dynamically from sys_choice list based selecting table, please helpt to write an scripted rest api
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Hello @BHARATHS
scripted rest api
(function process(request, response) {
try {
var body = request.body.data;
var tableName = body.table;
var recordSysId = body.sys_id;
var fieldName = body.field; // e.g. "state"
var choiceLabel = body.choice_label;
if (!tableName || !recordSysId || !fieldName || !choiceLabel) {
response.setStatus(400);
response.setBody({
status: "error",
message: "Missing required parameters"
});
return;
}
var choiceGR = new GlideRecord("sys_choice");
choiceGR.addQuery("name", tableName);
choiceGR.addQuery("element", fieldName);
choiceGR.addQuery("label", choiceLabel);
choiceGR.addQuery("inactive", false);
choiceGR.query();
if (!choiceGR.next()) {
response.setStatus(404);
response.setBody({
status: "error",
message: "Choice not found for given table and field"
});
return;
}
var choiceValue = choiceGR.getValue("value");
var targetGR = new GlideRecord(tableName);
if (!targetGR.get(recordSysId)) {
response.setStatus(404);
response.setBody({
status: "error",
message: "Record not found"
});
return;
}
targetGR.setValue(fieldName, choiceValue);
targetGR.update();
response.setStatus(200);
response.setBody({
status: "success",
table: tableName,
field: fieldName,
label: choiceLabel,
value: choiceValue
});
} catch (ex) {
response.setStatus(500);
response.setBody({
status: "error",
message: ex.message
});
}
})(request, response);
sys_choice table : Stores choice list values for fields like state, priority, etc.
If this response proves useful, please mark it as Accept as Solution and Helpful. Doing so benefits both the community and me. 👍🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
I won't recommend creating SLA via script.
I have never seen this in any of my implementations.
SLAs are to be configured by admin as 1 time activity and it can be updated later by admins in PROD
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Hi @BHARATHS
You are creating technical debt by using a script. It’s better to use the OOTB SLA definition and build it that way—it will be easier to implement and maintain.
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/dratulgrover [ Connect for 1-1 Session]
****************************************************************************************************************
