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
Matthias KimmigMatthias Kimmig 

How can I pass a value to the Apex class from the current Visualforce page/Opportunity?

Hey,

this is my first visualforce page.

I have one question concerning Apex class. How can I pass a value to the Apex class from the current Visualforce page/Opportunity? For example, the zip code?

In Germany, the postal code consists of 5 digits. But I want to pass only the first 3 digits to the Apex class. How does it work?

Visualfoce page:
<apex:page standardController="Opportunity" extensions="Controller" tabStyle="Opportunity">
    <apex:pageBlock title="zip code">
        <apex:pageBlockTable value="{!opp}" var="Opportunity" >
            <apex:column value="{!Opportunity.AccountId}"/>
            <apex:column value="{!Opportunity.City__c}"/>
            <apex:column value="{!Opportunity.Zip_Code__c}"/>
            <apex:column headerValue="Link">
                <apex:outputLink value="{!'/'&Opportunity.AccountId}">Link</apex:outputLink>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex-Class:
public class Controller {

    public List<Opportunity>opp{get; set;}
  
    public Controller(ApexPages.StandardController controller){
      
        opp = [SELECT Zip_Code__c, Stadt__c, AccountId FROM Opportunity WHERE Zip_Code__c LIKE :LEFT(currentobject.zip_Code__c,3);
    }
}

This command ":LEFT(currentobject.zip_Code__c,3)" does not work. What is the correct command?

Thanks for your answers.
Best Answer chosen by Matthias Kimmig
Shingo YamazakiShingo Yamazaki
Hello Matthias,

My name is Shingo Yamazaki.

How about using controller.getRecord() method to get current object?
public class Controller {
    public List<Opportunity>opp{get; set;}

    public Controller(ApexPages.StandardController controller){
        Opportunity currentobject = (Opportunity) controller.getRecord();
        opp = [SELECT Zip_Code__c, Stadt__c, AccountId FROM Opportunity WHERE Zip_Code__c LIKE :LEFT(currentobject.zip_Code__c,3)];
    }
}

But I'm not sure whether the function LEFT is available in SOQL.
(Maybe it's not...)

Instead, you can use String.substring function.
String zip = currentobject.zip_Code__c.substring(0, 3);
opp = [SELECT Zip_Code__c, Stadt__c, AccountId FROM Opportunity WHERE Zip_Code__c LIKE :zip];

All Answers

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
next to the method give
opp = new List<Opportunity>();
Shingo YamazakiShingo Yamazaki
Hello Matthias,

My name is Shingo Yamazaki.

How about using controller.getRecord() method to get current object?
public class Controller {
    public List<Opportunity>opp{get; set;}

    public Controller(ApexPages.StandardController controller){
        Opportunity currentobject = (Opportunity) controller.getRecord();
        opp = [SELECT Zip_Code__c, Stadt__c, AccountId FROM Opportunity WHERE Zip_Code__c LIKE :LEFT(currentobject.zip_Code__c,3)];
    }
}

But I'm not sure whether the function LEFT is available in SOQL.
(Maybe it's not...)

Instead, you can use String.substring function.
String zip = currentobject.zip_Code__c.substring(0, 3);
opp = [SELECT Zip_Code__c, Stadt__c, AccountId FROM Opportunity WHERE Zip_Code__c LIKE :zip];
This was selected as the best answer
Abi DuthuAbi Duthu
Hi,

Use the method Substring define it with the arguments Integer startIndex,Integer endIndex

It returns a new String that begins with the character at the specified startIndex and extends to the character at endIndex. 

For example:
'hamburger'.substring(4, 8);
// Returns "urge"
   
'smiles'.substring(1, 5);
// Returns "mile"

'longer'.substring(0, 3);
// Returns "lon"

Like this get your substring in one variable and pass it to Query. 


Matthias KimmigMatthias Kimmig
Hello,

thanks for your answer.

The new apex-class:
public class Controller {

    public List<Opportunity>opp{get; set;}
    public String zip;
              
    public Controller(ApexPages.StandardController controller){
        Opportunity currentobject = (Opportunity) controller.getRecord();
        zip = currentobject.Zip_Code__c.substring(0, 3);
        opp = [SELECT Zip_Code__c, Stadt__c, AccountId FROM Opportunity WHERE Zip_Code__c LIKE :zip]; 
    }
}

Now, I get an error message in the Opportunity view.

SObject row was retrieved via SOQL without querying the requested field: Opportunity.Zip_Code__c

What could be the problem?

thx.
Shingo YamazakiShingo Yamazaki
Hello,

Please try to add one line to Visualforce.
<apex:page standardController="Opportunity" extensions="Controller" tabStyle="Opportunity">
    <!-- Add this -->
    <apex:outputText value="{!opportunity.Zip_Code__c}" rendered="false" />

    <apex:pageBlock title="zip code">
        <apex:pageBlockTable value="{!opp}" var="Opportunity" >
            <apex:column value="{!Opportunity.AccountId}"/>
            <apex:column value="{!Opportunity.City__c}"/>
            <apex:column value="{!Opportunity.Zip_Code__c}"/>
            <apex:column headerValue="Link">
                <apex:outputLink value="{!'/'&Opportunity.AccountId}">Link</apex:outputLink>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Matthias KimmigMatthias Kimmig
Thanks. It works!

The command "WHERE Zip_Code__c LIKE :zip" doesn't work. The list is empty if I execute the querry. 
I have tested some ways but I find no solution. Could you help me?
Shingo YamazakiShingo Yamazaki
If you want to retrieve the records which contains the String defined as "zip" in the Zip_Code__c field, use regular expression.

// when you want to retrieve the records whose Zip_Code__c START with the string "zip"
zip = currentobject.Zip_Code__c.substring(0, 3) + '%';

// when you want to retrieve the records whose Zip_Code__c END with the string "zip"
zip = '%' + currentobject.Zip_Code__c.substring(0, 3);

// when you want to retrieve the records whose Zip_Code__c CONTAINS the string "zip"
zip = '%' + currentobject.Zip_Code__c.substring(0, 3) + '%';