You need to sign in to do that
Don't have an account?
Abhishek Khosla
Not able to able to understand how to write this trigger
My previous problem is not resolved I have a Custom filed in accounts which is name as No of contacts now i have to write trigger after update which follows the logic of if I put no of contacts more than the present value then the difference should create a contact( for ex Old value of contact = 8 new value 10 then 2 new contacts should be created ) if ( Old valve of contact = 10 and new value = 8 then 2 contact should be deleted ) how can I achieve this. I wrote the below trigger but it is not working correctly ( if old was 12 and new value 10 it is giving me an exception error ) ( if old value 12 and new value is 13 it creates 1 extra contact but if i update again the new value to 12 all contact gets deleted)
Ideal i want the same no of contacts should be there in the contact related list as per the No.of contacts mentioned in the account custom filed value
trigger trigger17 on Account (after update) {
for(Account a:trigger.new){
if(trigger.oldmap.get(a.id).Number_of_contacts__c <a.Number_of_contacts__c){
System.debug(a.Number_of_contacts__c- trigger.oldmap.get(a.id).Number_of_contacts__c);
for(Decimal i=1;i<=(a.Number_of_contacts__c- trigger.oldmap.get(a.id).Number_of_contacts__c);i++){
contact c = new contact();
c.AccountId =a.Id;
c.LastName = a.Name;
Insert c;
}
}
if(trigger.oldmap.get(a.id).Number_of_contacts__c >a.Number_of_contacts__c){
List<Contact> con = [Select id,name,accountid from contact where accountid =: a.id];
System.debug(con);
Decimal Diff = trigger.oldmap.get(a.id).Number_of_contacts__c - a.Number_of_contacts__c;
System.debug(Diff);
For(Contact c : Con){
for(Integer i = 1 ; i<= Diff; i++){
Delete c;
}
}
}
}
}
I also want the reverse if an contact related to that particular ID is deleted then in the No of Contact filed it should reduce if any new contact created on the same account then the no of contact field should increase
Ideal i want the same no of contacts should be there in the contact related list as per the No.of contacts mentioned in the account custom filed value
trigger trigger17 on Account (after update) {
for(Account a:trigger.new){
if(trigger.oldmap.get(a.id).Number_of_contacts__c <a.Number_of_contacts__c){
System.debug(a.Number_of_contacts__c- trigger.oldmap.get(a.id).Number_of_contacts__c);
for(Decimal i=1;i<=(a.Number_of_contacts__c- trigger.oldmap.get(a.id).Number_of_contacts__c);i++){
contact c = new contact();
c.AccountId =a.Id;
c.LastName = a.Name;
Insert c;
}
}
if(trigger.oldmap.get(a.id).Number_of_contacts__c >a.Number_of_contacts__c){
List<Contact> con = [Select id,name,accountid from contact where accountid =: a.id];
System.debug(con);
Decimal Diff = trigger.oldmap.get(a.id).Number_of_contacts__c - a.Number_of_contacts__c;
System.debug(Diff);
For(Contact c : Con){
for(Integer i = 1 ; i<= Diff; i++){
Delete c;
}
}
}
}
}
I also want the reverse if an contact related to that particular ID is deleted then in the No of Contact filed it should reduce if any new contact created on the same account then the no of contact field should increase
Please mark this as closed if this solves your issue. Thanks
All Answers
You can follow below piece of code to acheive your requirement. Let me know if you need further help.
if(fieldValue < numberOfContacts){
// delete contacts - implement logic here
Decimal diff = numberofcontacts - fieldValue;
System.debug(Diff);
for(Integer i =1; i<=diff;i++){
Contact c = new Contact();
c.id = acc.contacts.id;
Delete c;
Error : A non foreign key field cannot be referenced in a path expression: Contacts
Please mark this as closed if this solves your issue. Thanks