+ Start a Discussion
karthik karthikkarthik karthik 

How to return Two datatypes from single method in Apex

Hi Everyone,

I would like to know can we return Multiple datatypes from single method in apex, Can anyone please help me .

Thanks.
Best Answer chosen by karthik karthik
Marek Kosar_Marek Kosar_
Hello,
one way is to create wrapper class, for example:
public class wrapperClass{
   Integer int;
   String str;
   Account acc;
}
And use this class as a return value of your method:
public static wrapperClass methodToReturnWrapper(){

    wrapperClass wc = new wrapperClass();
    //here you can fill in wrapper with values you want, like wc.int = 1
    return wc;
}

Marek

 

All Answers

Marek Kosar_Marek Kosar_
Hello,
one way is to create wrapper class, for example:
public class wrapperClass{
   Integer int;
   String str;
   Account acc;
}
And use this class as a return value of your method:
public static wrapperClass methodToReturnWrapper(){

    wrapperClass wc = new wrapperClass();
    //here you can fill in wrapper with values you want, like wc.int = 1
    return wc;
}

Marek

 
This was selected as the best answer
karthik karthikkarthik karthik
Thank You Marek :)
ranveer singh 8ranveer singh 8
 well explained Marek Kosar
Veena GopalVeena Gopal
good explanation Marek Kosar. Thank you.
Christ Valentin 28Christ Valentin 28
NIce work Marek Kosar. I always had a little trouble understanding wrappers. I get it now. 

Thanks
Han SoloistHan Soloist
Hi, how can I retrieve the return of wrapperClass? for instance I wanted to use it on my testSetup. thanks