• Garima Agrawal 31
  • NEWBIE
  • 35 Points
  • Member since 2022

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hello Ohana,

I am getting an error 'Variable does not exist: varName' when I tried to access my global variable in the finish method of the batch class.
Point to Note:
1. Neither I am using a stateful class, not any static variable. So in any case values will be retained in my variable.
2. According to the salesforce article global variables declared in the batch class are accessible in all 3 methods.
Steps already performed:
1. The variable is initiated in the constructor with the parameter passed to the batch class.
2. The same variable is used in the start method to build my query.
Issues:
When I tries to access in finish method got an error saying the variable does not exist.
Below is the code snippet:

global class clsName implements DataBase.Batchable{
  global string var;
  public void clsName(string param){
     this.var = param;
  }
  public Database.queryLocator start(....){
       return Databse.getQueryLocator('SELECT Id from Account where ID =: 'var');
  }
  public static void execute(....){
     //Some Logic
  }
  public static void finish(....){
     System.debug(var);   //Error variable does not exist: var
  }
}

Let me know your suggestion or if I am making any mistake.
Thanks in advance.
Hello.

Its my first time working on Apex triggers and i have read that we must not run our SoQL queries inside of a bulk loop (same for DML calls). But i am struggling with that as i have a pretty complicated query inside my loop to make it work out of the loop. a kind of 2 level query that uses 4 sObjects that reference each others (Task, Contact, Account, Opportunity)

This is the Apex trigger i want to make it follow the best practices (i tested this version and it fires my trigger all good).

---------------------
//this trigger is used to update the opportunities retrieved from an account(s) related to the contact of this task that fires the trigger when it is marked as completed.

trigger Close_won_opportunities on Task (after update) {
    CloseWonOpportunitiesHandler taskHandler = new CloseWonOpportunitiesHandler();
    taskHandler.UpdateOpportunities(trigger.new);
    }

---------------------

public class CloseWonOpportunitiesHandler {
    public void UpdateOpportunities(List<Task> tasList){
        //create opportunities list
        List<Opportunity> OppsToUpdate = new List<Opportunity>();         
        //looping over the tasks list
        for(Task t : tasList){
            //if the task was created by a lead conversion and now is completed 
            if(t.leadConversion__c && t.Status=='Completed'){
                //get all the opportunities of the accounts related to the contact of this task
                 List<Opportunity> relatedOpps = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN (Select accountID from Contact where Id=:t.WhoId)];
                //Mark these opportunities as closed won
                for(Opportunity opp : relatedOpps){
                    opp.StageName = 'Closed Won';
                    OppsToUpdate.add(opp);
                }
            } 
      }
    //DML update opportunities
                upsert OppsToUpdate;
    }
}
---------------------

i tried solving it using nested Maps and multi level Queries but unfortunately i couldnt find the right way to do it.

i hope someone can help me with this.
thank you in advance.
Hello Ohana,

I am getting an error 'Variable does not exist: varName' when I tried to access my global variable in the finish method of the batch class.
Point to Note:
1. Neither I am using a stateful class, not any static variable. So in any case values will be retained in my variable.
2. According to the salesforce article global variables declared in the batch class are accessible in all 3 methods.
Steps already performed:
1. The variable is initiated in the constructor with the parameter passed to the batch class.
2. The same variable is used in the start method to build my query.
Issues:
When I tries to access in finish method got an error saying the variable does not exist.
Below is the code snippet:

global class clsName implements DataBase.Batchable{
  global string var;
  public void clsName(string param){
     this.var = param;
  }
  public Database.queryLocator start(....){
       return Databse.getQueryLocator('SELECT Id from Account where ID =: 'var');
  }
  public static void execute(....){
     //Some Logic
  }
  public static void finish(....){
     System.debug(var);   //Error variable does not exist: var
  }
}

Let me know your suggestion or if I am making any mistake.
Thanks in advance.