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
PrasadVRPrasadVR 

How to use method results in Another method with in the same class?

Hi All,

 

     I have two methods in controller . i want first results in to second method . I am not sure how to do this can any one help me out.

 My first Method

public void getresult() {

List<Expense__C> Exp = [select id
                            From Expense__C where status__C='Pending Approval'or status__C='Approved By Team Lead' or status__c='Approved By PM'];
    System.Debug('==============EXPENSE RECORDS LIST=========================='+exp.size());
     List<Id> ExpIds = new List<Id>();  here i am storing all ids in one list
            for (Expense__C Ex: Exp)
            {
                ExpIds.add(Ex.id);
    
            }  
       getapprovalist(); }  }

second method

 Private void getapprovalist() {

List<processInstanceWorkItem> work = [select processInstance.id,ProcessInstance.TargetObjectId, createdDate, actor.name, actor.id                
                from ProcessInstanceWorkitem
                where actor.id = :userId and ProcessInstance.TargetObjectId IN:ExpIds];  here i want to use expids list 

}

 

here i am getting error as variable doesn't exit Expids.

Prafull G.Prafull G.

Modified the second method to have a parameter as List<Id> and then use the prepared list (in first method) to pass as argument in second method.

 

i.e.

...

...

getapprovalist(ExpIds);

 

and update method syntax as 

 

Private void getapprovalist(List<Id> ExpIds) {

...

...

 

Hope it helps.

 

-P

PrasadVRPrasadVR

Hello,

    

     It's working good. i have one more doubt  can u give me any idea on this.

 

 public void fetch() {
   if(types=='Expense') {
  List<Expense__C> Exp = [select id
                            From Expense__C where status__C='Pending Approval'or status__C='Approved By Team Lead' or status__c='Approved By PM'];
    List<Id> ExpIds = new List<Id>();
            for (Expense__C Ex: Exp)
            {
                ExpIds.add(Ex.id);
    
            }
       getapprovalist(Expids);              }
    else {
       if(types=='Position') {            else condition if condition one fails it will come to here and we will do some query's
           if(val =='New Position') {
          List<Position__C> pos = [select id from position__C where status__C='New Position'];
          ExpIds.clear();
          List<Id> ExpIds = new List<Id>();  here i am stroing all ids
             for(Position__C p:pos) {
                ExpIds.add(p.id);
             }
          getapprovalist(Expids);   here i need to call again same method and need to pass list results to method
           }
       }
    }
   }

private void getapprovalist(list<Id>ExpIds,List<id>ExpIds1) {
   
  List<processInstanceWorkItem> work = [select processInstance.id,ProcessInstance.TargetObjectId, createdDate, actor.name, actor.id                
                from ProcessInstanceWorkitem
                where actor.id = :userId and ProcessInstance.TargetObjectId IN:ExpIds];    i want to pass ids to here for compare.

 

 

What I am exactly means I have pick list in My vf page ther i will select type of object and condition based on that i need query all prnding approvals for that object
.

So insted of writing all quries in every condition will write one method in that will all the pending approval records for that i need to pass different object ids to that method for comparision.

 


  

Prafull G.Prafull G.

Ok. So it means your method getapprovalist accepts 2 List<Id> arguments. and these list are being prepared in above fetch method.

 

What I recon, create the 2 variables at Class level (currently these are in if statements and therefore there scope is within if statement only). and then populate these in the same way your are doing currently.

 

Once this is done then you probably want to call the getapprovalist method once if-else is finished and pass both list parameters.

 

does it make sense to you? Let me know if you need any help.

PrasadVRPrasadVR

Yes It's working.

 

And i have a method in one controller i want to use that method in my second controller. hope we can do this along with i want add some more logic to that method in my second controller Is it possible???

Prafull G.Prafull G.
Yes, It is possible. You have to create class instance and then call the method using instance.

For example
You need to write the following code in Controller 2
// Instantiate controller 1
Controller1 class1 = new Controller1();
// call method
class1.myMethod1();
PrasadVRPrasadVR

ya, we can do this , what i am asking is i need to add aditional logic to that method.

 

You need to write the following code in Controller 2
// Instantiate controller 1
Controller1 class1 = new Controller1();
// call method
class1.myMethod1(here will pass the paremeters); like this we can use this method in controller 2 along with this i want to add some more logic to this in my controoler 2 and it doesn't effect to first class functionality.

Prafull G.Prafull G.
Yes. You can do that. Add additional logic to myMethod1() that will execute only when you pass some parameter and matches.

Cheers!
-P
PrasadVRPrasadVR

Can you explain with one example how to do this??

Prafull G.Prafull G.

public Controller1 {

  public void myMethod1(String execAdditional) {

    // some logic here i.e. common that is independent to the parameter

    if(execAdditional) {

        System.debug('===YOU ARE IN SPECIAL IF CLAUSE WHICH WILL EXECUTE ONLY WHEN execAdditional = TRUE===');

    }

  }

}

 

Here if the execAdditional = true then only the if clause will be executed otherwise not.

 

Regards,