- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 03:17 PM
I've created a new text field on my Account table called 'Account Friendly Name'. The default for this new field on all new Accounts created moving forward will be the actual value of the 'Account Name'. For all current Accounts, I need to update the Account Friendly Name with the current Account name. I'm assuming I would need to run a script of some sort, but don't have the ability to do that. Anyone provide sample code to use to update all current Accounts?
Account Friendly Name = u_account_friendly_name
Table / Account [customer_account]
Account Name = name
Table / Company [core_company]
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 04:14 PM
You need to run a fix script or background script to update the existing accounts.
var ca = new GlideRecord('customer_account');
ca.setLimit(2);
ca.query();
while (ca.next())
{
ca.u_account_friendly_name = ca.<account name field>;
ca.update();
}
Above is an example script. I have limit it to two, so that you test if works
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 04:32 PM - edited ‎12-05-2023 05:27 PM
Try the following in a script:
var dryRun = true;
var accountRec = new GlideRecord('customer_account'); // use correct table name
accountRec.query();
while (accountRec.next()) {
var companyAccount = new GlideRecord('core_company')
companyAccount.addQuery('name', accountRec.parent);
companyAccount.query();
if (companyAccount.next() {
accountRec.u_account_friendly_name = companyAccount.name;
gs.info('setting friendly name to ' + companyAccount.name + ' for ' + accountRec.name);
if (!dryRun)
accountRec.update();
}
}
provided the 'parent' field on core_company (by name) is the record in customer_account you want the value from.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 05:40 PM
Can I add a line in the code that only updates maybe 10 Accounts? Just to make sure it works correctly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2023 07:01 AM
Yes, see updated code posted by @Amit Gujarathi. but add
accountRec.setLimit(10);
Just before
accountRec.query();
Then there is no need for the 'updateLimit' variable in the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 08:25 PM
HI @MStritt ,
I trust you are doing great.
Please find the sample script below :
var dryRun = true; // Set to false to actually perform updates
var updateLimit = 10; // Number of records to update
var updatedCount = 0;
var accountRec = new GlideRecord('customer_account');
accountRec.query();
while (accountRec.next() && updatedCount < updateLimit) {
var companyAccount = new GlideRecord('core_company');
companyAccount.addQuery('name', accountRec.parent); // Assuming 'parent' is the correct field
companyAccount.query();
if (companyAccount.next()) {
accountRec.u_account_friendly_name = companyAccount.name;
gs.info('Setting friendly name to ' + companyAccount.name + ' for ' + accountRec.name);
if (!dryRun) {
accountRec.update();
updatedCount++;
}
}
}
gs.info('Updated ' + updatedCount + ' records.');
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi