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 

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

Hi @Ankur Bawiskar 

Thanks Ankur. Finally Scripted Rest API is working.

@Ankur Bawiskar Does updating the sys_properties via the GlideaRecord cause any system performance issues like setProperty? 


Thanks,
Gurbir