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
mohit tripathimohit tripathi 

Visualforce Extension ISsue

Hi Guys,


I am getting following error when  I am trying to fetch opportunities which I have already fetched in another method.
HEre is my Extension-

public class StandardControllerExtension {

    public Account Acct;
  
    public StandardControllerExtension(ApexPages.StandardController stdController) {
             Acct= (Account)stdController.getRecord();
    }
       Public List<Opportunity> getoppt(){
        
        return [ select name, stageName , Amount , CloseDate From Opportunity where AccountID =:acct.Id and ( LeadSource =  'Web'  And StageName= 'Prospecting')];
       
       }
       public void CreateTaskOnChildOppt(){
         List <Task> newtask = new List<task>();
         for (Opportunity opp : oppt) {
         if(!opp.isclose){
         newtask.add(
 new Task(
 WhatId = opp.Id,
 OwnerId = opp.OwnerId,
 ActivityDate = Date.today() + 3,
 
 Subject = 'Send follow-up email to primary contact'));
         
         }
       
       }
       if (newtask.size() > 0) insert newtask; 
       }
       
       public pageReference Save(){
       if(Acct.Rating=='Hot'){
       CreateTaskOnChildOppt();
       }
       update Acct;
       Return new PageReference('/' + Acct.id);
       }
}

Error-Error: StandardControllerExtension Compile Error: Variable does not exist: oppt at line 15 column 33

Highlighted them for the reference.

Thanks in advance
Best Answer chosen by mohit tripathi
Amit Singh 1Amit Singh 1
Ok Use below Code
 
public class StandardControllerExtension {

    public Account Acct;
  
    public StandardControllerExtension(ApexPages.StandardController stdController) {
             Acct= (Account)stdController.getRecord();
    }
       Public List<Opportunity> getoppt(){
        
        return [ select name, stageName , Amount , CloseDate From Opportunity where AccountID =:acct.Id and ( LeadSource =  'Web'  And StageName= 'Prospecting')];
       
       }
       public void CreateTaskOnChildOppt(){
		 List<Opportunity> oppList = getoppt();
         List <Task> newtask = new List<task>();
         for (Opportunity opp : oppList) {
         if(opp.StageName!='Closed Won'){
         newtask.add(
			 new Task(
			 WhatId = opp.Id,
			 OwnerId = opp.OwnerId,
			 ActivityDate = Date.today() + 3,
			 
			 Subject = 'Send follow-up email to primary contact'));
         
         }
       
       }
       if (newtask.size() > 0) insert newtask; 
       }
       
       public pageReference Save(){
       if(Acct.Rating=='Hot'){
       CreateTaskOnChildOppt();
       }
       update Acct;
       Return new PageReference('/' + Acct.id);
       }
}

 

All Answers

Amit Singh 1Amit Singh 1
replace your for loop with below code
for (Opportunity opp : getoppt()) {
Thanks,
 
mohit tripathimohit tripathi
Hi Amit, 
Thanks for the reply but I have tried that and getting following error.


Error: StandardControllerExtension Compile Error: Variable does not exist: getoppt at line 18 column 33
Amit Singh 1Amit Singh 1
Ok Use below Code
 
public class StandardControllerExtension {

    public Account Acct;
  
    public StandardControllerExtension(ApexPages.StandardController stdController) {
             Acct= (Account)stdController.getRecord();
    }
       Public List<Opportunity> getoppt(){
        
        return [ select name, stageName , Amount , CloseDate From Opportunity where AccountID =:acct.Id and ( LeadSource =  'Web'  And StageName= 'Prospecting')];
       
       }
       public void CreateTaskOnChildOppt(){
		 List<Opportunity> oppList = getoppt();
         List <Task> newtask = new List<task>();
         for (Opportunity opp : oppList) {
         if(opp.StageName!='Closed Won'){
         newtask.add(
			 new Task(
			 WhatId = opp.Id,
			 OwnerId = opp.OwnerId,
			 ActivityDate = Date.today() + 3,
			 
			 Subject = 'Send follow-up email to primary contact'));
         
         }
       
       }
       if (newtask.size() > 0) insert newtask; 
       }
       
       public pageReference Save(){
       if(Acct.Rating=='Hot'){
       CreateTaskOnChildOppt();
       }
       update Acct;
       Return new PageReference('/' + Acct.id);
       }
}

 
This was selected as the best answer
mohit tripathimohit tripathi
Thanks Amit, 

I made the changes suggested by you  and it is working perfectly fine..

Thanks a ton Man .. Cheers
mohit tripathimohit tripathi
Hi Amit,


When I Am trying to this via custom controller, I Am unable to fetch contact's record from an account in vf page.
I feel controller is fine but I Am missing something in VF page.

Here is my controller-

public class AccCustomOppController {

    Public Account Acct {get; set;}

public AccCustomOppController (){

String AcctId = ApexPages.CurrentPage().getParameters().get('id');

Acct= [select name, Rating,  id from Account Where Id =: AcctId];

}
public List<Contact> ContactList{
get{

ContactList = [ select name, Email , Department from Contact where AccountId = :Acct.Id ];

return ContactList;
}
set;
}

Public PageReference Save(){

update Acct;
return new pageReference ('/' + Acct.ID);

}
}

and VF page is-

<apex:page controller="AccCustomOppController">
<apex:form >
<apex:pageblock Title="Account Custom Controller check">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save the updated Account Information"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!Acct.Name}"/>
</apex:pageBlockSection>
</apex:pageblock>

<apex:pageBlock>
<apex:pageBlockTable value="{!ContactList}" var="Con">
<apex:column value="{!con.Name}" title="Contact Name"/>
</apex:pageBlockTable>
</apex:pageBlock>


</apex:form>
</apex:page>


Can you please have a look into this?



Thanks in advance