+ Start a Discussion
Puja khetanPuja khetan 

apex class for concatnate subject and Account name

All ,my requirement is on event object .
when any event is created 
Subject should be Account name + subject name .
I tried using process builder ,on process builder that is working in case of single event however in case of recurrence event ,on child events ,account names are getting duplicated so some once can help for any other solution is respective of Development ?
 
Deepali KulshresthaDeepali Kulshrestha
Hi Puja,

This is the apex code for concatenating the last name of the contact to its account name.
Let's suppose if you create A Contact whose FirstName='Prakhar' and LastName='Saxena' and the Accountname ='Tata', Then Account name must become 'TataSaxena'.


public class changeName {

    public  static void firstmethod(List<Contact> con)
        {
            // Class for adding contact
            Set<Id> idset2 = new Set<Id>();
            for(Contact con2 : con)
                {
                    idset2.add(con2.Id);
                }

            List<Contact> conlist2 = new List<Contact>();
            conlist2 = [select Id,Name,AccountId ,LastName ,FirstName from Contact where Id IN  : idset2];
             Set<Id> idset = new Set<Id>();
            for(Contact cono : con)
                {
                    idset.add(cono.AccountId);
                }
            List<Account> acclist = new List<Account>();
            acclist = [select Name from Account where Id IN  : idset];


            for (Account acc : acclist)
                {
                    for(Contact con2 : conlist2)
                        {
                            if(acc.Id == con2.AccountId)
                                {
                                    acc.Name = acc.Name+con2.LastName;
                                }
                        }

                }

            update acclist;


            
        }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha