0

I have some SF Id (18 char long) coming from a backup system and I have to check if they still exists. Is there a smarter way then do 3 SOQL? Maybe with SOSL?

Something like:

public static validate(Id accountId1, Id accountId2, Id opportunityId1) {
    // check if Account exists with accountId1
    // check if Account exists with accountId2
    // check if Account opportunity with opportunityId1
}
1

Not sure how you are calling your method so you can adapt this as needed:

public static validate(Id accountId1, Id accountId2, Id opportunityId1) {
    Set<ID> tmp = New Set<ID>(accountID1, AccountID2); //Although you should be passing this in
    Map<ID, Account> acctMap = New Map<ID,Account>([Select ID From Account Where ID IN :tmp]);

    //Do your check
    if(acctMap.containsKey(accountID1)) { //DO something for firstID }
    if(acctMap.containsKey(accountID2)) { //DO something for Second }

    Opportunity[] opps = [Select ID From Opportunity Where ID = :opportunityid];
     if(!opps.isEmpty()) { //Opp exists }

}

Now, if you had an external ID field set on those objects with the IDs then you could do it with SOSL but the ID field is not search-indexed so if you are finding the ID field you are stuck with SOQL.

|improve this answer|||||
  • You are not checking opportunityId.. I'd like to check if both 3 Ids are still valid – mcmc Jul 31 '15 at 13:50
  • Updated. I do not think you can do it via SOSL, although I do not use it often, my attempts to use an ID does not work and ID fields are not search-indexed so I do not think you can do it.....See updated code – Eric Jul 31 '15 at 13:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.