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 01:23 PM
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 01:59 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 12:00 PM
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);