1

I am working on creating my first wrapper class where I will want to allow users to select from a list of sites (custom object Site__c) and it will relate the site to a quote record through an Associated_Location__c object record.

Right now I am stuck on passing over the AccounId. I was hoping that the controller.getRecord().Id would pass the AccountId into the SOQL query but my query is giving me the old 'List has no rows for assignment to SObject'. I think this has to do with my StandardController being Associated_Location__c as it does not have an AccountId. How can I resolve this issue? Thanks in advance!

public class quoteAssociatedSiteExtension {

public List<assLoc> assLocList {get; set;}
public Quote theQuote;

public quoteAssociatedSiteExtension(ApexPages.StandardController controller) {

    theQuote = [select Id, AccountId from Quote where Id =:controller.getRecord().Id limit 1];
}

public List<assLoc> getAssLoc() {
    if(assLocList == null) {
        assLocList = new List<assLoc>();
        for(Site__c s: [select Id, Name, Account__r.Id from Site__c where Account__r.Id=:theQuote.AccountId limit 10]) {
            assLocList.add(new assLoc(s));
        }
    }
    return assLocList;
}


public PageReference processSelected() {

    List<Associated_Location__c> selectedAssociatedLocations = new List<Associated_Location__c>();

    for(assLoc aLoc: getAssLoc()) {
        if(aLoc.selected == true) {
            selectedAssociatedLocations.add(aLoc.ac);
        }
    }

    System.debug('These are the selected Contacts...');
    for(Associated_Location__c ac: selectedAssociatedLocations) {
        system.debug(ac);
    }
    assLocList=null; 
    return null;
}


public class assLoc {
    public Associated_Location__c ac {get; set;}
    public Site__c st {get; set;}
    public Quote qt {get; set;}
    public Boolean selected {get; set;}

    public assLoc(Associated_Location__c a) {
        ac = a;
        selected = false;
    }

    public assLoc(Site__c s) {
        st = s;
        selected = false;
    }

    public assLoc(Quote q) {
        qt = q;
        selected = false;
    }        
}}

Visualforce Page:

<apex:page standardController="Associated_Location__c" extensions="quoteAssociatedSiteExtension">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!AssLoc}" var="a" id="table">
            <apex:column >
                <apex:inputCheckbox value="{!a.selected}"/>
            </apex:column>
            <apex:column value="{!a.st.Name}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

1

As you mentioned relate the site to a quote record through an Associated_Location__c object record, there will be a field which points to Quote in Associate_Location__c object record. So you should use that field from the controller.getRecord().

Association_Location__c assLoc = (Association_Location__c)controller.getRecord();
theQuote = [select Id, AccountId from Quote where Id =:assLoc.Quote__c limit 1];

But to make the field to be available for the Standard Controller to fetch you need to add it to the Standard Controller before getting the record from the Standard Controller.

controller.addFields(new List<String>{'Quote__c'});

Hope it helps.

  • I don't know if this would work. Say there are no records of Associated_Location__c, how would it be possible to query? The Site records are related to the account but the Associated_Location records are only related to the quote. – wadams2010 May 10 '16 at 20:31
  • Associated_Location__c is related to Quote, Quote is related to Account and Account is related to Site__c. Since your standard controller being Associated_Location__c, you should get the Quote record and then using its accountId field, query the Site__c record. – Vigneshwaran G May 10 '16 at 20:38
  • I tried it out but no luck. It is saying that 'Field expression not allowed for generic SObject'. Here is what the code looks like: controller.addFields(new List<String>{'Quote__r'}); theQuote = [select AccountId from Quote where Name =:controller.getRecord().Quote__r.Id limit 1]; } – wadams2010 May 10 '16 at 21:17
  • Use Quote__c in both places. I have updated my answer with the field API name. – Vigneshwaran G May 10 '16 at 21:27
  • It is still giving me the same message 'Field expression not allowed for generic SObject. Here is the code: public quoteAssociatedSiteExtension(ApexPages.StandardController controller) { controller.addFields(new List<String>{'Quote__c'}); theQuote = [select Id, AccountId from Quote where Id =:controller.getRecord().Quote__c limit 1]; } – wadams2010 May 10 '16 at 21:31

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.