Copy string field value from one table to another with a business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 07:45 AM
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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 07:54 AM
Hi,
What issues are you having?
This post was a little rough to read and you're not saying if you tried this and what has or hasn't worked, you're just saying you're having issues.
1) You'd need to do if (grVendor.next()) { instead of if (vendor == riskVendor) {
2) and in the query you'd need to look for the vendor sys_id (since I assume it's a reference field)...so grVendor.addQuery('vendor', riskVendor);
3) You'd also need to check your field names if it's not u_vendor versus... just vendor...and if this is on a catalog item or something then current.variables.vendor...
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 08:05 AM
Please see my suggestions for fix above.
Thanks
Please mark reply as Helpful/Correct, if applicable.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 08:09 AM
I tried that plus what was suggested by Derrick, but neither worked
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 tuggested. 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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 08:20 AM
So does the vendor field actually match one then in your test? This will only work if the vendor is found on the other table. You could also try:
grVendor.addQuery('vendor.sys_id', riskVendor);
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!