Update new field on all Accounts with the Account Name

MStritt
Tera Guru

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]

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

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.

View solution in original post

8 REPLIES 8

Bert_c1
Kilo Patron

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.

Can I add a line in the code that only updates maybe 10 Accounts? Just to make sure it works correctly?

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.

Amit Gujarathi
Giga Sage
Giga Sage

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