You need to sign in to do that
Don't have an account?
.trigger account status changes to completed ,i want checkbox in all the related contact should be checked.
Account picklist filed status__c changes to completed , when all fields are not empty else status__c show as incomplete,,
If status__c changes to completed
,i want checkbox in all the related contact should be checked.
If status__c changes to completed
,i want checkbox in all the related contact should be checked.
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.
//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.
All Answers
Can you try checking the below code once and see if this works, please make changes to the field names as per the names available in the org.
Let me know in case if there are any questions.
Regards,
Anutej
@Vikas: You can update the account field in before insert and before update. For updating Contacts, you can use after insert and after update.
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.
//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.
1)1st trigger On Account where status is Picklist Value(completed and incompleted) AND status_account__C is Checkbox
2)2nd trigger on Contact where status is Picklist Value(completed and incompleted) AND acccount_checked__c is Checkbox
1)
trigger acctri on account(before insert,after insert)
{
set<id> acid= new set<id>();
if( trigger.isinsert)
{
if(trigger.isbefore)
{
for(Account a: trigger.new)
{
if(a.AccountNumber!=null && a.Phone!=null) //check all the field values as per the conditions
{
a.status__c='completed';
acid.add(a.id);
}
else
{
a.status__c='incompleted';
}
}
}
if(trigger.isafter)
{
list<contact> conlist=[select id,status_account__C from contact where Accountid in :acid];
list<contact> conlisttoupdate =new list<contact>();
for(Contact c: conlist)
{
c.status_account__C=true;
conlisttoupdate.add(c);
}
if(conlisttoupdate.size()>0) {update conlisttoupdate;}
}
}
}
2)
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);
list<contact> toupdate= new list<contact>();
for(account a: [select id,status__c from account where id in :accid])
{
if(a.status__c=='completed')
{
for(contact c: accconmap.get(a.id))
{
system.debug('inside the for loop');
c.acccount_checked__c = true;
toupdate.add(c);
}
}
}
system.debug(toupdate);
}
}