Business Rule Syntax Help- Copying RITM Reference Field to Another Reference Field Diff. Table

jlaps
Kilo Sage

Pretty sure I am making some basic mistakes, but having trouble either with the searching of the TICKET EXCHANGE record, and/or in setting the value.

Purpose: I created a queue to capture all the values of an onboard email before sending a notification out that today is missing values when the account is initially created, so capturing the information and verifying before sending. That is all working. 

What is not working is where I am watching for ONBOARD RITMs being created, and comparing the new hire user on the onboard to my queue, and updating the RITM field on the queue with the onboard RITM. 

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

var newhire = current.variables.requested_for;
gs.info("JAL1 newhire is: "+newhire); //I am getting the sys ID of the user record successfully
var exchangerec = new GlideRecord("u_element_ticket_exchange");
//exchangerec.addEncodedQuery("u_employee=newhire^u_ritmISEMPTY")
//exchangerec.addQuery("u_ritm", "");
exchangerec.addQuery("u_employee.sys_id", newhire);
exchangerec.query();
gs.info("JAL1 RITM is: "+current.getDisplayValue()); //I am getting the RITM# successfully
if (exchangerec){
	gs.info("JAL1 exchangerec is: "+exchangerec.sys_id); //I am never getting this record as anything but blank or undefined
	exchangerec.u_ritm = current.sys_id;
}

})(current, previous);

The log statements on this most recent version look like-

jlaps_0-1723123248008.png

I apologize for asking what is likely a stupid mistake, but not sure where I am going wrong and lack of caffeine has me asking!

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

Hi @jlaps 

As per my knowledge I found following two issues in your code:

1) You are querying the u_element_ticket_exchange table, but you need to ensure you're checking if the record exists after running .query().

 

2) In your if statement, you're checking if (exchangerec), which doesn't confirm if the query found any results. Instead, you should use .next() to move to the first record if it exists

 

I have fix in following script, please try once bellow script:

 

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

    var newhire = current.variables.requested_for;
    gs.info("JAL1 newhire is: " + newhire); // Getting the sys ID of the user record successfully

    var exchangerec = new GlideRecord("u_element_ticket_exchange");
    exchangerec.addQuery("u_employee", newhire); // Corrected query to directly compare the sys_id
    exchangerec.addNullQuery("u_ritm"); // Ensures only records with an empty u_ritm are selected
    exchangerec.query();

    if (exchangerec.next()) { // This checks if a record was found and moves to it
        gs.info("JAL1 exchangerec is: " + exchangerec.sys_id); // Should now output the sys_id of the found record
        exchangerec.u_ritm = current.sys_id;
        exchangerec.update(); // Save the updated record
    } else {
        gs.info("JAL1 No matching record found.");
    }

})(current, previous);

 

 

I hope my answer helps you to resolve your issue, if yes mark my answer helpful & correct.

THANK YOU

rajesh chopade.

View solution in original post

3 REPLIES 3

Neeraj31
Mega Sage

Hello,

 

Please try to use "exchangerec.next()" instead of "exchangerec" inside if condition.

if (exchangerec.next()){
	gs.info("JAL1 exchangerec is: "+exchangerec.sys_id); //I am never getting this record as anything but blank or undefined
	exchangerec.u_ritm = current.sys_id;
}

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Rajesh Chopade1
Mega Sage

Hi @jlaps 

As per my knowledge I found following two issues in your code:

1) You are querying the u_element_ticket_exchange table, but you need to ensure you're checking if the record exists after running .query().

 

2) In your if statement, you're checking if (exchangerec), which doesn't confirm if the query found any results. Instead, you should use .next() to move to the first record if it exists

 

I have fix in following script, please try once bellow script:

 

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

    var newhire = current.variables.requested_for;
    gs.info("JAL1 newhire is: " + newhire); // Getting the sys ID of the user record successfully

    var exchangerec = new GlideRecord("u_element_ticket_exchange");
    exchangerec.addQuery("u_employee", newhire); // Corrected query to directly compare the sys_id
    exchangerec.addNullQuery("u_ritm"); // Ensures only records with an empty u_ritm are selected
    exchangerec.query();

    if (exchangerec.next()) { // This checks if a record was found and moves to it
        gs.info("JAL1 exchangerec is: " + exchangerec.sys_id); // Should now output the sys_id of the found record
        exchangerec.u_ritm = current.sys_id;
        exchangerec.update(); // Save the updated record
    } else {
        gs.info("JAL1 No matching record found.");
    }

})(current, previous);

 

 

I hope my answer helps you to resolve your issue, if yes mark my answer helpful & correct.

THANK YOU

rajesh chopade.

Thank you Rajesh and Neeraj, the .next() did indeed result in actually locking onto the record I was looking for, and the assignment noted at the end of your reply Rajesh helped out as well.

Thanks again!