0

I created a function that I want to use in LWC, but when i first tested it the result was a ObjectObject. So i tried to test the function through Develper Console. So here is my function

public class Plan {
   @AuraEnabled
    public static string Plan(String jobName, String d, String scheduleName) {
       String jobId;
       Type scheduleClass = Type.forName(scheduleName);
       if (scheduleClass != null) {
           Schedulable m = (Schedulable) scheduleClass.newInstance();
            jobId = system.schedule(jobName, d, m);
       }
       return jobId;
   }
}

I tried using these for execution in the terminal

Plan A=new Plan('Test','0 0 13 * *  ?','PlanTest');

But I got an error Constructer not defined

6
  • You should not be able to save the class, it wd give compile time error Jul 12, 2019 at 8:43
  • @PranayJaiswal Well i'm able to save it. Jul 12, 2019 at 8:44
  • 2
    try, String jobId - Plan.plan('Test','0 0 13 * * ?','PlanTest'); Jul 12, 2019 at 8:45
  • In my knowledge, Constructor is never static and never has a return type, the fact that you can make static constuctor with return type, amazes me, it looks like its not a constuctor, but just a normal static method? Jul 12, 2019 at 8:49
  • Aha got the reason, :) stackoverflow.com/questions/18480867/… Jul 12, 2019 at 8:50

1 Answer 1

0

You are getting that error because you have not defined a parameterized constructor for class Plan.

What you have created in your class is a static method and not Constructor.

Excerpt from Using Constructors document.

The syntax for a constructor is similar to a method, but it differs from a method definition in that it never has an explicit return type and it is not inherited by the object created from it.

In your case, you have defined a static method with the same name as class name which is allowed but not recommended.

So this is how constructors should be defined

public class Plan {
    // The no argument constructor 
    public Plan() {
        // more code here
    }

    // The parameterized constructor 
    public Plan(String jobName, String d, String scheduleName) {
        // more code here
    }
}

So when you want to execute your code, you should use

Plan.Plan('Test','0 0 13 * *  ?','PlanTest'); //calling static method

instead of

Plan A=new Plan('Test','0 0 13 * *  ?','PlanTest'); 
//this code will check for parameterized constructor which is not present, hence error would be thrown

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .