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
RarLopzRarLopz 

Apex - If one record has Checkbox__c = true , make it true for other

What would be a best solution for this use case: 

If two Contacts have same FirstName, LastName and Email, and one of the Contact Record Custom_CheckBox__c = true, make it true for other record too. 

My appraoch : 
I was thinking, creating a Custom_Formula_Field__c returning text that is a concatenated string 'FirstNamelastNameEmail' 

Then in Developer Console, querying all Contact records that has  same value in Custom_Formula_Field__c

If one has Checkbox_-c = true, make the other record's Checkbox__c = true


Any suggestions?
Best Answer chosen by RarLopz
RarLopzRarLopz
Finally got this to work.. .
I know there are too many nested for, and i will hit governor limits. 
but for now this should suffice. If anyone wants to review my code and provide suggestions, I will be happy to get feedback 
Thanks!!
 
List<Contact> cons = [SELECT Id, Name,DuplicateCheckDSA__c,DSA__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();

     for(Contact con :cons){
		if(conMap.containsKey(con.DuplicateCheckDSA__c)){
            conMap.get(con.DuplicateCheckDSA__c).add(con);
        }else{
			list<Contact> conlist = new list<Contact>();
			conlist.add(con);
			conMap.put(con.DuplicateCheckDSA__c,conlist);
		}

     }
	list<Contact> dsaToTrueList = new list<Contact>();
	
    for(String s: conMap.keySet()){ 
        
        system.debug('This is the list for each key ' +s  + ' >>> ' +conMap.get(s));
        list<Contact> contactlist1 = conMap.get(s); 
        for(Contact c : contactlist1){
            
            system.debug('ContactID and DSA value is : ' + c.id + ' ' +c.DSA__c);
            Boolean mydsacheckbox = contactlist1[0].DSA__c; 
            system.debug('Check the DSA Temporary storage ' +mydsacheckbox);  
            
            if (c.DSA__c == false && mydsacheckbox == true){
                system.debug('TTTTTTTTT ' +c.DSA__c);
                c.DSA__c = true;
                dsaToTrueList.add(c);
                
            } else if (c.DSA__c == true && mydsacheckbox == false ){
                for(integer i = 0; i<contactlist1.size();i++ ) {
                contactlist1[i].DSA__c = true;
                dsaToTrueList.add(contactlist1[i]);
                }
            }
            
        }
    
    }
system.debug('The list size to be updated is ' +dsaToTrueList.size());
for(Contact c1:dsaToTrueList){
    system.debug('yyyyyyyyyyyyyyyy' +c1);
    
}

if(dsaToTrueList.size()>0){
  update dsaToTrueList;
}


 

All Answers

Raj VakatiRaj Vakati
I think you are doing on correct approach .. but when you are doing from the developer console you may have to use  the limited SOQL where condition .. please check 


Please use this formula 

FirstNamelastNameEmail+Chekcbox as a Key  

Otherwise, use the batch apex 
 
List<Contact> cons = [SELECT Id, Name,Custom_Formula_Field__c,Checkbox__c  FROM Contact ]; 

Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();
for(Contact con :cons){

	if(conMap.containsKey(con.Custom_Formula_Field__c){
        conMap.get(con.Custom_Formula_Field__c).add(con);
    }
    else{
        conMap.put(con.Custom_Formula_Field__c,con);
    }
	
}


// Get All the Collection and Update from the conMap

 
RarLopzRarLopz

This is my formula field : Custom_Formula_Field__c

Based on your suggestion this is my formula :  FirstName + LastName + Email + IF(Checkbox__c  , "True", "False")

Output: 
Contact1:  Custom_Formula_Field__c = HarryWoodwardshwoods@email.comFalse,   Checkbox__c   = False
Contact2: Custom_Formula_Field__c = HarryWoodwardshwoods@email.comTrue, Checkbox = True


List<Contact> cons = [SELECT Id, Name,Custom_Formula_Field__c,Checkbox__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();

     for(Contact con :cons){

         if(conMap.containsKey(con.Custom_Formula_Field__c){

             conMap.get(con.Custom_Formula_Field__c).add(con);

            } else{

          conMap.put(con.Custom_Formula_Field__c,con);

           }

     }

    // Get All the Collection and Update from the conMap

    (for String s: conMap.KeySet()){

             /// i don't understand how i can make the  Contact1 records's Checkbox as true

     }




 
Raj VakatiRaj Vakati
like below
List<Contact> cons = [SELECT Id, Name,Custom_Formula_Field__c,Checkbox__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();

     for(Contact con :cons){

         if(conMap.containsKey(con.Custom_Formula_Field__c){

             conMap.get(con.Custom_Formula_Field__c).add(con);

            } else{

					conMap.put(con.Custom_Formula_Field__c,con);

           }

     }

    // Get All the Collection and Update from the conMap
List<Contact> consTemp = new List<Contact>() ; 
for(Contact con : conMap.values()){
	if(!con.Checkbox__c ){
		con.Checkbox__c  =true ; 
		consTemp.add(con);
	}
	
}

update consTemp ;

 
RarLopzRarLopz
Thanks rajvakati, 

However what this does is  makes the Checkbox__c = true for all the contacts. 

So if in the map for a Key I have two records in the list, 

If the Checkbox on both is false, it makes the check box true for both.   here it should not do anything with the CheckBox_c 

if checkbox for one is true and other is false, it makes true for both.      This is fine 

So basically what i am trying to do is, if in the list, Checkbox is  Same for all the values of a particular key. don't do anything 
if Checkbox vlaue in the list for a particular key is differetn, make it true. 

Any suggestions? iwill keep trying too. 
Shubham_KumarShubham_Kumar
Hi RarLopz

Hope this helpls and let me know if you have any questions 
 
List<Contact> cons = [SELECT Id, Name,Custom_Formula_Field__c,Checkbox__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();
List<Contact> updateToTrueCont = new List<Contact>();

     for(Contact con :cons){

         if(conMap.containsKey(con.Custom_Formula_Field__c){
                if(conMap.get(con.Custom_Formula_Field__c).Checkbox__c == true){
                    if(con.Checkbox__c == false){
                        con.Checkbox__c =true;
                        updateToTrueCont.add(con);
                    }
                }
                if(conMap.get(con.Custom_Formula_Field__c).Checkbox__c == false){
                    if(con.Checkbox__c == true){
                        conMap.get(con.Custom_Formula_Field__c).Checkbox__c = true;
                        updateToTrueCont.add(conMap.get(con.Custom_Formula_Field__c));
                    }
                }
             

            }else{

                    conMap.put(con.Custom_Formula_Field__c,con);

           }

     }
     //Now in List<Contact> updateToTrueCont, You have only those contacts which you have to update to true. 
    if(updateToTrueCont.size() > 0){
        update updateToTrueCont;
    }
P.S.:- Mark this as solved if this helps.

Thanks
Shubham Kumar
 
RarLopzRarLopz
Thanks@shubhamkumar

// I tried your logic, it errors out at the following line 

for(Contact con :cons){
        if(conMap.containsKey(con.Custom_Formula_Field__c)){
            system.debug('This is the value ' +conMap.get(con.Custom_Formula_Field__c));   // so far so good... returns contact records in debug log
            
            if(conMap.get(con.Custom_Formula_Field__c).Checkbox__c == true){    // variable does not exist Checkboc__c . 
                 system.debug('-----Checkbox ----- '    +con.Checkbox__c );
                 if(con.Checkbox__c == false){
                     con.Checkbox__c =true;
                     updateToTrueCont.add(con);
                 }
             }

.........

}

 
RarLopzRarLopz
So far what i have got makes me feel confident that i am on the right track.... Here's my actual code. 
Perhaps the actual code here shall make it clear to get some help. I truly appreciate your help everyone!
 
List<Contact> cons = [SELECT Id, Name,DuplicateCheckDSA__c,DSA__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();

     for(Contact con :cons){
		if(conMap.containsKey(con.DuplicateCheckDSA__c)){
            conMap.get(con.DuplicateCheckDSA__c).add(con);
        }else{
			list<Contact> conlist = new list<Contact>();
			conlist.add(con);
			conMap.put(con.DuplicateCheckDSA__c,conlist);
		}

     }
    for(String s: conMap.keySet()){ 
        system.debug('This is the list for each key ' +s  + ' >>> ' +conMap.get(s));
        
// inside this FOR I am getting a list of contacts for each Key. Thats great!!

        // I want to loop through the list for each key and if DSA__c is true for even one record, make DSA__c      // true for other records pertaining to that key
        // e.g s: 0001 {contactId = 003abc, DSA__c = true}, {{contactId = 003pqr, DSA__c = false}} >>> since one record has DSA__c = true , make DSA__c true for ContactId = 003abc
       // e.g s: 0008 {contactId = 003mno, DSA__c = fasle}, {{contactId = 003efg, DSA__c = false}} >>> since both  record has DSA__c = false , nothing changes here
       // // e.g s: 0008 {contactId = 003mno, DSA__c = true}, {{contactId = 003efg, DSA__c = true}} >>> since both record has DSA__c = true , nothing changes here
    
}

 
RarLopzRarLopz
Debug logs to make it clear. 


User-added image
RarLopzRarLopz
So finally this is what i have been trying. 
There's a glitch though... in a list if there are two elements, one has DSA__c = truue, and the other has DSA__c = false, it doesnt update the false to true. 

Can someone please take a look at it. ? Thanks!
 
List<Contact> cons = [SELECT Id, Name,DuplicateCheckDSA__c,DSA__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();

     for(Contact con :cons){
		if(conMap.containsKey(con.DuplicateCheckDSA__c)){
            conMap.get(con.DuplicateCheckDSA__c).add(con);
        }else{
			list<Contact> conlist = new list<Contact>();
			conlist.add(con);
			conMap.put(con.DuplicateCheckDSA__c,conlist);
		}

     }
	list<Contact> dsaToTrueList = new list<Contact>();
	
    for(String s: conMap.keySet()){ 
        
        system.debug('This is the list for each key ' +s  + ' >>> ' +conMap.get(s));
        list<Contact> contactlist1 = conMap.get(s); 
        for(Contact c : contactlist1){
            
            system.debug('ContactID and DSA value is : ' + c.id + ' ' +c.DSA__c);
            Boolean mydsacheckbox = contactlist1[0].DSA__c; 
            system.debug('Check the DSA Temporary storage' +mydsacheckbox);  
            
            if (c.DSA__c != mydsacheckbox){
                system.debug('TTTTTTTTT');
                c.DSA__c = true;
                dsaToTrueList.add(c);
            }
        }
    
    }
system.debug('The list size to be updated is ' +dsaToTrueList.size());
for(Contact c1:dsaToTrueList){
    system.debug('yyyyyyyyyyyyyyyy' +c1);
    
}
if(dsaToTrueList.size()>0){
   update dsaToTrueList;
}

 
RarLopzRarLopz
Finally got this to work.. .
I know there are too many nested for, and i will hit governor limits. 
but for now this should suffice. If anyone wants to review my code and provide suggestions, I will be happy to get feedback 
Thanks!!
 
List<Contact> cons = [SELECT Id, Name,DuplicateCheckDSA__c,DSA__c FROM Contact ];
Map<String ,List<Contact>> conMap = new Map<String ,List<Contact>>();

     for(Contact con :cons){
		if(conMap.containsKey(con.DuplicateCheckDSA__c)){
            conMap.get(con.DuplicateCheckDSA__c).add(con);
        }else{
			list<Contact> conlist = new list<Contact>();
			conlist.add(con);
			conMap.put(con.DuplicateCheckDSA__c,conlist);
		}

     }
	list<Contact> dsaToTrueList = new list<Contact>();
	
    for(String s: conMap.keySet()){ 
        
        system.debug('This is the list for each key ' +s  + ' >>> ' +conMap.get(s));
        list<Contact> contactlist1 = conMap.get(s); 
        for(Contact c : contactlist1){
            
            system.debug('ContactID and DSA value is : ' + c.id + ' ' +c.DSA__c);
            Boolean mydsacheckbox = contactlist1[0].DSA__c; 
            system.debug('Check the DSA Temporary storage ' +mydsacheckbox);  
            
            if (c.DSA__c == false && mydsacheckbox == true){
                system.debug('TTTTTTTTT ' +c.DSA__c);
                c.DSA__c = true;
                dsaToTrueList.add(c);
                
            } else if (c.DSA__c == true && mydsacheckbox == false ){
                for(integer i = 0; i<contactlist1.size();i++ ) {
                contactlist1[i].DSA__c = true;
                dsaToTrueList.add(contactlist1[i]);
                }
            }
            
        }
    
    }
system.debug('The list size to be updated is ' +dsaToTrueList.size());
for(Contact c1:dsaToTrueList){
    system.debug('yyyyyyyyyyyyyyyy' +c1);
    
}

if(dsaToTrueList.size()>0){
  update dsaToTrueList;
}


 
This was selected as the best answer
Shubham_KumarShubham_Kumar
Hi RarLopz

It Seems that i forgot to change the map type that`s why the error occured.
I tried it in my org and it works now.
Could you please try again and let me know.
 

List<Contact> cons = [SELECT Id, Name,First_Last_Name_and_Email__c,Checkbox__c FROM Contact ];
Map<String ,Contact> conMap = new Map<String ,Contact>();
List<Contact> updateToTrueCont = new List<Contact>();

     for(Contact con :cons){

         if(conMap.containsKey(con.First_Last_Name_and_Email__c)){
                if(conMap.get(con.First_Last_Name_and_Email__c).Checkbox__c == true){
                    if(con.Checkbox__c == false){
                        con.Checkbox__c =true;
                        updateToTrueCont.add(con);
                    }
                }
                if(conMap.get(con.First_Last_Name_and_Email__c).Checkbox__c == false){
                    if(con.Checkbox__c == true){
                        conMap.get(con.First_Last_Name_and_Email__c).Checkbox__c = true;
                        updateToTrueCont.add(conMap.get(con.First_Last_Name_and_Email__c));
                    }
                }
             

            }else{

                    conMap.put(con.First_Last_Name_and_Email__c,con);

           }

     }
     //Now in List<Contact> updateToTrueCont, You have only those contacts which you have to update to true. 
    if(updateToTrueCont.size() > 0){
        update updateToTrueCont;
    }

 
RarLopzRarLopz
@shubham_kumar thanks you!!! much appreciated! 
I will test your code and let you know.