1

I'm trying to create an onclick to execute an apex action

import { LightningElement,wire} from 'lwc';
import getClass from "@salesforce/apex/SchedulableClasses.SchedulableClasses";
import setPlan from "@salesforce/apex/Plan.Plan";
export default class LightningExampleInputDateTime extends LightningElement {
    @wire (getClass) apexclass;
    handleClick(){
    @wire(setPlan,{jobName:'Test',d:'0 0 13 * * ?',scheduleName:'PlanTest'}) jobId;
    }   
    }

I know the handleClick function is wrong but i can't find another method to call an apex action without using @wire

public class Plan {
   @AuraEnabled(cacheable=true)
    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;
   }
}
2

You can call Apex methods without using the wire service.

You just have to call your method defined by the import. Like this :

handleClick(){
   setPlan({jobName:'Test',d:'0 0 13 * * ?',scheduleName:'PlanTest'})
      .then(result => {
         // code executed if success
      })
      .catch(error => {
         // code executed if error
   });
}
| improve this answer | |
  • Martin was too fast :p You can't call wire inside a function. But you can use the Martin's solution. I think that another solution would be to track a variable in your handleClick() function and put the trackable variable in your wire call... – FabienHuot Jul 12 '19 at 10:04
  • Yes these value in the variables are just for testing to see if it works 100%, before tracking any variables from the HTML – Rami Abou Khalil Jul 12 '19 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.