I have two fields in my Opportunity object: Phone_1__c & Phone_2__c. Now I have to check to check the whole Org, if there are any opportunities made earlier using any of these two numbers. (Org has more than 53k records).


I added the Phone_1__c & Phone_2__c to the list and performed SOQL using that

[SELECT count() FROM Opportunity
                            WHERE (Phone_1__c IN :oppAddOnPhoneList
                            OR Phone_2__c IN :oppAddOnPhoneList)

oppAddOnPhoneList : list containing Phone numbers of all current Opportunities.

Now the problem is, this code works fine for single opportunity. It will not work for a list<Opportunities> containing new phone number and existing phone number, as the oppAddOnPhoneList always has some existing phone number present. Any suggestions /ideas to bulkify this code?

Note: I will also add other filters in SOQL ( Stage, createddate etc..,) Batch apex will run asynchronously. But I need to throw the error instantly. Read about the @readOnly annotation. But does it need vf page?

  • This query is already bulkified, what I understood is you want to compare List of new phone no with List of existing phone no, is that correct? – sagar maurya Feb 20 '19 at 5:20
  • @sagarmaurya : Yes, I'll be comparing new phone number with existing one. But when passed List<opportunity>, I couldn't separate which phone number among the new is existing and which are not, so that I could throw error to the duplicates alone – Arun Kumar Feb 20 '19 at 5:57
  • You will have to iterate over the each Opportunity with new phone no and compare them with List of existing Opportunity having phone no. – sagar maurya Feb 20 '19 at 6:06
  • Is it the only possible solution?? Because to compare with existing phone number I might have to fetch all phone number from my org ( > 50k, even with all filters & conditions). That's why I tried count() instead – Arun Kumar Feb 20 '19 at 6:10
  • Yes, I can think of this solution. If this is the case, you can use batch class. – sagar maurya Feb 20 '19 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.