Update system property with Scripted Rest API ( POST)

Abhijit Das7
Tera Expert

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

 

1 ACCEPTED SOLUTION

@Abhijit Das7 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

@Abhijit Das7 

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);

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

I can see all gs.info in logs.

 

AbhijitDas7_0-1683193435564.png

But still system property is not updated

System property name :

AbhijitDas7_1-1683193532751.png

 

Thanks  

@Abhijit Das7 

scripted rest api is in which scope?

system property is in which scope?

if both are in same scope then it should work fine

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

Both are in same scope , system property and scripted rest api.

AbhijitDas7_0-1683196777668.pngAbhijitDas7_1-1683196808556.png

 

 

Thanks

Hi @Ankur Bawiskar 

In code rec1.update and rec1.update is not working . Otherwise I have placed gs.info at all levels and tested.

Thanks