2

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;
   }
}

1 Answer 1

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
   });
}
2
  • 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
    Commented Jul 12, 2019 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 Commented Jul 12, 2019 at 10:14

You must log in to answer this question.

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