- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 11:37 AM
Hi,
I have two reference fields(Program Manager, company) from two different tables(sys_users, core_company ) in program record. I want the company field needs to auto populate depends on program manager. I know we can achieve this by writing a business rule but If someone changes the company value in sys_user record that automatically needs to update the company value in program. Ex; Manager X has company dell, he changes from dell to intel, the company field needs to change to intel automatically so we don't need to go and change manually.
Please someone help me how can I achieve this.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 12:15 PM
Hi @Yamja
BR can be something like this -
// Check if the company field is modified
if (current.company.changes()) {
// Get the updated company value
var newCompany = current.company;
// Query the program table for records with the old company value
var programGR = new GlideRecord('program');
programGR.addQuery('program_manager', current.sys_id); // Assuming the reference field is named 'program_manager'
programGR.addQuery('company', current.company.getPrevious()); // Get the previous value of the company field
programGR.query();
// Update the company field in the program records with the new company value
while (programGR.next()) {
programGR.company = newCompany;
programGR.update();
}
}
Reference qualifier script -
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 12:15 PM
Hi @Yamja
BR can be something like this -
// Check if the company field is modified
if (current.company.changes()) {
// Get the updated company value
var newCompany = current.company;
// Query the program table for records with the old company value
var programGR = new GlideRecord('program');
programGR.addQuery('program_manager', current.sys_id); // Assuming the reference field is named 'program_manager'
programGR.addQuery('company', current.company.getPrevious()); // Get the previous value of the company field
programGR.query();
// Update the company field in the program records with the new company value
while (programGR.next()) {
programGR.company = newCompany;
programGR.update();
}
}
Reference qualifier script -
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2023 09:46 AM
Hi Tushar,
I wrote this script "
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2023 12:09 PM
Hi Tushar, Thanks for the script. It's working, I wrote a BR in user table with after insert and update condition.
I removed
"programGR.addQuery('company', current.company.getPrevious());"
from the script it worked. Thanks