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
Vivekh RajVivekh Raj 

Calling a apex class method inside another class

Hi,
I was trying to call an method of class 1 in class 2. but i am getting this error while saving class 2.
Error: Compile Error: Constructor is not visible: <IPgap>(constructor)
Ex:
Class 1:
public class IPgap 
{
   public Opportunity  opp{get; set;}
   public IPgap (ApexPages.StandardController controller)
      {
         //Some logic
      }
   private void Info(){
        intrest = new List<SelectOption>();
     }
}

Class 2:
global without sharing class ipHelper {
    //variable declarations
 public void method1() 
    {
        IPgap ipreq = new IPgap();
        string returnValue = ipreq.Info();
    }
}

Anyone can help me.

Thanks
Vivek
Best Answer chosen by Vivekh Raj
Alexander TsitsuraAlexander Tsitsura
try this
 
global without sharing class ipHelper {
    //variable declarations
 public void method1() 
    {
         Opportunity opp = new Opportunity();
         ApexPages.StandardController sc = new ApexPages.StandardController(opp);

        IPgap ipreq = new IPgap(sc);
        string returnValue = ipreq.Info();
    }
}

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Vivekh,

It's becouse you try to call constructor without paramer, and this contructor not exists, you should pass ApexPages.StandardController to you IPgap contructor

Thanks,
Alex
Alexander TsitsuraAlexander Tsitsura
try this
 
global without sharing class ipHelper {
    //variable declarations
 public void method1() 
    {
         Opportunity opp = new Opportunity();
         ApexPages.StandardController sc = new ApexPages.StandardController(opp);

        IPgap ipreq = new IPgap(sc);
        string returnValue = ipreq.Info();
    }
}

Thanks,
Alex
This was selected as the best answer
Vivekh RajVivekh Raj
Thanks Alex. Its working
Robert_StrunkRobert_Strunk
Hi Vivekh Raj,
    The reason this is happening is because your constructor in class one is receiving a StandardController as a parameter.  There are a few ways to resolve this, but that is up for you to decide.  Here are the different ways I see to get around this.  
  1. Create a new constructor in class 1 that doesnt take in any params.
  2. In Class two's method1, pass in a standard controller into your construct of class 1.  
  3. Remove your overloaded constructor from class 1 and allow the built in constructor to be called.
Also it should be noted that your IPgap.info() method is private in scope and cannot be called from an outside class.  You should either change the methods scope to be public or create another public method in class 1 that will call the info() mehtod.  


Hope that helps!