2

We need to change currency of around 300 accounts in our Salesforce/Financialforce system. That is not possible if accounts have posted invoices. So I have to clone accounts and adjust currency in new ones (which have no invoices).

Cloning an account is not a problem. My question is what is the best way to clone its related lists of contacts, cases, opportunities ... ?

For cloning the account I wrote classes BatchAccountCloneClass and AutoAccountCloneClass. BatchAccountCloneClass (will be used for a batch later) selects all needed accounts (I have a list of Ids) and calls AutoAccountCloneClass which clones given account. Do I have to write similar classes for each related list or is there a better solution which can clone different object types (account, contact ...)?

public with sharing class BatchAccountCloneClass {
    public BatchAccountCloneClass() {
        List<String> AccIdList = new List<String> { '001O000001PjbxIIAR' ... };
        List<Schema.FieldSetMember> fieldSets = new List<Schema.FieldSetMember>();

        fieldSets.addAll(SObjectType.Account.FieldSets.Account_Information.getFields());

        List<String> fieldNames = new List<String>();

        for(Schema.FieldSetMember f : fieldSets) {
            fieldNames.add(f.getFieldPath());
        }

        String sql = 'SELECT ' + String.join(fieldNames , ',') + ' FROM Account WHERE Id IN (\'' + String.join(AccIdList , '\',\'' ) + '\')';   
        List<Account> sourceAccounts = Database.query(sql);

        for (Account sa : sourceAccounts) {
            AutoAccountCloneClass cc = new AutoAccountCloneClass(sa);
        }

    }
}

public with sharing class AutoAccountCloneClass {
    public AutoAccountCloneClass(Account sourceAccount) {
        Account a = sourceAccount.clone(false, true, false, false);
        ...
    }
}
  • Instead of cloning accounts, can you backup all invoices, delete them, change the currency, then reimport them? Of course you'll want to do certain steps before the reimport like Set Audit Fields for the original CreatedDate, temporarily turn off workflows / automation (using cloudtoolkit.co), etc. – David Cheng Dec 20 '18 at 15:56
  • Thanks, maybe you can give me a hint how to backup/delete/recover invoices for an account automatically with apex? – Obi_77 Dec 21 '18 at 10:27
  • Can't do it with Apex, you need to use Data Loader or similar tool. – David Cheng Dec 21 '18 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.