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
munna123munna123 

trigger for repopulating previous contacts in account object under particular account

actually i created a custom field "contacts name" in account object. under a particular account if we create contacts they should get displayed in that custom field seperated by ",". similarly if we delete a contact, remaining group of contacts should be displayed under that particular account... i did as below but its not working..... can u correct it plz

trigger tgr_updating_contact_2 on Contact (after insert, after update, after delete) {
  set<id>accid = new set<id>();
    if(trigger.isdelete){
    for(contact c:trigger.old){
        accid.add(c.accountid);
    }
   }
    else{
        for(contact c:trigger.new){
        accid.add(c.accountid);
    }
   }
        list<account>accnt = new list<account>();
        accnt = [select id, contacts_name__c,(select firstname, lastname from contacts) from account where id in:accid];
        for(account a:accnt){
            a.contacts_name__c = null;
        for(contact c:a.contacts){
                if(a.contacts_name__c==null)
                    a.contacts_name__c = c.lastname;
                else
                    a.contacts_name__c = c.lastname+ ','+ a.contacts_name__c;
    }
        for(contact c:a.contacts){
                if(Trigger.isDelete && c.AccountId != null){
              a.contacts_name__c= a.contacts_name__c;
         }
       }
     }
        update accnt;
}
Salesforce DeveloperSalesforce Developer
Try This....

trigger tgr_updating_contact_2 on Contact (after insert, after update, after delete) {
    list<account> Aclist=new list<account>();
  set<id>accid = new set<id>();
    if(trigger.isdelete){
    for(contact c:trigger.old){
        accid.add(c.accountid);
    }
   }
    else{
        for(contact c:trigger.new){
        accid.add(c.accountid);
    }
   }
        list<account>accnt = new list<account>();
        accnt = [select id, contacts_name__c,(select firstname, lastname from contacts) from account where id in:accid];
        for(account a:accnt){
            a.contacts_name__c = null;
        for(contact c:a.contacts){
                if(a.contacts_name__c==null){
                        a.contacts_name__c = c.lastname;
                        Aclist.add(a);
                    }
                else{
                        a.contacts_name__c = c.lastname+ ','+ a.contacts_name__c;
                        Aclist.add(a);
                    }
    }
        for(contact c:a.contacts){
                if(Trigger.isDelete && c.AccountId != null){
              a.contacts_name__c= a.contacts_name__c;
              Aclist.add(a);
         }
       }
     }
        update Aclist;
}
Shaijan ThomasShaijan Thomas
its working for me. Modified SOQL
select firstname, lastname,accountId from contacts  
Thanks
Shaijan
munna123munna123
hiii, my requirement is when i select the required object fom dropdown and click "add" button, i have to get the list of fields of that particular object with checkboxes. when i checkin some fields like phone, name and all then i need to get all the records of that object with the selected field values. i have a program regarding this. but the thing is its not working for custom objects. it is giving some error... and i want to improve my program so that if i checkin and click " view report" button, i have to get the records of that particular object with selected fields and the list of notes and attachments (if present) for all records of that selected object... here is my code vf page:
munna123munna123
hiii , my requirement is when i select the required object fom dropdown and click "add" button, i have to get the list of fields of that particular object with checkboxes. when i checkin some fields like phone, name and all then i need to get all the records of that object with the selected field values. i have a program regarding this. but the thing is its not working for custom objects. it is giving some error... and i want to improve my program so that if i checkin and click " view report" button, i have to get the records of that particular object with selected fields and the list of notes and attachments (if present) for all records of that selected object... here is my code vf page: