Modify the code as below :
Let me know how it works out for you.Set<String> mobSet = new Set<String>();
for(Site__c s:listregMobile){
mobSet.add(s.Contact__r.MobilePhone); // Adding mobile phone present in Site records to a Set.
}
for(Registrations__c r:listregQry){
if(mobSet.contains(r.site__r.Contact__r.MobilePhone)){ // Checking if the mobile phone present in Set matches with any mobile phone from Registration
r.Mobile_Used__c = true; // If matched, then set Mobile Used for that registration record to true;
}
}
update listregQry// update the list outside the for loop to commit data to database
Hi,Try this out
Please mark this question as Solved if this answers your question, so that others can view it as a proper solution.Thanks,ApoorvMap<String,List<Id>> mobMap = new Map<String,List<Id>>();
for(Site__c s:listregMobile){
if(mobMap.containsKey(s.Contact__r.MobilePhone)){
List<Id> siteIdList = mobMap.get(s.Contact__r.MobilePhone);
siteIdList.add(s.Id);
mobMap.put(s.Contact__r.MobilePhone,siteIdList);
} else {
mobMap.put(s.Contact__r.MobilePhone,new List<Id> {s.id});
}
}
for(Registrations__c r:listregQry){
if(mobMap.containsKey(r.site__r.Contact__r.MobilePhone)){
List<Id> siteList = mobMap.get(r.site__r.Contact__r.MobilePhone);
if(siteList.size()>1){
r.Mobile_Used__c = true;
}
}
}
update listregQry
Hi Apoorv,
Apologies for the delay - When I add in the code below I get the error message :Illegal assignment from List<Id> to List<Site__c>
for(Registrations__c r:listregQry){ if(mobMap.containsKey(r.site__r.Contact__r.MobilePhone)){ List<Site__c> siteList = mobMap.get(r.site__r.Contact__r.MobilePhone);Try this out:
Let me know if this works out for you.Map<String,List<Id>> mobMap = new Map<String,List<Id>>();
for(Site__c s:listregMobile){
if(mobMap.containsKey(s.Contact__r.MobilePhone)){
List<Id> siteIdList = mobMap.get(s.Contact__r.MobilePhone);
siteIdList.add(s.Id);
mobMap.put(s.Contact__r.MobilePhone,siteIdList);
} else {
mobMap.put(s.Contact__r.MobilePhone,new List<Id> {s.id});
}
}
for(Registrations__c r:listregQry){
if(mobMap.containsKey(r.site__r.Contact__r.MobilePhone)){
List<Site__c> siteList = mobMap.get(r.site__r.Contact__r.MobilePhone);
if(siteList.size()>1){
r.Mobile_Used__c = true;
}
}
}
update listregQry
Sorry meant to write if the mobile phone number is returned more than once. Apoorv, that works now thanks.
I just need to add in another condition to ensure that the mobile phone number is returned more than - do you know how I would add that?
Hi Apoorv,
Thanks for the reply however when I try to compile the class I get the error message
Variable does not exist: Contact__r.MobilePhoneI'm getting that error from this line:mobSet.add(Contact__r.MobilePhone);
I also need to add into my condition that the total number of mobile phones found is greater than 1 (to exclude the one that we are currently registering.
Hi Rad,Just try this out :
Let me know if this helps !Thanks,ApoorvSet<String> mobSet = new Set<String>();
for(Site__c s:listregMobile){
mobSet.add(Contact__r.MobilePhone); // Adding mobile phone present in Site records to a Set.
}
for(Registrations__c r:listregQry){
if(mobSet.contains(r.site__r.Contact__r.MobilePhone)){ // Checking if the mobile phone present in Set matches with any mobile phone from Registration
r.Mobile_Used__c = true; // If matched, then set Mobile Used for that registration record to true;
}
}
update listregQry// update the list outside the for loop to commit data to database
8 answers