function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nagendra kumar 21nagendra kumar 21 

Update the parent field value based on child field value using workflows/Apex

Hello Everyone,

I would like to know what should i use update a field on Account object based on chiled objects fields on account.

I Have created a Checkbox field on Account, and it needs to be True if 
account division field is  'xx' and MFD field (checkbox) is 'False' then the fiel on account which i created should be True(check) and they should no longer show up as a 'abc customer' 
, If not false(uncheck)..

Please suggest me how can i do this.. If i need to write a code for this please provide a smaple code for it..
Any suggestions is appriceated.


Thanks,
Nag
Kevin CrossKevin Cross
Have you tried a formula field?  You then make the formula of your checkbox  AND( $Organization.Division = 'xx',  {MFD condition} ).  Not sure what the MFD field is but hope you get the idea.  
SandhyaSandhya (Salesforce Developers) 

Hi Nagendra kumar,

You can achieve this using formula field as well.
Please follow below steps.

1.create custom field some xxx with formula as datatype and return type as checkbox and in formula editor use below formula
AND (accountdivision='xx',MFD=flase)

Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, u can give me kudos.
 
Thanks and Regards
Sandhya 

 
nagendra kumar 21nagendra kumar 21
Thanks @Kevin MFD is a custom filed that created under Child Object
nagendra kumar 21nagendra kumar 21
Thanks Sandhya, I have tried it but it dint worked.. i might missing something from you're answer. 
 
Kevin CrossKevin Cross
You could try a rollup summary field to find the MAX value of MFD, which if it behaves the way I think is TRUE if any of the children are TRUE or FALSE otherwise.  I will see if you can simulate the rollup in one formula.  Otherwise, my suggestion is to create the rollup field to show MFD at the Account level, then the formula AND( $Organization.Division = 'xx',  $Account.MFD ) should get you the correct answer.

I will post back when I get a chance to confirm in development environment.
Kevin CrossKevin Cross
Okay.  I checked and do not see child objects in the regular formula, so think you will need to go the rollup summary route.  Just setup MFD field at Account level to rollup your child module with MAX on the MFD field.  Then in your formula, use formula similar to below, noting you can select Division from under Account if you created a custom field there (or shows up different for you).  In my version, Division shows up under $Organization.
AND( $Organization.Division = 'xx', NOT(MFD__c) )
nagendra kumar 21nagendra kumar 21

Thanks Kevin for you suggestions, But i actually went through Code.

 {
        set<Id> SetAccIds = new set<Id>();
        map<Id, id> mapAccount = new map<Id, id>();
        for( Account_Salesview__c acsv : lstSalesViews )
        {
            if( acsv.Account__c != null )
            {
                setAccIds.add(acsv.Account__c);
            }
        }
        for( Account_Salesview__c obj : [Select SalesView__r.Sales_Division__c, AccountDivision__c, Marked_for_Deletion_Flag__c, Account__c FROM Account_Salesview__c WHERE Account__c IN : setAccIds] )
        {
            if(obj.AccountDivision__c =='CE' && obj.Marked_for_Deletion_Flag__c == False )
            {
                mapAccount.put(obj.Account__c,obj.Account__c);
            }
            
        }
        Account[] lstAccounts = new Account[]{};
        for( Account objAcc : [Select Id, isWater__c FROM Account WHERE RecordType.Name = 'Water' AND Id IN :mapAccount.keyset()] )   
        {            
            objAcc.isWater__c = true;
            //objAcc.Freight_Terms__c = string.JOIN(mapAccount.get( objAcc.Id ), ';');
            lstAccounts.add(objAcc);  
        }
        if( !lstAccounts.isEmpty() )
        {
            update lstAccounts;
        }
        
    }
    
    public static void deleteIsWaterFieldOnTheAccount(Account_Salesview__c[] lstSalesViews )
    {
        set<Id> SetAccIds = new set<Id>();
        map<Id, id> mapAccount = new map<Id, id>();
        map<Id, Integer> deletedAccountIds = new map<Id, Integer>();
        map<Id, Integer> allAccountIds = new map<Id, Integer>();
        List<Id> lstIds = new List<Id>();
        for( Account_Salesview__c acsv : lstSalesViews )
        {
            if( acsv.Account__c != null && acsv.AccountDivision__c =='CE' && acsv.Marked_for_Deletion_Flag__c == False )
            {
                setAccIds.add(acsv.Account__c);
                if(!deletedAccountIds.containsKey(acsv.Account__c))
                {
                    deletedAccountIds.put(acsv.Account__c,1);
                }
                else{
                    Integer value=deletedAccountIds.get(acsv.Account__c);
                    ++value;
                    deletedAccountIds.put(acsv.Account__c,value);
                }
            }
        }
        for( Account_Salesview__c obj : [Select SalesView__r.Sales_Division__c, AccountDivision__c, Marked_for_Deletion_Flag__c, Account__c FROM Account_Salesview__c WHERE Account__c IN : setAccIds] )
        {
           if( obj.Account__c != null && obj .AccountDivision__c =='CE' && obj .Marked_for_Deletion_Flag__c == False )
            {
                //setAccIds.add(acsv.Account__c);
                if(!allAccountIds.containsKey(obj.Account__c))
                {
                    allAccountIds.put(obj.Account__c,1);
                }
                else{
                    Integer value=allAccountIds.get(obj.Account__c);
                    ++value;
                    allAccountIds.put(obj.Account__c,value);
                }
            }
            else
            {
                if(!allAccountIds.containsKey(obj.Account__c))
                {
                    allAccountIds.put(obj.Account__c,0);
                }
            }            
        }

Kevin CrossKevin Cross
Wasn't sure how comfortable you were with Apex.  Glad you figured out a way to do this that meets your needs and glad you shared for other readers.  I am a programmer, so love code solutions personally.  Just never want to force things that way as not everything is a nail that needs a hammer. *smile*