- 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 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 03:47 AM
Thanks Ankur. Finally Scripted Rest API is working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 12:53 PM
@Ankur Bawiskar Does updating the sys_properties via the GlideaRecord cause any system performance issues like setProperty?
Thanks,
Gurbir