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
Matt HamptonMatt Hampton 

Help With An Extension

Hello:

 

I am trying to develop a Visualforce page where I am inseting a new section of a related object into the record if a certain criteria is met.

 

I have a custom sObject (Zip_Code__C) that is in a Master-Detail with sObject Sites__c where Zip_Code__c is the parent object.

 

Site__c has multiple record types so I want to break up the related lists into sections based on record type.

 

The controller I am tyring to write is:

 

public class MyController {

    private final Sites__C sites;

    public MyController() {
        sites = [SELECT Name, Units__c FROM Sites__c
                   WHERE Zip_Code__c = :ApexPages.currentPage().getParameters().get('id')
                   AND RecordTypeID='012C0000000GEK4'];
    }

    public sites__c getsites() {
        return sites;
    }

    public PageReference save() {
        update sites;
        return null;
    }
}

 

 

By doing this, it would insert a table into the record itself rather than create a related list. 

 

My Visialforce code is:

 

<apex:page standardController="Zip_Code__c" extensions="MyController">
<!-- the first example, below, demonstrates the use of the pageBlockTable tag to display a mash-up list of Job Application & Candidate information -->
<!-- this is important because we did not have to burn custom fields in order to pull in the candidate info for display -->
<!-- also notice that the formatting is taken care of for you when using the pageBlockTable tag -->
<apex:pageBlock >
<apex:pageBlockTable value="{!Zip_Code__c.Sites__r}" var="item">
<apex:column value="{!item.name}"/> 
<apex:column value="{!item.Units__c}"/>
</apex:pageBlockTable> 
</apex:pageBlock>
<!-- the example below demonstrates the use of the dataTable tag to display the same information as the first example above -->
<!-- notice that formatting is not handled for you when using the dataTable tag, and we need to explicitly declare column heading labels -->
</apex:page>

 

 

I am getting error Error: Unknown constructor 'MyController.MyController(ApexPages.StandardController controller).

 

Thoughts? Suggestions? Help?

 

Best Answer chosen by Admin (Salesforce Developers) 
logontokartiklogontokartik

Try the following in re: to the orginal query you had

 

Apex Class : 

 

public class MyController {

    //private final Sites__c sites;
    
    private List<Sites__c> sites = new List<Sites__c>();

    public MyController(Apexpages.StandardController controller) {
      String zipCodeId = controller.getId();

 sites = [SELECT Name, Units__c FROM Sites__c
                   WHERE Zip_Code__c = :zipCodeId
                   AND RecordTypeID='012C0000000GEK4'];
    }

    public List<Sites__c> getsites() {
        return sites;
    }

    public PageReference save() {
        update sites;
        return null;
    }
}

 

Visualforce Page :

 

<apex:page standardController="Zip_Code__c" extensions="MyController">
<!-- the first example, below, demonstrates the use of the pageBlockTable tag to display a mash-up list of Job Application & Candidate information -->
<!-- this is important because we did not have to burn custom fields in order to pull in the candidate info for display -->
<!-- also notice that the formatting is taken care of for you when using the pageBlockTable tag -->
<apex:pageBlock >
<apex:pageBlockTable value="{!sites}" var="item">
<apex:column value="{!item.name}"/> 
<apex:column value="{!item.Units__c}"/>
</apex:pageBlockTable> 
</apex:pageBlock>
<!-- the example below demonstrates the use of the dataTable tag to display the same information as the first example above -->
<!-- notice that formatting is not handled for you when using the dataTable tag, and we need to explicitly declare column heading labels -->
</apex:page>

 I am not sure about your VF page in 2nd query - from what I see, using {!Zip_Code__c.Releases__r} would fetch all records. you need to query for the Releases Object in the controller by putting the WHERE clause (as we did for Sites above) and use that in pageblocktable

 

All Answers

logontokartiklogontokartik

Your constructor in your Extension class should have Apexpages.StandardController as argument. try changing your code

public class MyController {

    //private final Sites__c sites;
    
    private List<Sites__c> sites = new List<Sites__c>();

    public MyController(Apexpages.StandardController controller) {
      String zipCodeId = controller.getId();

 sites = [SELECT Name, Units__c FROM Sites__c
                   WHERE Zip_Code__c = :zipCodeId
                   AND RecordTypeID='012C0000000GEK4'];
    }

    public List<Sites__c> getsites() {
        return sites;
    }

    public PageReference save() {
        update sites;
        return null;
    }
}

 

Here in code i made few changes,

1. Removed the Apexpages.currentPage and added  the controller.getId() method, 

 

2 .Changing the Sites__c to a List as the query you are trying to run will always return a list but not a single record. 

 

 

Matt HamptonMatt Hampton

Hello:

 

I am still having problems with this. Attached is my VF code:

 

<apex:page standardcontroller="Zip_Code__c" extensions="RFMDU">
    <apex:pageBlock >
                <apex:pageBlockTable value="{!Zip_Code__c.Releases__r}" var="zip" width="100%" columns="5">
                <apex:column id="c5" headerValue="MDU">
                    <apex:outputField value="{!zip.Name}"/>
                </apex:column>
                    <apex:column id="c4" headerValue="Units">
                        <apex:outputField value="{!zip.Units__c}"/>  
                </apex:column>
                    <apex:column id="c3" headerValue="Account Manager">
                        <apex:outputField value="{!zip.Account_Manager__c}"/>  
                </apex:column>
                    <apex:column id="c2" headerValue="Release Date">
                        <apex:outputField value="{!zip.Released_To_Sales__c}"/>  
                </apex:column>
                    <apex:column id="c1" headerValue="Zip Code">
                        <apex:outputField value="{!zip.Zip_Code__c}"/>   
                </apex:column>
                </apex:pageBlockTable>
    </apex:pageBlock>

 I used the class mentioned above as the extension however it is still pulling back all record types. Any suggestions?

 

Thanks,

 

Matt

logontokartiklogontokartik

Yes. you are using Zip_code__c directly from the VF Page in the PageBlockTable, you should use the one from the controller class. In your case try using sites., because that was the list that is retrieved with a WHERE filter. 

Matt HamptonMatt Hampton

Kartik:

 

I'm not sure I understand exactly where you are referencing me to make the change in my VF code. I am sorry I am new to all of this coding. Could you tell me where I need to make the change?

logontokartiklogontokartik

Try the following in re: to the orginal query you had

 

Apex Class : 

 

public class MyController {

    //private final Sites__c sites;
    
    private List<Sites__c> sites = new List<Sites__c>();

    public MyController(Apexpages.StandardController controller) {
      String zipCodeId = controller.getId();

 sites = [SELECT Name, Units__c FROM Sites__c
                   WHERE Zip_Code__c = :zipCodeId
                   AND RecordTypeID='012C0000000GEK4'];
    }

    public List<Sites__c> getsites() {
        return sites;
    }

    public PageReference save() {
        update sites;
        return null;
    }
}

 

Visualforce Page :

 

<apex:page standardController="Zip_Code__c" extensions="MyController">
<!-- the first example, below, demonstrates the use of the pageBlockTable tag to display a mash-up list of Job Application & Candidate information -->
<!-- this is important because we did not have to burn custom fields in order to pull in the candidate info for display -->
<!-- also notice that the formatting is taken care of for you when using the pageBlockTable tag -->
<apex:pageBlock >
<apex:pageBlockTable value="{!sites}" var="item">
<apex:column value="{!item.name}"/> 
<apex:column value="{!item.Units__c}"/>
</apex:pageBlockTable> 
</apex:pageBlock>
<!-- the example below demonstrates the use of the dataTable tag to display the same information as the first example above -->
<!-- notice that formatting is not handled for you when using the dataTable tag, and we need to explicitly declare column heading labels -->
</apex:page>

 I am not sure about your VF page in 2nd query - from what I see, using {!Zip_Code__c.Releases__r} would fetch all records. you need to query for the Releases Object in the controller by putting the WHERE clause (as we did for Sites above) and use that in pageblocktable

 

This was selected as the best answer
Matt HamptonMatt Hampton

Thank you so much for your help. This did it.

Matt HamptonMatt Hampton

Kartik:

 

Could you attempt to help me with two other components of the Apex Class?

 

I only want to display records where the Date_of_Last_Resweep is at least 90 days prior. (Example, I would only want records to show in my table where Date_of_Last_Resweep is September 12, 2012 or prior).

 

Also, when the class executes, I want them sorted by Current_Penetration__c from lowest to highest.

 

Any suggestions? I cannot quite figure this out.

 

Thanks in advance!

 

Matt

logontokartiklogontokartik

in your SOQL where clause just add " AND Date_of_Last_Resweep < LAST_90_DAYS ORDER BY Current_Penetration__c".  

Matt HamptonMatt Hampton

Kartik:

 

Can I bug you again if you don't mind?

 

I have the following:

 

sObject Resweep__c is the Parent in a Lookup Relationship with Site__c

sObject Site__c is the Parent in a Lookup Relationship with Video_Qualified_Address__c

sObject Video_Qualified_Address__c is the parent in a Lookup relationship with Subscriber__c

 

I need to write VF code to pull Video_Qualified_Address__c and Subscriber__c fields into a pageblocktable where Resweep__c.Site__c = Video_Qualified_Address__c.Site__c

 

I tried to use the code from your previous reply as a guide but I am having trouble with the condition that Site__c on the Resweep__c record = Site__c on the Video_Qualified_Address__c record.

 

What do I need to change?

 

Thanks a million for all your help.

 

Matt