Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy string field value from one table to another with a business rule

NonaJohnson
Kilo Expert

Am writing what I thought would be an easy business rule to copy the data  from one table to another, but am having some issues.

Table 1 contains a custom field named u_vendor_tier_score (string value)

Table 2 contains a custom field name u_impact score (also a string value field)

I want to copy the data in Table 1 u_vendor_tier score to Table 2 u_impact_score if the vendors on each match

Created an after/update business rule using Table 2 as that has the field that is used in the condition (risk rating changes) and added a new GlideRecord to Table 1 that has the field (u_vendor_tier_score) that I want to get the value from and copy it to Table 2 into u_impact_score

Here is the BR script:

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

var impactScore = current.u_impact_score; // not sure if I needed this var or not
var riskVendor = current.vendor;  //added this var as I need to match the vendor field from Table 1 and Table 2. Both of the fields are vendor reference fields going to the same table

var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment'); // this is table 1
grVendor.addQuery('vendor', current.sys_id); //get the vendor sys_id
grVendor.query();
if(vendor == riskVendor){                // if the vendor from table 1 = vendor from table 2
  grVendor.u_vendor_tier_score = impactScore;   //then copy the vendor tier score to the impact score field
  grVendor.update();
}
})(current, previous);

So if the two vendors from Table 1 and Table 2 match, copy the u_vendor_tier_score from table 1 to table 2 into the u_impact_score field

Can anyone make a suggestion on the BR or should I be doing this differently.

Thanks

Nona Johnson

17 REPLIES 17

The one that is correct is bc87b0f56f990bc023cdd194eb3ee430 (this is the vendor sys_id).  The other sys_id is related to the Vendor Risk assessment itself, not the vendor.

find_real_file.png

and the reason I changed the value set script from

grVendor.u_vendor_tier_score = impactScore;

to:

impactScore = grVendor.u_vendor_tier_score;

is because it was clearing out the u_vendor_tier_score field after the business rule ran.

I changed the below script to include "vendor" and not just the sys_id and the message log now shows the same sys_id.  

 

I also got these two in the log, but not sure what they are referring to

find_real_file.png

 

gs.log('Current record sys_id is: ' + grVendor.vendor.sys_id);

Finally figured this out - I was setting the values inside the new GlideRecord script instead of it being after

 

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

var impactScore = current.u_impact_score;
var riskVendor = current.vendor.sys_id.toString(); //Call toString

var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
grVendor.addQuery('vendor.sys_id', riskVendor);
grVendor.query();
while (grVendor.next()) {
current.u_impact_score = grVendor.u_vendor_tier_score;
}

})(current, previous);