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
ali-kantar1.3897183320927883E12ali-kantar1.3897183320927883E12 

can someone help me identify what is going wrong in the apex Method

i have written this method but it keeps giving me this error :
Error: Compile Error: Variable does not exist: childOpps at line 14 column 23

public class StandardControllerExtension {
Account acct;
public StandardControllerExtension (ApexPages.standardController std)
{
acct = (Account)std.getRecord();
}
public List<Opportunity> getChildOpps() {
return [Select Name, Amount, StageName, CloseDate From Opportunity
Where AccountId = :acct.Id
and (IsWon = true or IsClosed = false)];
}
private void createTaskOnChildOpps () {
List<Task> tasksToInsert = new List<Task> ();
for (Opportunity opp : childOpps) {
if (!opp.isClosed) {
tasksToInsert.add (
new Task(
WhatId = Opp.Id,
OwnerId = opp.OwnerId,
ActivityDate =Date.today () + 3,
Status = 'Not Started',
Subject = 'Send follow-up email to pirmary contact'
)
);
}
}
if (tasksToInsert.size() >0 )insert taskToInsert;
}
public PageReference save() {
if (acct.Rating =='Hot') {
createTaskOnChildOpps();
}
update acct;
return new PageReference ('/' + acct.Id);
}
}

can anyone help me with this ? 
Best Answer chosen by ali-kantar1.3897183320927883E12
Sagarika RoutSagarika Rout
public class StandardControllerExtension {
Account acct;
List<Opportunity> ChildOpps ;
public StandardControllerExtension (ApexPages.standardController std)
{
acct = (Account)std.getRecord();
}
public List<Opportunity> getChildOpps()
{
    ChildOpps = [Select Name, Amount, StageName, CloseDate From Opportunity Where AccountId = :acct.Id and (IsWon = true or IsClosed = false)];
    return ChildOpps;
}
private void createTaskOnChildOpps () {
List<Task> tasksToInsert = new List<Task> ();
for (Opportunity opp : childOpps) {
if (!opp.isClosed) {
tasksToInsert.add (
new Task(
WhatId = Opp.Id,
OwnerId = opp.OwnerId,
ActivityDate =Date.today () + 3,
Status = 'Not Started',
Subject = 'Send follow-up email to pirmary contact'
)
);
}
}
if (tasksToInsert.size() >0 )insert taskToInsert;
}
public PageReference save() {
if (acct.Rating =='Hot') {
createTaskOnChildOpps();
}
update acct;
return new PageReference ('/' + acct.Id);
}
}

All Answers

Saravanan @CreationSaravanan @Creation
Hi ,

Include this below line in the variable decaration.

list<Opportunity> ChildOpps=new list<Opportunity>();

Then it will work.
blucasblucas
Your method getChildOpps is called by the VisualForce page that uses your controller. Your VisualForce markup will call getChildOpps and use the returned list as needed. However, the list of Opportunities isn't assigned to any local variables in your controller. It only has scope to the VisualForce page. If you want to re-use the list of Opportunities you will need to assign it to a variable so that it is preserved. Saravanan's answer is partially right, but his list would be empty when createTaskOnChildOpps is called. You would need to change getChildOpps to assign to that new variable -

List<Opportunity> ChildOpps;

public List<Opportunity> getChildOpps()
{
    ChildOpps = [Select Name, Amount, StageName, CloseDate From Opportunity Where AccountId = :acct.Id and (IsWon = true or IsClosed = false)];
    return ChildOpps;
}

You could also directly call your getChildOpps method in createTaskOnChildOpps, but I wouldn't recommend it. You might end up with a different list of Opportunities than the page originally started with.
ali-kantar1.3897183320927883E12ali-kantar1.3897183320927883E12
i still cant idenify where i would put the :
list<Opportunity> ChildOpps=new list<Opportunity>();

because its the first time writing such a long code !
Sagarika RoutSagarika Rout
public class StandardControllerExtension {
Account acct;
List<Opportunity> ChildOpps ;
public StandardControllerExtension (ApexPages.standardController std)
{
acct = (Account)std.getRecord();
}
public List<Opportunity> getChildOpps()
{
    ChildOpps = [Select Name, Amount, StageName, CloseDate From Opportunity Where AccountId = :acct.Id and (IsWon = true or IsClosed = false)];
    return ChildOpps;
}
private void createTaskOnChildOpps () {
List<Task> tasksToInsert = new List<Task> ();
for (Opportunity opp : childOpps) {
if (!opp.isClosed) {
tasksToInsert.add (
new Task(
WhatId = Opp.Id,
OwnerId = opp.OwnerId,
ActivityDate =Date.today () + 3,
Status = 'Not Started',
Subject = 'Send follow-up email to pirmary contact'
)
);
}
}
if (tasksToInsert.size() >0 )insert taskToInsert;
}
public PageReference save() {
if (acct.Rating =='Hot') {
createTaskOnChildOpps();
}
update acct;
return new PageReference ('/' + acct.Id);
}
}
This was selected as the best answer
ali-kantar1.3897183320927883E12ali-kantar1.3897183320927883E12
Thanks for your help Sagarika Rout i added the missing but the expression didnt work when i had this in the method : if (tasksToInsert.size() >0 )insert taskToInsert;  it said that the variable was not recodnised i took it out of the method and it worked fine.