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

@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

Hi @Ankur Bawiskar ,

Thank you so much for your quick response and guidance. I got the required result with your help.

 

With regards

 

P. Sivakrishna

Pavankumar_1
Mega Patron

Hi @Sivakrishna ,

if finance_company_code is a reference field you need to pass the sysid of the record you need to set not just one of the field value.

Refer below code

(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.sys_id;//pass sysid of the record
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar