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
Gopal ChatGopal Chat 

Hi,this is the trigger question,working on insert but not working on delete(before or after) and there is no error message showing

On Contact to update the Account Name field with contact Last Name, concatenated in the end. Example: If you Create A Contact whose FirstName='Sarthak' and LastName='Saxena' and the Accountname ='Tata', Then Account name must become 'TataSaxena'. 
Update the above trigger to delete the Last Name from the Account field when a Contact is deleted without deleteing the other Last name.



trigger AddlastNmaeAccount on Contact (after insert,before delete) 
{
    if(trigger.isInsert)
    {
    set<id> s=new set<id>();
    list<Account> listaccount=new list<Account>();
    for(contact con:trigger.new)
    {
        s.add(con.accountId);
    }
    Map<id,account> mv=new Map<id,account>([select id,name from account where id IN:s]);
    for(contact cont:trigger.new)
    {
        Account a=new Account();
        a.Id=mv.get(cont.AccountId).id;
        a.name=mv.get(cont.AccountId).Name+cont.LastName;
        listaccount.add(a);
    }
    Update listaccount;
    }
    if(Trigger.isDelete)
    {
     set<id> s1=new set<id>();
     list<Account> listaccount=new list<Account>();
     for(Contact oContact : Trigger.Old)
     {
         if(oContact.lastName!= null) 
         s1.add(oContact.accountId);
     }
     Map<id,account> mv=new Map<id,account>([select id,name from account where id IN:s1]);
     for(contact cont:trigger.new)
     {
        Account a=new Account();
        a.Id=mv.get(cont.AccountId).id;
        a.name=a.name.replace(cont.LastName,a.name);

        listaccount.add(a);
     }
        update listaccount;
    }
}
v varaprasadv varaprasad
Hi Gopal,

Try once below snippet code.and use after delete event.
 
if(Trigger.isDelete)
    {
     
     list<Account> listaccount=new list<Account>();
     for(Contact oContact : Trigger.Old)
     {
         if(oContact.accountId != null){
		 
		Account a=new Account();
        a.Id=oContact.accountId;
        a.name=a.name.replace(cont.LastName,a.name);
		  listaccount.add(a);
		 } 
        
		 
		 
		 
     }
	 
	  update listaccount;

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com




 
v varaprasadv varaprasad
 a.name=a.name.replace(oContact .LastName,a.name);
Gopal ChatGopal Chat
thanks,but i want this from map
v varaprasadv varaprasad
Hi Gopi,

Then use your code only it will work change event to after delete.
 
Map<id,account> mv=new Map<id,account>([select id,name from account where id IN:s1]);
     for(contact cont:trigger.old)
     {
        Account a=new Account();
        a.Id=mv.get(cont.AccountId).id;
        a.name=a.name.replace(cont.LastName,a.name);

        listaccount.add(a);
     }
        update listaccount;
    }




Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com