Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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.
8 answers
  1. Oct 12, 2016, 9:08 AM
    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.

     
  2. Oct 13, 2016, 1:53 PM
    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
  3. Oct 13, 2016, 1:38 PM

    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);
  4. Oct 12, 2016, 9:48 AM
    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.
  5. Oct 12, 2016, 9:16 AM

    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?

  6. Oct 12, 2016, 9:01 AM

    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.

  7. Oct 11, 2016, 4:31 PM
    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
0/9000