You need to sign in to do that
Don't have an account?
RarLopz
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?
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?
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!!
All Answers
Please use this formula
FirstNamelastNameEmail+Chekcbox as a Key
Otherwise, use the batch apex
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
}
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.
Hope this helpls and let me know if you have any questions
P.S.:- Mark this as solved if this helps.
Thanks
Shubham Kumar
// 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);
}
}
.........
}
Perhaps the actual code here shall make it clear to get some help. I truly appreciate your help everyone!
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!
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!!
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.
I will test your code and let you know.