1

I have a trigger which inserts merged record under a custom object called Account_merged__c. The trigger is as follows:

trigger AccountMergeTrigger on Account (after delete) {
    List<Account_Merged__c> listAccountBackup = new List<Account_Merged__c>();
    for(Account acct : trigger.old) {
        if(String.isNotBlank(acct.MasterRecordId)) { 
            listAccountBackup.add(new Account_Merged__c(
                Name = acct.Name, 
                Policy_Number__c = acct.Policy_Number__c,
                Account_Code__c = acct.Account_Code__c,
                TaxID__c = acct.TaxID__c
            ));  
        }         
    }
    if(listAccountBackup.size() > 0) {
        insert listAccountBackup;
    }    
}

In the above trigger, the name field will be saved as 'Account A Acquired Account B'. Now, I want to separate the value into 2 fields like field 1 will be having Account A and field 2 will be having Account B leaving out acquired keyword.

I already have formula in place to get this done but I want this to be done with the above trigger itself. Below are the formulas used in field 1 and field 2

field 1:

IF(CONTAINS(Name, "acquired"),
LEFT(Name,Find("acquired", Name)-2),
IF(CONTAINS(Name, "Acquired"),
LEFT(Name,Find("Acquired", Name)-2), ""))

The above formula will cut the left part of acquired word.

field 2:

IF(CONTAINS(Name, "acquired"),
RIGHT(Name,LEN(Name)-Find("acquired", Name)-8),
IF(CONTAINS(Name, "Acquired"),
RIGHT(Name,LEN(Name)-Find("Acquired", Name)-8), ""))

The above formula will cut the right part of acquired word.

Update

Below is the updated trigger suggested by @Manish.

trigger AccountMergeTrigger on Account (after delete) {
    List<Account_Merged__c> listAccountBackup = new List<Account_Merged__c>();
    for(Account acct : trigger.old) {
        if(String.isNotBlank(acct.MasterRecordId)) {
            Account_Merged__c ma = new Account_Merged__c();
            ma.Name = acct.Name;
            ma.Policy_Number__c = acct.Policy_Number__c;
            ma.Account_Code__c = acct.Account_Code__c;
            string s = Acct.Name;
            integer i = s.indexof('Acquired');
            string account2 = s.substring(i+9); 
            string account1 = s.substring(0,i-1);
            system.debug('account2 '+account2); 
            system.debug('account1 '+account1);
            ma.Field_1__c = account1;
            ma.Field_2__c = account2;
            listAccountBackup.add(ma);
        }         
    }
    if(listAccountBackup.size() > 0) {
        insert listAccountBackup;
    }    
}

While merging the duplicate records, I am getting the error as

AccountMergeTrigger: execution of AfterDelete caused by: System.StringException: Ending position out of bounds: -2 Trigger.AccountMergeTrigger: line 12, column 1

Can anyone suggest the changes in my trigger to divide the name string and save it using the trigger itself.

1
  • why you are using substring unnecessarily You can do this via splitting string via 'Aqquire ' keyword Commented May 3, 2020 at 7:34

3 Answers 3

3

Please add null check where ever required this code will split account name and fill value in field1 and field2

trigger AccountMergeTrigger on Account (after delete) {
    List<Account_Merged__c> listAccountBackup = new List<Account_Merged__c>();
    for(Account acct : trigger.old) {
        if(String.isNotBlank(acct.MasterRecordId)) {
            list<string> lstSplittedFields = acct.name.split('Acquired');
            listAccountBackup.add(new Account_Merged__c(
                Name = acct.Name, 
                Policy_Number__c = acct.Policy_Number__c, 
                Account_Code__c = acct.Account_Code__c,
                TaxID__c = acct.TaxID__c,
                FirstField__c=lstSplittedFields[0].trim(),
                SecondField__c= lstSplittedFields[0].trim()
            ));  
        }         
    }
    if(listAccountBackup.size() > 0) {
        insert listAccountBackup;
    }    
}
4
  • using the above suggested code, I'm getting full name in field 1 and field 2. The string is not getting divided
    – Skumar
    Commented May 3, 2020 at 8:05
  • String myString1 = 'Account1 Aqquire Account 2'; String myString2 = 'Aqquire'; list<string> result = myString1.split(mystring2); system.debug(result); Execute this code in developer console its return splitter string Commented May 3, 2020 at 8:11
  • Can you add debug after splitting string i thinkd your account Name does not use Aqquire Keyword Commented May 3, 2020 at 8:12
  • There was a small field error in my code and I corrected it. Thanks @Dhiraj and everyone. It worked :)
    – Skumar
    Commented May 3, 2020 at 8:16
3

You can simply do

List<String> splitValues = acc.Name.split('Acquired');

The list will have 2 values one before acquired accessible via splitValues[0] and other with splitValues[1].

1

You can try something like below

Acct is the instance of new Account_merged__c inserted

string s = Acct.Name;
integer i = s.indexof('Acquired');
if(i != -1) { // If string contains 'Acquired' then only execute below
    string account2 = s.substring(i+9); // 9 = number of characters in 
    acquired + 1 for space
    string account1 = s.substring(0,i-1);
}
system.debug('account2 '+account2); // Print Account B
system.debug('account1 '+account1);//  Print Account A
4
  • My name field will be saved when the record is created using the trigger I mentioned above. So that saved name will be having name like Account A acquired Account B. Account A and Account B is used for example purpose but they will be different names for all records and only acquired word will be common. I want the field to be split based on that word. Hence, along with the name field, I want the field1 and field2 saved using the same after delete trigger. Please suggest
    – Skumar
    Commented May 3, 2020 at 6:49
  • Updated my answer. 's' can be any string , it will always look for 'acquired' in the s and split the string in 2 parts. Commented May 3, 2020 at 6:53
  • I have updated the question with the error I'm getting after updating the trigger with your suggested code. Could you please check the code and let me know if I missed anything here
    – Skumar
    Commented May 3, 2020 at 7:16
  • I don't see you are using the updated code , I see if condition is missing in your code. You are getting this error , if the Acquired is not present in any of the record, i = -1 . Use the updated code. Also, you can use split function as suggested by Manjit Commented May 3, 2020 at 7:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .