I created new triggers as the above ones were not working and tested in my org and they were working fine can you check ones.As per my understanding, the use case was that when a new account along with the related contacts are inserted then the checkbox on the related contact field should be true if the status__c field on the account is complete. trigger actritri on account(before insert,after insert,before update)
{
set<id> acid= new set<id>();
{
if(trigger.isbefore && (trigger.isinsert || trigger.isupdate))
{
for(Account a: trigger.new)
{
if(a.Name!='' ) //check all the field values as per the conditions
{
a.status__c='complete';
acid.add(a.id);
}
else
{
a.status__c='incomplete';
}
}
}
if(trigger.isbefore && trigger.isupdate)
{
list<contact> conlist=[select id,acccount_checked__c from contact where Accountid in :acid];
list<contact> conlisttoupdate =new list<contact>();
for(Contact c: conlist)
{
c.acccount_checked__c=true;
conlisttoupdate.add(c);
}
if(conlisttoupdate.size()>0) {update conlisttoupdate;}
}
}
}
//below contact trigger would be fetching the new contacts and check if the status field is complete if so it would make the checkbox true.
P.S: Sorry for multiple revisions.trigger contritri on Contact (before insert) {
if(trigger.isbefore && trigger.isinsert)
{
set<id> accid = new set<id>();
map<id, list<contact>> accconmap= new map<id, list<contact>>();
for(contact c: trigger.new)
{
accid.add(c.AccountId);
if(accconmap.containsKey(c.AccountId)) {
List<contact> contactl = accconmap.get(c.AccountId);
contactl.add(c);
accconmap.put(c.accountid,contactl);
} else {
accconmap.put(c.AccountId, new List<contact> { c });
}
}
system.debug(accconmap);
for(account a: [select id,status__c from account where id in :accid])
{
if(a.status__c=='complete')
{
for(contact c: accconmap.get(a.id))
{
system.debug('inside the for loop');
c.acccount_checked__c = true;
}
}
}
}
}
11 answers