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

Need Help to find the Logic using Maps or anything

Hi All ,


I am stuck to impliment a logic... below is the requirment,,

i have 3 objects( Leads , 2 custom(Virtual Numbers and Lead Virtuals)

 I have a campaign and in that campaign i have 1000+ leads......

there is button(visualforce button) on campaign( Assign Numbers) , when i click on the the button...

it will search all the leads in the campaign and  for each lead get his Area code(for eg. if Phone no is((408)512-2626) then get the 408)

For each  Area code eg 408 get the virtual number and put that Virtual no into the one of the custom field on lead object(Virtual_Number__c).

Also get each Associated Lead with Virtual no Id's and create a new lead Virtual.

  if a virtual number of a particular Area code is not avialable then than create a task.


If i impliment this logic for a single lead that works fine but i am getting hard time as bulk asigning


please see the logic below for assigning no to single lead....


Lead l2 = [Select Id,Status,Phone,Virtual_Number__c,FCCampaign__c,
                                  Number_type__c,IsProvisioned__c From Lead where Id = :id];
    if(l2.Status != 'New'){
        ReturnCode = 'Lead Status should be New ';
              return null;
    string temp;
             temp = l2.Phone.substring(0,5);
             temp = temp + '%';
             list<Virtual_Number__c> v1 =    [Select Virtual_Number__c, Virtual_Number_Staus__c, Id
                             From Virtual_Number__c where Virtual_Number__c like :temp
                                      and Virtual_Number_Staus__c = 'Ready to Assign' limit 1];
                      System.debug('After VN Number................' + v1);
         FCCampaign__c F1 = [Select Id,Start_Trial_Date__c from FCCampaign__c where Id = :l2.FCCampaign__c];        
             Lead_Virtual__c vr = new Lead_Virtual__c();                        
                   if (v1.size()>0){
                       l2.Virtual_Number__c = v1[0].Virtual_Number__c;
             l2.Status = 'Trial';
             l2.Provisioning_Status__c = 'Provisioned';
           //  l2.Active__c = true;
             l2.Start_Trial_Date__c = F1.Start_Trial_Date__c;
             l2.IsProvisioned__c = true;
            // l2.End_Trial_Date__c = 14;
             v1[0].Virtual_Number_Staus__c = 'Assigned';
             vr.Lead__c = l2.Id;
             vr.Virtual_Number__c = v1[0].Id;
             vr.Status__c = 'Active';
                       update l2;
                       update v1;
                       insert vr;
                       PageReference detailPage = (new ApexPages.StandardController(L1)).view();
                   return detailPage; 
                   }catch(exception e){
                       System.debug('Error:' +e);
                       Returncode = 'SFDC Error:' +e;
                       return null;
             ReturnCode = 'Virtual Number is not available for lead area code: Please contact Twilio';
             l2.Provisioning_Status__c = 'Pending';
             update l2;
              return null;
public PageReference okay(){
        PageReference detailPage = (new ApexPages.StandardController(L1)).view();
     return detailPage;


Creating task is not impliment till now in above code

Here chalanges in bulk assigning  are that how to track each with virtual number with the lead and also how to create lead virtuals with the particular lead and virtual number.

If i take leads in a list and loop the virtual number with particular area code then it will cross the SOQL limits.

I want to implimnet logic in such a way that i can update approx 800 in a time in 1 shot( as i can use @future in my class)


Any help  will be appriciated




Well typically to avoid running a SOQL query for every record, you write a query using "IN" and passing in a list of values, so one query for all the data you need from that object.


Problem here is you are using LIKE - can't use a list for that parameter, maybe you want a field on Virtual_Number that is the area code, then you can use IN.




yes i tried to do that, but actually i need to look the virtual number according to areacode( there is no other option) i have tried to put all the area code in a map and using the keyset() i can search the virtual numer using IN. but again i need to match this with the particular lead and also i need to make sure that it will not pick the same virtual number again and again to assign.

The above code is working as i am doing for 1 lead.But if i will do for 1000+ leads, it will blow up


Any suggestions!!

Are you saying this is one-to-one? Lead to Virtual Number?
Yes for a particular area code i need to search a virtual number(one to one)

Hi again, here is some partial code to explain how I would attempt what your doing from info you have given.



List<Lead> leads = [SELECT ...];
List<Virtual_Number__c> virtNums = [SELECT...];

List<Lead> leadsToUpdate = new List<Lead>();
List<Virtual_Number__c> vnsToUpdate = new List<Virtual_Number__c>();

for (Lead lead : leads) {
for (Virtual_Number__c vn : virtNums) {
if (vn.Virtual_Number_Status__c == 'Ready to Assign' && vn.Virtual_Number__c.startsWith(lead.Phone.substring(0,5))) {
lead.Virtual_Number__c = vn.Virtual_Number__c;
vn.Virtual_Number_Staus__c = 'Assigned';

update leadsToUpdate;
update vnsToUpdate;




Message Edited by Nick34536345 on 02-12-2010 07:47 AM

Hey, thanks for the sample code... now i got some idea... but also i need to remeber that which Virtual numebr is assignrd and the loop should not pick the same number again n again..


any siggestions

Also if virtual numbers are more than 1000 and the loop cn't find in first 1000 than how can i procced to search the area code for the other virtual number for that area code