You need to sign in to do that
Don't have an account?
Hm_invi
Hi,
Iam new to apex. this is one of the trigger scenario bothering me.
Please help me with this.
I have a status checkbox field on accounts.
status picklist field with "open","closed" on contacts.
1) if status="closed" in all the related contacts then checkbox on account should be true.
2) if status="open" in any one of the related contacts then checkbox on account should be false.
-------------------------------------------------------------------------------------------
Trigger:
trigger contactTrigg on contact(after insert, after update, after delete, after undelete){
if(trigger.isInsert || trigger.isUpdate){
accProgress.method1(trigger.new,trigger.old);
}
}
-------
Handler:
public class accProgress{
public static void method1(list<contact> newInsert, list<contact> oldInsert){
set<id> conSet = new set<id>();
for(contact con1 : newInsert){
if(con1.Accountid != null){
conSet.add(con1.accountid);
}
}
for(contact con2 : oldInsert){
conSet.add(con2.accountid);
}
List<account> accList =[SELECT id, progress_field__c,(SELECT id, progress_field_contacts__c FROM contacts) FROM account WHERE id IN :conSet];
if(accList != null){
for(account acc : accList){
for(contact con : acc.contacts){
if(con.progress_field_contacts__c == 'open' ){
acc.progress_field__c = false;
}
else{
acc.progress_field__c = true;
}
}
}
update accList;
}
}
---------------------------------------------------------------------------------------------
1st one is working. i.e, whenever i make all related contacts "close" . the check box is getting checked(true).
2nd one is where iam failing. when i'm changing any contacts picklist values to open the account status checkbox is not unchecking(false).
please help me.
trigger to check and uncheck parent checkbox field values based on child picklist values
Hi,
Iam new to apex. this is one of the trigger scenario bothering me.
Please help me with this.
I have a status checkbox field on accounts.
status picklist field with "open","closed" on contacts.
1) if status="closed" in all the related contacts then checkbox on account should be true.
2) if status="open" in any one of the related contacts then checkbox on account should be false.
-------------------------------------------------------------------------------------------
Trigger:
trigger contactTrigg on contact(after insert, after update, after delete, after undelete){
if(trigger.isInsert || trigger.isUpdate){
accProgress.method1(trigger.new,trigger.old);
}
}
-------
Handler:
public class accProgress{
public static void method1(list<contact> newInsert, list<contact> oldInsert){
set<id> conSet = new set<id>();
for(contact con1 : newInsert){
if(con1.Accountid != null){
conSet.add(con1.accountid);
}
}
for(contact con2 : oldInsert){
conSet.add(con2.accountid);
}
List<account> accList =[SELECT id, progress_field__c,(SELECT id, progress_field_contacts__c FROM contacts) FROM account WHERE id IN :conSet];
if(accList != null){
for(account acc : accList){
for(contact con : acc.contacts){
if(con.progress_field_contacts__c == 'open' ){
acc.progress_field__c = false;
}
else{
acc.progress_field__c = true;
}
}
}
update accList;
}
}
---------------------------------------------------------------------------------------------
1st one is working. i.e, whenever i make all related contacts "close" . the check box is getting checked(true).
2nd one is where iam failing. when i'm changing any contacts picklist values to open the account status checkbox is not unchecking(false).
please help me.
Trigger change
Handlet Change:
All Answers
acc.progress_field__c = false;
Like below :
acc.progress_field__c = false;
break;
I little tweaked the code
This is the same Made changes here
Let me know it helped
acc.progress_field__c = false;
Like below : the above correction works fine but it does not in the below case:
i have three related contacts.
for one of the contact picklist field, progress_field_contacts__c = 'open' and this contact is deleted.
for the remaining 2 contacts, progress_field_contacts__c = 'close'.
now the account checkbox field, progress_box__c = 'false'.
but it should be checked as 'true'.
how can i include this after delete event into the same class.
You can add below condition in the if statement of your trigger like below for delete event:
if(trigger.isInsert || trigger.isUpdate || trigger.isDelete){
Trigger change
Handlet Change:
Any Update?