+ Start a Discussion
RadDude89RadDude89 

Compare value on current contact record against all other contact records

Hi,

We are trying to add logic into our apex class where if the user registers a site and the mobile phone value held at contact is found on any other contact records it will update a field on registration.

So below I have these 2 lists.

listregQry= this contains the mobilephone from the contact being registered.
listregMobile= this contains all Mobile Phone values from our system

listregQry=[Select site__r.Contact__r.MobilePhone from Registrations__c where Id in : lstRegistrations ];
listregMobile=[Select Contact__r.MobilePhone from Site__c];

What I want to do is update a field called Mobile Used to TRUE when the Mobile Phone from listregQry is found on the system on a different contact record.

Does anyone know how I can achieve this?


Thanks in advance.
Apoorv Saxena 4Apoorv Saxena 4
Hi Rad,

Just try this out :
 
Set<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

Let me know if this helps !

Thanks,
Apoorv
RadDude89RadDude89

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.MobilePhone
I'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.

Apoorv Saxena 4Apoorv Saxena 4
Modify the code as below :
 
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

Let me know how it works out for you.


 
RadDude89RadDude89

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?

 

RadDude89RadDude89
Sorry meant to write if the mobile phone number is returned more than once.
Apoorv Saxena 4Apoorv Saxena 4
Try this out:
 
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

Let me know if this works out for you.
RadDude89RadDude89

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);

Apoorv Saxena 4Apoorv Saxena 4
Hi,

Try this out
 
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<Id> siteList = mobMap.get(r.site__r.Contact__r.MobilePhone);
		
		if(siteList.size()>1){
			r.Mobile_Used__c = true; 
		}
	}
}

update listregQry

Please mark this question as Solved if this answers your question, so that others can view it as a proper solution.

Thanks,
Apoorv