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
NikithaNikitha 

how to make first and last name as full name using trigger?

Best Answer chosen by Nikitha
Malika Pathak 9Malika Pathak 9
Please Find The Solution. how to make first and last name as full name using trigger?
public class contactFullName {
         public static void forFullName(List<Contact> contactList){
        
        List<Contact> ContactListUpdate=new List<Contact>();
        for(Contact con:contactList){
            Contact conNew=new Contact();
            conNew.FullName__c=con.FirstName+' '+con.LastName;
            conNew.id=con.id;
            ContactListUpdate.add(conNew);
        }
        if(ContactListUpdate.size()>0){
            system.debug('inside '+ContactListUpdate);
            update ContactListUpdate;
            
        }
    }
    
}

//Trigger

trigger ContactTrigger on Contact (After Insert) {
 if(trigger.isAfter && trigger.isInsert){
        contactFullName.forFullName(trigger.new);
    }
}


You need to create custem field on Contact i.e FullName.

I hope it will help you.If it will help you please mark best answer.

Thanks

All Answers

Malika Pathak 9Malika Pathak 9
Please Find The Solution. how to make first and last name as full name using trigger?
public class contactFullName {
         public static void forFullName(List<Contact> contactList){
        
        List<Contact> ContactListUpdate=new List<Contact>();
        for(Contact con:contactList){
            Contact conNew=new Contact();
            conNew.FullName__c=con.FirstName+' '+con.LastName;
            conNew.id=con.id;
            ContactListUpdate.add(conNew);
        }
        if(ContactListUpdate.size()>0){
            system.debug('inside '+ContactListUpdate);
            update ContactListUpdate;
            
        }
    }
    
}

//Trigger

trigger ContactTrigger on Contact (After Insert) {
 if(trigger.isAfter && trigger.isInsert){
        contactFullName.forFullName(trigger.new);
    }
}


You need to create custem field on Contact i.e FullName.

I hope it will help you.If it will help you please mark best answer.

Thanks

This was selected as the best answer
SwethaSwetha (Salesforce Developers) 
HI Swathi,
When you can accomplish this using click tools that salesforce offers out of the box, is there a reason to go with trigger?

You can create a simple formula field like below on your object (say Opportunity)

Opportunity__r.Owner.FirstName + " " + Opportunity__r.Owner.LastName

Related: https://trailblazers.salesforce.com/answers?id=90630000000h7yJAAQ

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
ravi soniravi soni

Hi Swathi,
your requirment is good but you can fill fullName using formula instead of trigger. but if you want to write a trigger for it then you should try following trigger.

trigger contactTriggerForFullName on Contact (before insert, before update) {
    
    if(trigger.isBefore){
        if(trigger.isInsert || trigger.isUpdate ){
        
        for(contact con : trigger.new){
            con.FullName__c = con.FirstName + ' ' + con.LastName;
            }
        }
        }
}
let me know if it's helps you and close your query marking it as solved. so that it can help to others.
NikithaNikitha
Thank you so much!!
ravi soniravi soni

hi Swathi,
if it helped you then please marking it as best so that it can help to others.
Thank You