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 09:30 AM
May be there are multiple records matching. Replace if(grVendor.next()) with a while loop like while(grVendor.next())
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 10:36 AM
I thought you maybe had the right answer, but it still did not work when I changed it to "while" but I did notice something else. The copying of the field was clearing out the u_vendor_tier_score on the sn_vdr_risk_asmt_vdr_tiering_assessment table, so I switched the two around but it still did not copy the number from the field over to the impactScore field.
Old: grVendor.u_vendor_tier_score = impactScore;
New: impactScore = grVendor.u_vendor_tier_score;
(function executeRule(current, previous /*null when async*/) {
var impactScore = current.u_impact_score;
var riskVendor = current.vendor.sys_id;
var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
grVendor.addQuery('vendor.sys_id', riskVendor);
grVendor.query();
while (grVendor.next()) {
impactScore = grVendor.u_vendor_tier_score;
grVendor.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 11:00 AM
//Try the following code.
(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()) {
grVendor.u_vendor_tier_score = impactScore;
//DO NOT RUN OTHER BRs ON THIS TABLE AFTER UPDATE. Probably they are updating the score
grVendor.setWorkflow(false);
grVendor.update();
}
})(current, previous);
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 11:28 AM
I added the info you suggested and have confirmed that there are no other business rules running on either table I'm using after mine - have the order on this one set to 1000 and all the others are at 100.
Unfortunately it still did not update the impact score field - I do really appreciate your help/advice.
Nona Johnson
here is the BR script currently
(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()) {
impactScore = grVendor.u_vendor_tier_score;
grVendor.setWorkflow(false);
grVendor.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 12:45 PM
Nona,
It appears you have the set action backwards in the while loop. Try this script with log statements to determine the issue.
(function executeRule(current, previous /*null when async*/ ) {
var impactScore = current.u_impact_score;
gs.log('Impact Score: ' + impactScore);
var riskVendor = current.vendor.sys_id.toString(); //Call toString
gs.log('Vendor sys_id: ' + riskVendor);
var grVendor = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
grVendor.addQuery('vendor.sys_id', riskVendor);
grVendor.query();
while (grVendor.next()) {
gs.log('Current record sys_id is: ' + grVendor.sys_id);
grVendor.u_vendor_tier_score = impactScore;
gs.log('Vendor Tier score updated');
grVendor.setWorkflow(false);
grVendor.update();
}
})(current, previous);
Thanks,
Derrick Johnson