+ Start a Discussion
SeanCenoSeanCeno 

Owner__r Reference Not Working

Our org has a search controller that pulls contacts by zipcode and city. I am trying to add the ability to search by State and Contact Owner. The class compiles and the test class passes with 80% coverage (not great, I know). But when I try to deploy into production, it's not recognizing Owner__r.Name. Can someone look into my code to see how I would properly reference the Contact Owner as another viable search option?? If the VisualForce page is needed, it can be provided.

 

Error:

 

Test_TerritorySearchController.testController() Class 167   Failure Message: "System.QueryException: Didn't understand relationship 'Owner__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.", Failure Stack Trace: "Class.TerritorySe...

 

Class:

 

public with sharing class TerritorySearchController
{
    public String searchType { public get; public set; }  
    public String searchValue { public get; public set; }
    public String zipcode { public get; public set; }
    public String state { public get; public set; }
    public String owner { public get; public set; }
    public String contactType { public get; public set; }  //Contact_Type__c
    public String statusType { public get; public set; } //Producer_Status__c / Prospect_Status__c
    
    public String citySearch { public get; public set; }
    public String citySearchResults { public get; public set; }
    public String citySearchSelected { public get; public set; }
    
    public String stateSearch { public get; public set; }
    public String stateSearchResults { public get; public set; }
    public String stateSearchSelected { public get; public set; }
    
    public String ownerSearch { public get; public set; }
    public String ownerSearchResults { public get; public set; }
    public String ownerSearchSelected { public get; public set; }
    
    public List<Contact> reps { public get; private set; }
    public Boolean mapView { public get; private set; }
    
    public TerritorySearchController()
    {
        this.reps=new List<Contact>();
        this.searchType = getParam('searchType', 'zipcode');
        this.searchValue = getParam('searchValue', '');
        this.zipcode = getParam('zipcode', '');
        this.state = getParam('state', '');
        this.owner = getParam('owner', '');
        this.contactType = getParam('contactType', '');
        this.statusType = getParam('statusType', '');
        this.citySearch = getParam('citySearch', '');
        this.citySearchResults = '';
        this.citySearchSelected = '';
        this.stateSearch = getParam('stateSearch', '');
        this.stateSearchResults = '';
        this.stateSearchSelected = '';
        this.ownerSearch = getParam('ownerSearch', '');
        this.ownerSearchResults = '';
        this.ownerSearchSelected = '';
        this.mapView = getBooleanParam('mapView', false);
    }
    
    public String getParam(String name, String defaultValue) {
        String value = ApexPages.currentPage().getParameters().get(name);
        return value == null ? defaultValue : value;
    }
    
    public Boolean getBooleanParam(String name, Boolean defaultValue) {
        String value = ApexPages.currentPage().getParameters().get(name);
        return value == null ? defaultValue : 'true'.equalsIgnoreCase(value);
    }
    
    public PageReference processSearch()
    {
        PageReference p=null;
        
        if (this.searchType=='city')
            p=searchCities();
        else if(this.searchType=='state')
                p=searchStates();
        else if(this.searchType=='zipcode')
                p=searchZips();
        else if(this.searchType=='owner')
                p=searchOwners();
            
        return p;
    }
    
    public PageReference searchZips()
    {
        List<String > lZips=new List<String>();
        lZips.add(this.searchValue);
        this.reps=getContactsByZip(lZips);
        return null;
    }
    
    public PageReference searchStates()
    {
        List <String > lStates = new List<String>();
        lStates.add(this.searchValue);
        this.reps=getContactsByState(lStates);
        return null;
    }
    
    public PageReference searchOwners()
    {
        List<String > lOwners=new List<String>();
        lOwners.add(this.searchValue);
        this.reps=getContactsByOwner(lOwners);
        return null;
    }
    
        public PageReference searchForCities()
    {
        String str='';
        this.citySearchResults='[';
        String strCity=this.citySearch; //Apexpages.currentPage().getParameters().get('citySearch');
        String strSOQL='SELECT Id, Name, City__c, State__c FROM Zip_Codes__c WHERE City__c LIKE \''+strCity+'%\' ORDER BY State__c LIMIT 10';
        System.debug(strSOQL);
        List<Zip_Codes__c> lZ=Database.query(strSOQL);
        Set<String> sZ=new Set<String>();
        
        for(Zip_Codes__c z : lZ)
        {
            str=z.City__c+', '+z.State__c;
            
            if(!sZ.contains(str))
            {
                if(sZ.size()>0)
                  this.citySearchResults=this.citySearchResults+',';
                  
                this.citySearchResults=this.citySearchResults+' { value: \''+z.Id+'\', label: \''+str.replace('\'', '\\\'')+'\'} ';
                sZ.add(str);
            }
        }
                    
        this.citySearchResults=this.citySearchResults+']';
        return null;
    }
    
    public PageReference searchCities()
    {
        this.reps=getContactsByCity(this.searchValue);
        return null;
    }

    public PageReference searchForStates()
    {
        String str='';
        this.stateSearchResults='[';
        String strState=this.stateSearch; //Apexpages.currentPage().getParameters().get('stateSearch');
        String strSOQL='SELECT Id, Name, City__c, State__c FROM Zip_Codes__c WHERE State__c LIKE \''+strState+'%\' ORDER BY City__c LIMIT 10';
        System.debug(strSOQL);
        List<Zip_Codes__c> lS=Database.query(strSOQL);
        Set<String> sS=new Set<String>();
        
        for(Zip_Codes__c s : ls)
        {
            str=s.City__c+', '+s.State__c;
            
            if(!sS.contains(str))
            {
                if(sS.size()>0)
                  this.stateSearchResults=this.stateSearchResults+',';
                  
                this.stateSearchResults=this.stateSearchResults+' { value: \''+s.Id+'\', label: \''+str.replace('\'', '\\\'')+'\'} ';
                sS.add(str);
            }
        }
                    
        this.stateSearchResults=this.stateSearchResults+']';
        return null;
    }

        public PageReference searchForOwners()
    {
        String str='';
        this.ownerSearchResults='[';
        String strOwner=this.ownerSearch; //Apexpages.currentPage().getParameters().get('ownerSearch');
        String strSOQL='SELECT Id, Owner__r.Name, City__c, State__c FROM Zip_Codes__c WHERE Owner__r.Name LIKE \''+strOwner+'%\' ORDER BY Owner__r.LastName LIMIT 10';
        System.debug(strSOQL);
        List<Zip_Codes__c> lO=Database.query(strSOQL);
        Set<String> sO=new Set<String>();
        
        for(Zip_Codes__c o : lO)
        {
            str=o.City__c+', '+o.State__c+', '+o.Owner__c;
            
            if(!sO.contains(str))
            {
                if(sO.size()>0)
                  this.ownerSearchResults=this.ownerSearchResults+',';
                  
                this.ownerSearchResults=this.ownerSearchResults+' { value: \''+o.Id+'\', label: \''+str.replace('\'', '\\\'')+'\'} ';
                sO.add(str);
            }
        }
                    
        this.ownerSearchResults=this.ownerSearchResults+']';
        return null;
    }

    public Integer getRepCount()
    {
        return this.reps.size();
    }
    
    private List<Contact> getContactsByZip(List<String> lZips)
    {   
        List<Contact> lContacts=new List<Contact>();
        
        if(contactType==null)
            contactType=''; 
        
        String strZipCodes=' \'0\'';
        String strSOQL='SELECT ID, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, Phone, Email, Contact_Type__c, Prospect_Status__c, Producer_Status__c, Broker_Dealer_Name__c FROM Contact ';
        
        for(String s: lZips)
        {
            if(s.trim()!='')
            {
                strZipCodes=strZipCodes+' OR mailingpostalcode like \''+s+'%\' ';
                this.zipcode=s;
            }
        }
         
        strSOQL=strSOQL+' WHERE (MailingPostalCode like' + strZipCodes + ') ';
      //strSOQL=strSOQL+' AND SA_Status__c = \'NS REIT - Signed Selling Agreement\' ';
        strSOQL=strSOQL+' AND ( NS_Income_II_SA_Status__c = \'NS I2 - Signed Selling Agreement\' OR NS_HI_SA_Status__c = \'NS HI - Signed Selling Agreement\' )';
        strSOQL=strSOQL+' AND Contact_Type__c != \'POND List\' ';
        strSOQL=strSOQL+' AND Contact_Type__c != null ';
      //strSOQL=strSOQL+' AND contact.owner !=null ';
        
        //
        // Handle producer/prospect types.
        //
        
        if ('Un-Profiled'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Un-Profiled\' ';
        } else if ('All Producers'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Producer\'';
        } else if ('A Producers'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Producer\' AND Producer_Status__c like \'Producer A-%\'';
        } else if ('B Producers'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Producer\' AND Producer_Status__c like \'Producer B-%\'';
        } else if ('C Producers'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Producer\' AND Producer_Status__c like \'Producer C-%\'';
        } else if ('Sphere Of Influence Producers'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Producer\' AND Producer_Status__c like \'Sphere of Influence%\'';
        } else if ('All Prospects'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Prospect\'';
        } else if ('A Prospects'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Prospect\' AND Prospect_Status__c like \'Prospect A-%\'';
        } else if ('B Prospects'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Prospect\' AND Prospect_Status__c like \'Prospect B-%\'';
        } else if ('C Prospects'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Prospect\' AND Prospect_Status__c like \'Prospect C-%\'';
        } else if ('Sphere Of Influence Prospects'.equalsIgnoreCase(contactType)) {
            strSOQL += ' AND Contact_Type__c = \'Prospect\' AND Prospect_Status__c like \'Sphere of Influence%\'';
        }
        
        System.debug(strSOQL);
        lContacts=Database.query(strSOQL);
        return lContacts;
    }
    
    private List<Contact> getContactsByCity(String strCityState)
    {
        List<Contact> lContacts=new List<Contact>();
        Integer i=strCityState.lastIndexOf(',');
        
        if(i<1)
            return lContacts;
            
        String strCity=strCityState.substring(0, i).trim();
        String strState=strCityState.substring(i+1).trim().toUpperCase();
        
        System.debug('SELECT Id, Name FROM Zip_Codes__c WHERE City__c =\''+strCity+'\' AND State__c=\''+strState+'\' ORDER BY longitude__c');
        List<Zip_Codes__c> lZ=[SELECT Id,Name FROM Zip_Codes__c WHERE City__c=:strCity AND State__c=:strState ORDER BY longitude__c];
        List<String> lZips=new List<String>();  
        
        for(Zip_Codes__c z : lZ)
        {
            lZips.add(z.Name);    
        }
        
        lContacts=getContactsByZip(lZips);
        return lContacts;       
    }
    
    
    private List<contact> getContactsByState (List<String> lStates)
    {
        list<Contact> llContacts=new List<Contact>();
        
        if(contactType==null)
            contactType='';
        
        String strStates=' \'0\'';
        String strSOQL='SELECT Id, Name, City__c, State__c FROM Zip_Codes__c WHERE City__c=:strCity AND State__c=:strState ORDER BY City__c LIMIT 10';
        
      for(String s: lStates)
        {
            if(s.trim()!='')
            {
                strStates=strStates+' OR MailingState like \''+s+'%\' ';
                this.state=s;
            }
        }
        return llContacts; 
    }
    
    private List<Contact> getContactsByOwner(List<String> lOwner)
    {
        List<Contact> lllContacts=new List<Contact>();
        
         if(contactType==null)
            contactType='';
        
        String strOwner=' \'0\'';
        String strSOQL='select Id, Owner__r.Name, longitude__c, latitude__c, Territory__c, City__c, State__c from Zip_Codes__c WHERE Owner__r.Name=:strOwner';
        
        for(String o: lOwner)
        {
            if(o.trim()!='')
            {
                strOwner=strOwner+' OR Name like \''+o+'%\' ';
                this.owner=o;
            }
        }
        return lllContacts;
    }

    
    public Boolean getIsDisabledDownloadCSV() {
        return reps == null || reps.size() == 0;
    }
    
    public PageReference redirectToCSV() {
        processSearch();
        
        String documentData = '';
        documentData += '"Name",';
        documentData += '"Broker_Dealer_Name__c",';
        documentData += '"MailingStreet",';
        documentData += '"MailingCity",';
        documentData += '"MailingState",';
        documentData += '"MailingPostalCode",';
        documentData += '"Phone",';
        documentData += '"Email",';
        documentData += '"Last Completed Event",';
        documentData += '"Owner__r.Name",';
        documentData += '"Contact_Type__c"\r\n';
        
        for(Contact contact : reps) {
            documentData += '"' + contact.Name + '",';
            documentData += '"' + contact.Broker_Dealer_Name__c + '",';
            documentData += '"' + contact.MailingStreet + '",';
            documentData += '"' + contact.MailingCity + '",';
            documentData += '"' + contact.MailingState + '",';
            documentData += '"' + contact.MailingPostalCode + '",';
            documentData += '"' + contact.Phone + '",';
            documentData += '"' + contact.Email + '",';
            documentData += '"' + contact.Last_Event_Date__c + '",';
            documentData += '"' + contact.Owner + '",';
            documentData += '"' + contact.Contact_Type__c + '"\r\n';
        }
        
        Document document = new Document();
        document.Body = Blob.valueOf(documentData);
        document.ContentType = 'text/csv';
        document.FolderId = UserInfo.getUserId();
        document.Name = 'Territory Search - CSV Export - ' + DateTime.now().format() + '.csv';
        insert document;
        
        PageReference pageReference = new PageReference('/servlet/servlet.FileDownload?file=' + document.Id);
        pageReference.setRedirect(true);
        return pageReference;
    }
    
    public String getMapViewUrl() {
        PageReference pageReference = System.Page.TerritorySearch;
        pageReference.getParameters().put('searchType', searchType);
        pageReference.getParameters().put('searchValue', searchValue);
        pageReference.getParameters().put('zipcode', zipcode);
        pageReference.getParameters().put('contactType', contactType);
        pageReference.getParameters().put('statusType', statusType);
        pageReference.getParameters().put('citySearch', citySearch);
        pageReference.getParameters().put('stateSearch', stateSearch);
        pageReference.getParameters().put('ownerSearch', ownerSearch);
        pageReference.getParameters().put('mapView', 'true');
        return pageReference.getUrl();
    }
}

 

Test Class:

 

@isTest
private class Test_TerritorySearchController 
{

    static testMethod void testController()
    {
        TerritorySearchController tsc=new TerritorySearchController();
        tsc.searchValue='00001';
        tsc.searchType='zipcode';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);
        
        tsc.contactType='Prospect';
        tsc.statusType='Prospect A- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);

        tsc.contactType='Producer';
        tsc.statusType='Producer A- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);
        
        tsc.contactType='Prospect';
        tsc.statusType='Prospect B- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);

        tsc.contactType='Producer';
        tsc.statusType='Producer B- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);
        
        
        tsc.contactType='Prospect';
        tsc.statusType='Prospect C- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);

        tsc.contactType='Producer';
        tsc.statusType='Producer C- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);
        
        
        tsc.contactType='Prospect';
        tsc.statusType='Prospect Sphere of Influence- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);

        tsc.contactType='Producer';
        tsc.statusType='Producer Sphere of Inlfuence- $1M+ potential business/yr';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);

        tsc.searchValue='Ridgefield, CT';
        tsc.searchType='city';
        tsc.zipcode='';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);
        
        tsc.searchValue='Sean Briceno';
        tsc.searchType='owner';
        tsc.processSearch();
        //System.assertNotEquals(tsc.getRepCount(), 0);
        
        tsc.searchZips();
        tsc.getRepCount();
        
        tsc.citySearch='West';
        tsc.searchForCities();
        
        tsc.searchCities();
        tsc.searchForCities();
        
        tsc.searchStates();
        tsc.searchForStates();
        
        tsc.searchOwners();
        tsc.searchForOwners();
        
        tsc.redirectToCSV();
        tsc.getMapViewUrl();
    }
}

 

nickwick76nickwick76
Hi,
I world go to workbench.developerforce.com and check the Zip_Codes__c object to make sure you have the right relationship name. If it is correct, check that you have deployed the object first. Maybe this field is not present in the org you are deploying to?

// Niklas
SeanCenoSeanCeno

Zip_Codes__c object is already in production as the Territory Search Controller is already operational in production. Just not sure why referencing the contact owner is a problem.

nickwick76nickwick76

Hi,

Are you positive that the field referencing to the contact owner is in production?

 

// Niklas

SeanCenoSeanCeno

I found out my Owner__c was a text field in production, not a lookup. That was my problem. But now my Owner and State queries aren't pulling any results when I use the search controller in production. Does anybody see why these 2 fields aren't returning any data? Zip Codes and Cities works just fine.

nickwick76nickwick76

Ok, that explains it. 

 

Can you please specify the queries you have problem with and the ones working? It's a long code block and I'm unsure which ones you are referring to.

 

// Niklas