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

Yes the vendors do match - I did go in and reset each one from both tables just to make sure

have tried it with the 

if (grVendor.next()) {   and also   

if (grVendor.vendor.sys_id == riskVendor) {

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

var impactScore = current.u_impact_score;
var riskVendor = current.vendor;

var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
grVendor.addQuery('vendor.sys_id', riskVendor);
grVendor.query();
if (grVendor.next()) {
grVendor.u_vendor_tier_score = impactScore;
grVendor.update();
}
})(current, previous);

I also checked the dictionary on the vendor field and it is an OOB reference field to the core_company table and the name is just "vendor"

NonaJohnson
Kilo Expert

The business rule is not working - it does not copy the field values from one custom field to another based on the vendor.  I have tried several versions of this business rule.  Here is the BR without all the text. I am doing a standard condition (if a field changes) and I did put the order of the BR to 1000 so that it would run last.

 

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

var impactScore = current.u_impact_score;
var riskVendor = current.vendor;


var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
     grVendor.addQuery('vendor', current.sys_id);
     grVendor.query();
if(vendor == riskVendor){
     grVendor.u_vendor_tier_score = impactScore;
     grVendor.update();
}
})(current, previous);

djohnson1
Mega Guru

Nona, 

     I believe the issue is related to your GlideRecord Query. I suspect the current.sys_id is the sys_id of the table2 record and not the vendor. Please update your script as noted below: 

 

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

    var impactScore = current.u_impact_score;
    var riskVendor = current.vendor; 

    var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
    grVendor.addQuery('vendor', riskVendor);
    grVendor.query();
    if (grVendor.vendor.sys_id == riskVendor) {
        grVendor.u_vendor_tier_score = impactScore;
        grVendor.update();
    }
})(current, previous);

 

Thanks, 

 

Derrick Johnson

Thanks Derrick - I have tried that and also a suggestion on my if statement from Allen - still does not copy over.

Here is what I have now - I removed the current.sys_id reference from the add query (Derricks) suggestion and also changed the if statement like you suggested.  The field value still does not copy over

 

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

var impactScore = current.u_impact_score;
var riskVendor = current.vendor;

var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
grVendor.addQuery('vendor', riskVendor);
grVendor.query();
if (grVendor.next()) {
grVendor.u_vendor_tier_score = impactScore;
grVendor.update();
}
})(current, previous);