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
hmoh1920hmoh1920 

pass a variable from one class to another

Hi,

 

How retrieve a variable declared in my controller from my batch.

 

I call the batch from a method in my controller and this variable is opportunity id.

 


 

thanks.

AmitSahuAmitSahu
you can use global variable I guess . but for that you have to make the class as global as well.
hmoh1920hmoh1920

Hi,

is it possible to declare  an controller  a "global controlle"  because i call batch  from my controller and a like transmetre theID opportunity to the batch.

 

 

thanks

hmoh1920hmoh1920

Hi,

 

this is my code:

 

myControllerExtension is the extension of standard controller opportunity

*****begin****

global class myControllerExtension {

   public Opportunity Opp;
   global String IdNewAvenant;
   global String IdPoliceMere;
   Opportunity opportunity {get;set;}   
   public Opportunity OppAcc {get;set;}
   
/********************************************************************************************/       
   ApexPages.StandardController controller;   
   public myControllerExtension(ApexPages.StandardController c)
   
   {
        this.Opp = (Opportunity) c.getRecord();
               
   OppAcc = [select Accountid, LeadSource,Activit_principale__c, Ancien_num_ro_de_police__c
             from Opportunity where  id = :Opp.id];
       
   }                              
/********************************************************************************************/
   public PageReference Avenant()
      {
        return Page.Avenant;
      }   
/********************************************************************************************/    
     public PageReference cancel()
     {
            PageReference opportunityPage = new ApexPages.StandardController(opp).view();
            opportunityPage.setRedirect(true);
            return opportunityPage;
     }
/********************************************************************************************/     
   public Opportunity getOpportunity() {
      opportunity = new Opportunity();
      return opportunity;
   }
/********************************************************************************************/     
   public PageReference save() {
      
   Opportunity opport = new Opportunity(
                                         LeadSource=OppAcc.LeadSource,
                                         AccountId=OppAcc.AccountId,
                                         Ancien_num_ro_de_police__c=OppAcc.,
                                         Activit_principale__c=OppAcc.Activit_principale__c
                                       );    
             insert opport;
             IdPoliceMere = Opp.id;
             IdNewAvenant=opport.id;
                   
   BatchCreerAvenant acc = new BatchCreerAvenant();
   Database.executeBatch(acc);         
                 
      PageReference opptyPage = new ApexPages.StandardController(opport).view();
      opptyPage.setRedirect(true);
      return opptyPage;    
   }       
}

******end**********

my batch:

*****begin*********

global class BatchCreerAvenant implements Database.Batchable<sObject>{

String IdAvenant;
String IdPolice;

        
global final String query ='SELECT id,name FROM Opportunity';        

global Database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<Sobject> scope){

//recover the variable at this level
IdAvenant = IdNewAvenant;  //Save error: Variable does not exist: IdNewAvenant
IdPolice = IdPoliceMere;  //Save error: Variable does not exist: IdPoliceMere

List<Opportunity> opps = [select id,  Date_d_ech_ance_de_la_police__c, Total_montant_pay__c, varBalance__c , BalanceTest__c
                            from Opportunity where id=:IdNewAvenant];

              
              
 
}
global void finish(Database.BatchableContext BC){
   
}
}

 

please help me to recover this variabe at my batch.

 

 

Thanks.

 

 

 

AmitSahuAmitSahu
you have to create an instance of the mycontrollerextension and access it's variables.
hmoh1920hmoh1920

how?

 

thanks

AmitSahuAmitSahu

myControllerExtension .variable doesnot work ?