+ Start a Discussion
VICKY_SFDCVICKY_SFDC 

.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.
Best Answer chosen by VICKY_SFDC
ANUTEJANUTEJ (Salesforce Developers) 
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.
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;
                }
            }
        }
        
        
        
    }
}

P.S: Sorry for multiple revisions.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Vikas,

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.
 
trigger acctri on account(before insert)
{
    if(trigger.isbefore && trigger.isinsert)
    {
    set<id> acid= new set<id>();
    for(Account a: trigger.new)
    {
    if(a.field!=null && a.field2!=null) //check all the field values
    {
    a.status__c='completed';
    acid.add(a.id);
    }
    else{
    a.status__c='incomplete';
    }
    }

    list<contact> conlist=[select id,status__c from contact where Accountid in :acid];
    list<contact> conlisttoupdate =new list<contact>();
    for(Contact c: conlist)
    {
    c.status__C='completed';
    conlisttoupdate.add(c);
    }
    if(conlisttoupdate.size()>0) {update conlisttoupdate;}
    }
}

Let me know in case if there are any questions.

Regards,
Anutej
VICKY_SFDCVICKY_SFDC
@ANUTEJ Hi,Your code is worked in accounts,means once all fields are fields then it showing it as cmpleted,,,bt why we need to create status__C in contact and even ,,in contact i need a field which is checked when Account status is completed so how to achieve that
Abhishek BansalAbhishek Bansal
@Anutej: We will never get the Id in a before insert context, so the contact will never be updated.

@Vikas: You can update the account field in before insert and before update. For updating Contacts, you can use after insert and after update.
VICKY_SFDCVICKY_SFDC
@abhishek Bansal   in contact i need a field which is checked when Account status is completed so how to achieve that?
ANUTEJANUTEJ (Salesforce Developers) 
Yeah, sorry for the mistake Vikas as Abhishek correctly mentioned we won't be having the context of the id so it wouldn't be possible to access their id's to fetch contacts related to that account in before trigger, but what we can do is in before trigger check for all the values and update the status depending on the condition and add the id to the set if the all status changes to complete and in after context we can fetch those contact records that are in this set and update that checkbox status_account__c and then add it to a list and update it can you try checking the below code once:
 
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.field!=null && a.field2!=null) //check all the field values as per the conditions
	{
		a.status__c='completed';
		acid.add(a.id);
	}
		else
	{
		a.status__c='incomplete';
	}
	}
	}

	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;}
	}
	}
}
VICKY_SFDCVICKY_SFDC
STILL IN CONTACT FIELD status_account__C is UNCHECKED
ANUTEJANUTEJ (Salesforce Developers) 
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.
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;
                }
            }
        }
        
        
        
    }
}

P.S: Sorry for multiple revisions.
This was selected as the best answer
VICKY_SFDCVICKY_SFDC
which type of field is this   toupdate
ANUTEJANUTEJ (Salesforce Developers) 
it was not a field  I was trying a different logic before the one that I posted and I forgot to remove that debug statement.
VICKY_SFDCVICKY_SFDC
@ANUTEJ  Thanks Bro for help,,for future reference of any other Developer i POST COMPLETE CODE

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);
}
}
ANUTEJANUTEJ (Salesforce Developers) 
Glad to help :)