You need to sign in to do that
Don't have an account?
How To delete the related contact based on updation of a particular field using trigger
trigger CreationOfContact on Account (after insert,after Update) { Map<Id,Decimal> mp=new Map<Id,Decimal>(); list<Contact> lst=new List<Contact>(); for(Account a:Trigger.New) { mp.put(a.id,a.Number_Of_Contacts__c); } if(Trigger.isInsert && Trigger.isAfter) { if(mp.size()>0 && mp!=null) { for(Id a:mp.keyset()) { for(Integer i=1;i<=mp.get(a);i++) { Contact c=new Contact(); c.lastname='Test'+i; c.accountid=a; lst.add(c); } } } } insert lst; if(Trigger.isUpdate && Trigger.isAfter) { for(Account a:Trigger.new) { Decimal a1=trigger.oldmap.get(a.id).Number_Of_Contacts__c; Decimal a2=trigger.newMap.get(a.id).Number_Of_Contacts__c; Decimal a3=0; if(a2>a1) { a3=a2-a1; for(Integer i=1;i<=a3;i++) { Contact c=new Contact(); c.lastname='Update' +i; c.AccountId=a.id; lst.add(c); } } } insert lst; } }
In this trigger i have created a custom field called no.of.contacts on account object, i've the following scenario:
1. When a new account is created and the that field is populated with (say 2) it should create 2 new contacts.
2. When i edit the that field ( say to 5 now before it was 2 ) so the difference of both is now 3, so it should 3 more contacts.
3. when i edit that field to (say to 4 now earlier the new updated value is 5) so the difference is 1 here , therefore 1contact should be deleted.
So, the code i've written works well upto 2nd scenario, the only problem i;m facing now is at 3rd scenario.
Could someone help me out?
All Answers
It works perfectly fine but could you please explain me why have you used,
Map<Id, List<Contact>> acctContacts = new Map<Id, List<Contact>>();
I'm just not able to understand that logic.
Thanks
Just try This Below code, Hope it works for you
Thanks
Akshay
How would i write a trigger for deleting a contact related to that account
E.g: If i created an account with no of contacts field as 3 , 3contacts would be created, now if i go to contacts page and delete 1 of those contacts i want to show to no of contacts field on Account as 2.
How can i write a delete trigger for that on contact?.
// get a map of the accounts with the number of items Map<id,Account> popMap = new Map<id,Account>([select id, Number_Of_Contacts__c from Account where id IN :accountIds]); List<AggregateResult> ars = [SELECT accountId, count(Id) FROM Contact WHERE accountId IN :accountIds]; for (AggregateResult ar : ars) { popMap.get(ar.get('accountId')).Number_Of_Contacts__c = ar.get('expr0'); popsToUpdate.add(popMap.get(ar.get('accountId'))); } update popsToUpdate; }
This gets the statistics, number of Contacts per account, from the Contact object by counting the number of times each Account shows up via the AccountId field in the Contact object.
The above code runs through the statistics and then updates the Number_Of_Contacts__c field of Account with the actual number of Contacts per Account
Simple trigger without using Map
Trigger NmbrofChldrecs on Account (After insert,After Update) {
list<contact> cons= new List<contact>();
list<contact> delcons= new List<contact>();
If(trigger.Isinsert){
for(Account a:trigger.new){
for(integer i=1;i<=a.Child_Contacts__c;i++){
contact c = new contact();
c.Lastname=a.Name+i;
c.Phone = a.Phone;
c.accountid=a.id;
cons.add(c);
}
}
}
if(trigger.isUpdate){
//for(account a:trigger.new){}
for(account acc : [select id, name, Child_Contacts__c, phone, (select id, lastname,Phone from contacts) from account where id in:trigger.new])
{
if(acc.contacts.size()<acc.Child_Contacts__c){
for(integer i=acc.contacts.size();i<acc.Child_Contacts__c;i++){
contact co = new contact();
co.lastname=acc.Name+(i+1);
co.Phone=acc.Phone;
co.accountid=acc.id;
cons.add(co);
}
}
else if(acc.contacts.size()>acc.Child_Contacts__c){
delcons = [select id, lastname,Phone from contact where accountid=:acc.id offset:integer.valueof(acc.Child_Contacts__c) ];
}
}
}insert cons;
delete delcons;
}