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
Phuc Nguyen 18Phuc Nguyen 18 

Apex pass list results to another method

Hello All,
Having an issue passing a list to another method within a class.

So I have this code:
public list<Cand__c> tc1;

 Cand__c tc1 = [my query]
How fo I pass the results of tc1 to another method in the class?
This code below is not working.  The list is null
public void summary ( list<Cand__c> new cand)

Any suggestions would be greatly appreciated.

Cheers,
P


 
Best Answer chosen by Phuc Nguyen 18
Ashish Singh SFDCAshish Singh SFDC
Hi Phuc,

As per my understanding you're trying to pass a list from 1 method to another of sObjecttype=Cand__c.

Declare a list at class level
public class xyz{

public Llist<Cand__c> tc1;

public void method1(){
tc1 = [Select id,name from Cand__c];
method2( tc1)
}

public void method2( Llist<Cand__c> cand){
System.debug(cand.size());
}
}

Thanks,
Ashish Singh.

All Answers

Maharajan CMaharajan C
Hi Phuc,

If you are trying to pass the data from one method to another method then use the below syntax:

Public class className{
    
    Public list<Cand__c> tc1(){
        List<Cand__c> tc1list = [your query];
        summary(tc1list);
        return tc1list;
    }
    
    public void summary( list<Cand__c> cand)(){
          system.debug( ' === ' + cand);
    }
}           


if you are not trying to pass the data from one method to another method but want the local property data inside method then you can access like below :

Public class className{
    Public list<Cand__c> tc1 = [your query];
    
    public void summary()(){
          system.debug( ' === ' + tc1 );
    }
​​​​​​​}

​​​​​​​
Thanks,
Maharajan.C
Phuc Nguyen 18Phuc Nguyen 18
Hello Maharajan,
Thnak you for the reply,  I do not think I can use the voide since I have a void method.
Public void className{
    
    Public list<Cand__c> tc1(){
        List<Cand__c> tc1list = [your query];
        summary(tc1list);
        return tc1list;
    }

And I have to keep it a void method so any ideas for a nother approach?

Thanks you for your time,
P
Ashish Singh SFDCAshish Singh SFDC
Hi Phuc,

As per my understanding you're trying to pass a list from 1 method to another of sObjecttype=Cand__c.

Declare a list at class level
public class xyz{

public Llist<Cand__c> tc1;

public void method1(){
tc1 = [Select id,name from Cand__c];
method2( tc1)
}

public void method2( Llist<Cand__c> cand){
System.debug(cand.size());
}
}

Thanks,
Ashish Singh.
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Just realized something.  I need to iterate through the query.
it need to be like this 
for (Cand__c tc1 : [query where Id in: some value])
I am iterating through an Id and geetting all of the records asociated
So wil lpassing to the other method look like 
Public void method2( Llist<Cand__c> cand)
or
Public void method2(Cand__c cand)

When I have it as
Public void method2( Llist<Cand__c> cand)
It tells me that the variable does not exist.
for example:
cand.Action_Tasks__c = null;
And note.  My query is looking at parent and all of its related child records.
for (Cand__c tc1 : [
                    SELECT Id,(SELECT Id FROM child__r )
                    FROM Cand__c
                    WHERE id IN : some_set
                ])
When I run a query like below. tci is being put inside a list right?
for (Cand__c tc1 : [query where Id in: some value])

Any guidance is appreciated. Seem to be going in a circle.
Thanks
P



 
Phuc Nguyen 18Phuc Nguyen 18
Hello All,
Here is what I am seeing:
public Cand__c tc1;

 public void start(){
if (!Trigger.isDelete) {
            if (Trigger.isUpdate) {
               for (Cand__c tc1 : [
                    SELECT Id, Tasks__c , (SELECT Id FROM Actions__r )
                    FROM Cand__c
                    WHERE id IN :candId
                ])
                    system.debug('debug tc1 ' + tc1);  //tc1 has a value but I think its only a single record
                system.debug('inside bulk after soql ' + candId);
                actionTaskRollUps(tc1);
            }
        }

public void actionTaskRollUps(Cand__c newcand) {
newCand.Tasks__c = null; ---system.NullPointerException: Attempt to de-reference a null object
 for (strk__Action_Item__c ai1 : newCand.Actions__r) --- does not recognize Actions__r
When I run the debug log I can see there is a value in tc1 at this line:   system.debug('debug tc1 ' + tc1) 
But it appears to be only a single record since it is not a list. 
But when I change tc1 to a list I cannot even deploy the code since it does not see the the fields in the list.
I am really stuck so if anyone has any suggesitons please advise.

Thank you,
P