You need to sign in to do that
Don't have an account?
munna123
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;
}
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;
}
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;
}
select firstname, lastname,accountId from contacts
Thanks
Shaijan