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
impalacrazy69impalacrazy69 

soql to pull name/ID from other object to compare

Afternoon,

I need soql help...
My requirements are as follows:

1. Check if it is in a North American Territory (including mid market)

2. If it is:   

a. Look at the Primary QSL.Assigned To field

b. Check that it matches the RSD/RSM on the Lead.Territory - Where im having the most trouble on how to do this

c. If there is a mismatch, display an error message "Please update the lead owner to the correct queue. If this is a PRM lead or you need help, contact salesops@imperva.com."

Below is the code i have but just looking i know its not working. I cant figure how to get my select statements correct. Can anyone help...

// North America Territory assignment
     if (this.lead.Territory__c.Name == 'NA -*'){
     
      RSD = [Select ID,Regional_Sales_Director__c.Name from Territory__c where ID := ];
      primeQ = [Select ID, Assigned_To__c.Name From QSL_Review_Request__c Where ID := ];
     
      if (RSD <> primeQ){
       addError('Please update the lead owner to the correct queue. If this is a PRM lead or you need help, contact salesops@imperva.com');
       isValid = false;
      
      }
     
     }
OyeCodeOyeCode
//Code- Harshit Pandey
//http:www.oyecode.com

Here is how I would design, I rather make a map that queries custome object record that fill in name for that custom objects, this is how I would do it 

Map<id, Territory__c> RSDMap= new Map<id, Lead>( [Select ID,Regional_Sales_Director__c.Name from Territory__c);

Map<id, QSL_Review_Request__c> primeQMap= new Map<id, Lead>( [Select ID,Regional_Sales_Director__c.Name from QSL_Review_Request__c);


//Now i have all id required, i need not to have query second time to salesforce as my map has what I needed
//so bulkyfied my code and save time of repetatively querying cloud

//Now ill design a compare method
public boolean ismatch(String id)
{
    boolean toreturn= false;
    if(RSDMap.containsKey(id)
     return true;
     return toreturn;

}

Now ill go through the code in this fashion
// North America Territory assignment
     if (this.lead.Territory__c.Name == 'NA -*'){
    
    // if its not a match then throw error
    if(!isMatch(primeQ))
     {
       addError('Please update the lead owner to the correct queue. If this is a PRM lead or you need help, contact salesops@imperva.com');
       isValid = false;
    
      }
OyeCodeOyeCode
I can always answer you better if you can share the relationship beteween these custom objects 
impalacrazy69impalacrazy69
The leads are associated with a terriroty we have setup in our territory object. And the QSL review is what is linked to the lead which the review is assigned to a sales person who is also assigned to a terrirtory. The class itself is a validation class that checks to make sure things are in place before the leace can be convereted into a contact or opportunity. Below is the fill code and i highlighted where i am adding in my check.

Thanks for the help on this....

public class ConvertLeadController {
   
    public Lead lead {public get; private set;}
   
    /* CONSTRUCTOR */
    public ConvertLeadController(ApexPages.StandardController controller) {
     this.lead = [SELECT Id, AcceptRequiredForConversionQsl__c, AcceptedForConversionPrm__c, RecordType.Name,
                         Contact_Role__c, Product_Interest__c, OwnerId, PartnerOwnerForConverted__c, Territory__c.Name
                  FROM Lead WHERE Id = :controller.getSubject().Id];
    }
   
    public PageReference validate() {
    
     Boolean isValid = true;
    
     // Not accepted via the PRM process
     if (this.lead.RecordType.Name == 'Deal Registration' && !this.lead.AcceptedForConversionPrm__c) {
            addError('Please approve the PRM request prior to converting this lead.');
            isValid = false;
     }
    
     // PRM lead owned by an active Partner user
     if (this.lead.RecordType.Name == 'Deal Registration') {
     
      List<User> owner = [SELECT Id, IsActive FROM User WHERE Id = :this.lead.OwnerId AND UserRole.PortalType = 'Partner'];
     
      if (owner.size() != 1) {
       addError('PRM leads must be owned by a partner user. Please change the Lead owner to the correct partner before converting.');
       isValid = false;
      } else if (!owner[0].IsActive) {
       addError('The owner of this lead is an inactive partner. Please change the owner to an active partner before converting.');
       isValid = false;
      }
     }
     // North America Territory assignment
     if (this.lead.Territory__c.Name == 'NA -*'){
     
      RSD = [Select ID,Regional_Sales_Director__c.Name from Territory__c where ID := ];
      primeQ = [Select ID, Assigned_To__c.Name From QSL_Review_Request__c Where ID := ];
     
      if (RSD <> primeQ){
       addError('Please update the lead owner to the correct queue. If this is a PRM lead or you need help, contact salesops@imperva.com');
       isValid = false;
      
      }

     
     }
    
     // Not accepted via the QSL process
  if (this.lead.AcceptRequiredForConversionQsl__c) {
    addError('Please accept the QSL Review Request prior to converting this lead.');
    isValid = false;
  }
    
     // Needs contact role
     if (this.lead.Contact_Role__c == null) {
      addError('Please fill out the Contact Role field before converting this lead.');
      isValid = false;
     }
    
     // Needs product interest
        if (this.lead.Product_Interest__c == null) {
            addError('Please fill out the Product Interest field before converting this lead.');
            isValid = false;
        }
       
        // Check if it is owned by an internal user. For internal leads only
        if (this.lead.RecordType.Name != 'Deal Registration') {
         try {
          Group queue = new Group(id=lead.OwnerId);
         } catch (TypeException e) {
          addError('Please change the lead owner to the correct Queue before converting.');
             isValid = false;
         }
        }
    
     if (isValid) {
      // These are pre-conversion actions to make sure the lead is ready to be converted without errors
     
      // For PRM leads, assign the partner owner to a static field. Standard owner gets overwritten by the converting user
      // as a part of the conversion process
      if (this.lead.RecordType.Name == 'Deal Registration') this.lead.PartnerOwnerForConverted__c = this.lead.OwnerId;
     
      // Remove any time based workflows prior to conversion
      // This can leave a hole in some business processes if the user cancels the convert after reaching this page
      this.lead.RemoveTimeBasedWorkflows__c = true;
      update this.lead;
     
      String owner = UserInfo.getFirstName() + '+' + UserInfo.getLastName();
      return new PageReference('/lead/leadconvert.jsp?retURL=/'+ this.lead.Id +
                       '&id='     + this.lead.Id +
                       '&owner_id_lkid='  + UserInfo.getUserId() +
                       '&owner_id_lkold=' + owner +
                       '&owner_id='    + owner);

     } else return null;
    }
   
    private void addError(String msg){
     ApexPages.Message error = new ApexPages.Message(ApexPages.Severity.FATAL, msg);
        ApexPages.addMessage(error); 
    }
}
impalacrazy69impalacrazy69
Both the fields on each object are lookups to the user object.