Business Rule not populated the value of a field

Sivakrishna
Kilo Sage

Hi,

      I have a Record producer "Payment request", When I have submitted the Record Producer, a Finance case will be created. When I open the finance case in Native UI, there is a field called "company_code" in the finance case, the value of the "company_code" is populated based on the below Before Business rule.

 

When to Run:

----------------

When: before

order:100

Filter Conditions: No conditions

Insert: checked as true

Update: checked as false

Advanced: Checked as True

 

Script:

-------

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here
var cc = current.internal_user.u_company_code;
var grCC = new GlideRecord('u_company_code');
grCC.addQuery('u_company_code_1', cc);
grCC.query();
gs.info("Print" + " : " + grCC.getRowCount());

gs.info("Print"+":"+grCC.u_company_code_1);

if (grCC.next()) {
gs.info("Entered If Loop");

current.finance_company_code = grCC.u_company_code_1;

}

 

In the above code, it entering the IF loop but not populating the value of "grCC.u_company_code_1". I have logged the "grCC.u_company_code_1", The value of the filed "grCC.u_company_code_1" shown in the logs also but not populated in the finance case field "current.finance_company_code = grCC.u_company_code_1;"

 

Please help me...

 

With Regards

 

P. Sivakrishna

1 ACCEPTED SOLUTION

@Sivakrishna 

I hope with this sample you should be able to achieve. If you are new to development then please go through docs or fundamentals course

(function executeRule(current, previous /*null when async*/ ) {

	// Add your code here
	var cc = current.internal_user.u_company_code;
	var grCC = new GlideRecord('u_company_code');
	grCC.addQuery('u_company_code_1', cc);
	grCC.query();
	gs.info("Print" + " : " + grCC.getRowCount());

	gs.info("Print"+":"+grCC.u_company_code_1);

	if (grCC.next()) {
		gs.info("Entered If Loop");

		var rec = new GlideRecord('table referred by finance_company_code');
		rec.addQuery('fieldName', grCC.u_company_code_1); // give field which holds the value
		rec.query();
		if(rec.next()){
			current.setValue('finance_company_code', rec.getUniqueValue());
		}
	}
	
});

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

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Sivakrishna 

both the fields are reference type and referring to same table? finance_company_code and u_company_code_1

If yes then it should work fine

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

Hi @Ankur Bawiskar ,

Thank you for your immediate response.

This field "finance_company_code" is a referenced field of "u_company_code" table.

This field "u_company_code_1" is a string type field and table is "u_company_code"

finance_company_code and  u_company_code_1 are not referring to the same table.

 

Please provide some alternative solution.....

 

With Regards

 

P. Sivakrishna

 

@Sivakrishna 

this line will work if the string value present in u_company_code_1 is the Display value present in table being referred by finance_company_code

current.setDisplayValue('finance_company_code',grCC.u_company_code_1);

If this doesn't work then please query table referred by finance_company_code with the field which holds value present in u_company_code_1 and then set sysId

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 ,

Thank you for immediate response.

Please can you tell me method to get the sys_id of querried record ??

 

With Regards

P, Sivakrishna