- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 12:14 AM - edited 05-04-2023 02:07 AM
Hi Everyone,
I have created one Scripted Rest API (POST) to update values of two system property. But when I am testing this API from Rest API explorer it is not working , system properties are not getting updated . Can anyone help me in correcting my mistake.
Scripted Rest API :
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var tenantId = parsedData.tenant_id;
var tenantName = parsedData.tenant_name;
if(tenantId){
var rec1 = new GlideRecord("sys_properties");
rec1.addQuery("name", "name_of_the_system_property1");
rec1.query();
if (rec1.next()){
rec1.value = tenantId ;
rec1.update;
}
}
if(tenantName ){
var rec2 = new GlideRecord("sys_properties");
rec2.addQuery("name", "name_of_the_system_property2");
rec2.query();
if (rec2.next()){
rec2.value = tenantName ;
rec2.update;
}
}
})(request, response);
cc: @Ankur Bawiskar @kamlesh kjmar @Community Alums
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 03:40 AM
it should be update() and not update
you are using wrong function name
Final code here
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parsedData = JSON.parse(requestBody);
var tenantId = parsedData.tenant_id;
var tenantName = parsedData.tenant_name;
gs.info("Tenant ID" + tenantId);
gs.info("Tenant Name" + tenantName);
if(tenantId){
var rec1 = new GlideRecord("sys_properties");
rec1.addQuery("name", "name_of_the_system_property1"); // are you using correct name here
rec1.query();
if (rec1.next()){
gs.info("inside 1st query");
rec1.value = tenantId ;
rec1.update();
}
}
if(tenantName ){
var rec2 = new GlideRecord("sys_properties");
rec2.addQuery("name", "name_of_the_system_property2"); // are you using correct name here
rec2.query();
if (rec2.next()){
gs.info("inside 2nd query");
rec2.value = tenantName ;
rec2.update();
}
}
})(request, response);
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
05-04-2023 02:34 AM
So you are able to see the values in system logs which you receive after parsing?
if yes then check if query is working fine or not
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parsedData = JSON.parse(requestBody);
var tenantId = parsedData.tenant_id;
var tenantName = parsedData.tenant_name;
gs.info("Tenant ID" + tenantId);
gs.info("Tenant Name" + tenantName);
if(tenantId){
var rec1 = new GlideRecord("sys_properties");
rec1.addQuery("name", "name_of_the_system_property1"); // are you using correct name here
rec1.query();
if (rec1.next()){
gs.info("inside 1st query");
rec1.value = tenantId ;
rec1.update;
}
}
if(tenantName ){
var rec2 = new GlideRecord("sys_properties");
rec2.addQuery("name", "name_of_the_system_property2"); // are you using correct name here
rec2.query();
if (rec2.next()){
gs.info("inside 2nd query");
rec2.value = tenantName ;
rec2.update;
}
}
})(request, response);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 02:45 AM
I can see all gs.info in logs.
But still system property is not updated
System property name :
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 03:34 AM
scripted rest api is in which scope?
system property is in which scope?
if both are in same scope then it should work fine
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 03:38 AM - edited 05-04-2023 03:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 03:36 AM
In code rec1.update and rec1.update is not working . Otherwise I have placed gs.info at all levels and tested.
Thanks