- 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 12:26 AM
Hi @Abhijit Das7 ,
what's the error you're getting?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 12:35 AM
Hi @Community Alums
I am not getting any error it is just that system properties are not updating.
I am sending data like this:
But system properties are not updating. I cannot see the values.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 02:19 AM
don't use decode method
try this and check what comes in system logs
Also I assume you are sending the request body in this format
{
"tenant_id":"hello",
"tenant_name":"bye"
}
(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");
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);
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:31 AM - edited 05-04-2023 02:35 AM
Even after your suggestion, system property value is not updating. I am able to see gs.info values in logs.
I am using this scripted rest api :
Thanks