function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rupeshk67Rupeshk67 

Wanted to know about Split function and its uses

Hi Guys,
I am trying to write a trigger on below reuirements and stuck at one point(how to add a word in between last name of a contact record once it is created).

Reuirements:
Write a trigger on Account object.
Criteria:- 
  1. Type:- “Customer
  2. Account Name end with “Your Name” . Example , “Telestra Rupesh”
Action on Trigger:-  
On Creation of account record, it should create one associated Contact record whenever above criteria meet.  
Example :- “Telestra Contact Rupesh

Please note: word 'contact' should come in middle of account name


Thanks,
Rupesh
Best Answer chosen by Rupeshk67
Suraj Tripathi 47Suraj Tripathi 47
Hi RupeshK67,
You can take reference from the below code and you can use substringBefore and substringAfter method.
for(Account a:acclist){
            Contact con      = new Contact();
            if(a.Name.endsWith('rupesh') && a.Type.Contains('Customer')){
                con.LastName = a.name.substringBefore(' ')+' contact '+a.name.substringAfter(' ');
                con.AccountId = a.Id;
                  conList.add(con);
            }
        }

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

All Answers

Rupeshk67Rupeshk67
my code :
class:
public class AccountTriggerHandler {
    public static void createContacts(List<Account> acclist){
        
        list<Contact> conList= new list<Contact>();
        for(Account a:acclist){
            Contact con      = new Contact();
            if(a.Name.endsWith('rupesh') && a.Type.Contains('Customer')){
                con.LastName = a.name.substring(a.Name.indexOf(''))+ ' Contact';
                con.AccountId = a.Id;
                  conList.add(con);
            }
        }
        if(conList!=null){
            insert conList;
        }
        
    }
}

it adds 'contact' at last but not in middle of contact name. Please help me to resolve it.
Thanks,
Suraj Tripathi 47Suraj Tripathi 47
Hi RupeshK67,
You can take reference from the below code and you can use substringBefore and substringAfter method.
for(Account a:acclist){
            Contact con      = new Contact();
            if(a.Name.endsWith('rupesh') && a.Type.Contains('Customer')){
                con.LastName = a.name.substringBefore(' ')+' contact '+a.name.substringAfter(' ');
                con.AccountId = a.Id;
                  conList.add(con);
            }
        }

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
This was selected as the best answer
Rupeshk67Rupeshk67
Hi Suraj,
Thanks for your help, it is working as expected.
Regards,
Rupesh
ravi soniravi soni
Hi Rupesh,
I think this is the good way.
public class addWordMiddleOfLastName {
public static void createContacts(List<Account> acclist){
        
        list<Contact> conList= new list<Contact>();
    
        for(Account a:acclist){
            Contact con = new Contact();
            
              if(a.Name.endsWith('rupesh') && a.Type.Contains('Customer')){
                list<string> lstSplitData = a.name.split(' ');
                string sName = a.name.substringBefore(' ')+' Contact '+a.name.substringAfter(' ');
                system.debug('sName : ' + sName);
                con.LastName = sName;
                con.AccountId = a.Id;
                  conList.add(con);
             
            }
            
        }
        if(conList!=null){
            insert conList;
        }
        
    }
}

let me know if it helps you by marking it as best answer.
Thank you
Rupeshk67Rupeshk67
Hi Veer,
when i use above code it works only when account name is like 'test rupesh'.
If i mention account name as ' test techbook comp rupesh' then contact name appears as 'test contact techbook' and other words disappear.
Kinldy help.
Thanks !