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
Robert Wambold 10Robert Wambold 10 

Custom Controller after getting Opportunity Id, how get other Opportunity fields for later use in a query

Hello All!

I have Custom Controller I want to call with a Custom Button on the Opportunity Page. The Custom Controller will get the Opportunity Id from the Opportunity page. I want to get an Opportunity Custom Field, OS_Number__c  (Text 6) from the passed Opportunity Id then save OS_Number__c for use in another query.

My VF Page displays the data correctly when I hard-code the OS_Number__c as '44831' so that part seems to be working correctly.

How do I correct my Custom Controller to work without hard-coding?

How do I add the Custom Button to the Opportunity Page?

Thank you for your help!

Robert

 

Controller:

public class JoinedProposalCheckController {

    public List<Opportunity> OSNum {get;set;} 
    public List<Opportunity> opptyList {get;set;}

    	public JoinedProposalCheckController() {
           populateOSNum();
		   populateOpptyList();
	    }

        private void populateOSNum() {    
           OSNum = [SELECT OS_Number__c   
                    FROM Opportunity  
                    WHERE Id = :ApexPages.currentPage().getParameters().get('id')
                    LIMIT 1 
                   ]; 
           System.Debug('OSNum='+OSNum); 
        }

        private void populateOpptyList() {    
           opptyList = [SELECT Id, Name, Account.Name, OS_Number__c, Opportunity_Number__c, Joint_Proposal__c   
                        FROM Opportunity 
                        WHERE OS_Number__c = '44831' 
                       ]; 
        }
      
        public List<Opportunity> getopptyList() {
          return opptyList;        
        }    
}
 

VFP:

<apex:page Controller="JoinedProposalCheckController" showHeader="true" sidebar="false" tabStyle="Opportunity">  
     <apex:form id="JPForm">  
          <apex:pageBlock title="Opportunities List" >   

            <apex:pageBlockSection >
                Please review Opportunities without a Joined Proposal
            </apex:pageBlockSection>
                 
            <apex:pageBlockSection >         
               <apex:pageBlockTable value="{!OpptyList}" var="op" width="100%">
                        <apex:column headerValue=" Opportunity">
                             <apex:outputLink value="/{!URLFOR(op.Id)}">{!op.Opportunity_Number__c}
                             </apex:outputLink>
                        </apex:column>  
                        <apex:column Value="{!op.Name}" headerValue="Opportunity Name___________________ ">
                        </apex:column>                               
                        <apex:column Value="{!op.OS_Number__c}" headerValue=" OS Number">
                        </apex:column>  
                        <apex:column Value="{!op.Account.Name}" headerValue="Account Name______________________________________ ">
                        </apex:column>                                       
                        <apex:column Value="{!op.Joint_Proposal__c}" headerValue="Joined Proposal____________________________________________________________">
                        </apex:column>                                            
               </apex:pageBlockTable> 
            </apex:pageBlockSection>                 
        
          </apex:pageBlock>    
     </apex:form>
</apex:page>


Custom Button:

Joint Proposal Check - Custom Button

Best Answer chosen by Robert Wambold 10
David Zhu 🔥David Zhu 🔥
I think you need to remove single quotation mark from line 12
OSNum_wrk = OSNum[0].OS_Number__c;

All Answers

David Zhu 🔥David Zhu 🔥
It is a big task. You may use the code snippet blow for your reference.

1. Display list on vf page
 1.1 VF Page:
<apex:page Controller="JoinedProposalCheckController" showHeader="true" sidebar="false" tabStyle="Opportunity" action="{!Init}">  
     <apex:form id="JPForm">  

 1.2 Apex code, add new method Init.

public void Init() {
  populateOSNum();
   populateOpptyList();
}


 1.3 in Apex code, remove method from construct.
public JoinedProposalCheckController() {
  populateOSNum();
   populateOpptyList();

 }

2. Add save button
    2.1 VF Page, add inside <apex:pageBlock> section
<apex:pageBlock title="Opportunities List" >
       <apex:pageBlockButtons>
            <apex:commandButton action="{!save}" value="Save"/>
       </apex:pageBlockButtons>

   .....
   2.2. Apex:
    Add new method Save
    public void Save()
    {
       Update opptyList ;
     }

 
Robert Wambold 10Robert Wambold 10

Hi David,

Thank you for the advice, however I need the "button" to be on the Opportutnity Page not my JoinedProposalCheck VFP.

While I have your attention can you help me with query?

I build a string variable OSNum_wrk ( OSNum_wrk = '\''+OSNum[0].OS_Number__c+'\''; ) and displays correctly with System.Debug as '44831'.

When I use OSNum_Wrk in my WHERE statement functions like it's Null? (Commented out below)  When I use Literal  '44831' I get the desired results.

Any ideas?

public with sharing class JoinedProposalCheckController {
String OSNum_wrk = '';   
  public JoinedProposalCheckController() {

     // Get Opportunity.OS_Number__c for passed Opportunity   
        List<Opportunity> OSNum = [SELECT OS_Number__c   
                                   FROM Opportunity  
                                   WHERE Id = '0060y000017j0IRAAY'  
                                 //WHERE Id = :ApexPages.currentPage().getParameters().get('id')
                                   LIMIT 1]; 

                          OSNum_wrk = '\''+OSNum[0].OS_Number__c+'\''; 
                          System.Debug('OSNum_wrk='+OSNum_wrk); 
       
  }
   
  // Get Opportunites that have the same OC_Number__c         
     List<Opportunity> opptyList = [SELECT Id, Name, Account.Name, OS_Number__c, Opportunity_Number__c, Joint_Proposal__c   
                                    FROM Opportunity 
                                    //WHERE OS_Number__c = :OSNum_wrk
                                    WHERE OS_Number__c = '44831' 
                                    LIMIT 10]; 
                                       
  // Return List of Oportunites to Visualforce Page
     public List<Opportunity> getopptyList() {
        return opptyList;                
     }  
        
}
David Zhu 🔥David Zhu 🔥
I think you need to remove single quotation mark from line 12
OSNum_wrk = OSNum[0].OS_Number__c;
This was selected as the best answer
Robert Wambold 10Robert Wambold 10
Thank you David!