2

I would like to know if system.scheduled is aura enabled or not. If not is there another methode that can replace it or a way to make it enabled?

Edit final

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 doing this to make it possible to schedule the jobs i already have in my apex classes that implements schedulable.

The idea is I'm trying to create a lightning page which schedules Apex Classes without going to Setup to set a schedule or using the developpers console.

To be more precise what i'm trying to achieve is a page where i can choose which Apex Class i want to schedule and also the ability to choose a schedule for it using lightning input variables.

<template>
    <h2>Planning Apex Action    </h2>
    <div>
        <lightning-input label="Job Name" value={jobName}></lightning-input>
        </div>
    <h3>Apex Action</h3>
    <template if:true={apexclass.data}>
            <template for:each={apexclass.data} for:item="apex">
                    <lightning-input type="checkbox" key={apex.Id} label={apex.Name} value={apex.Name}></lightning-input>
            </template>
    </template>
    <div>
        <h3>Time of execution</h3>
        <lightning-input name="hours" label="Hour" value={hour}></lightning-input>
        <lightning-input name="minutes" label="Minute" value={minute} ></lightning-input>
    </div>
    <div>
        <h3>Day(s) of week</h3>
        <lightning-input type="checkbox" name="Day of the week" label=Monday value='MON'></lightning-input>
        <lightning-input type="checkbox" name="Day of the week" label=Tuesday value='TUE'></lightning-input>
        <lightning-input type="checkbox" name="Day of the week" label=Wednesday value='WED'></lightning-input>
        <lightning-input type="checkbox" name="Day of the week" label=Thursday value='THU'></lightning-input>
        <lightning-input type="checkbox" name="Day of the week" label=Friday value='FRI'></lightning-input>
        <lightning-input type="checkbox" name="Day of the week" label=Saturday value='SAT'></lightning-input>
        <lightning-input type="checkbox" name="Day of the week" label=Sunday value='SUN'></lightning-input>
    </div>

    <div>
        <lightning-button label="Submit" onclick={handleClick}></lightning-button>
    </div>

</template>

Apex class to get different schedulable classes

public class SchedulableClasses {
    @AuraEnabled(cacheable=true)  
    public static List<ApexClass> SchedulableClasses(){

        ApexClass[] schedulableClasses = (ApexClass[])
  [FIND 'implements schedulable' IN ALL FIELDS RETURNING ApexClass(NamespacePrefix, Name)][0];

ApexClass[] realSchedulables = new ApexClass[0];
for(ApexClass klass: schedulableClasses) {
  try {
    Object t = JSON.deserialize('{}', 
      klass.NamespacePrefix == null? 
        Type.forName(klass.Name):
        Type.forName(klass.NamespacePrefix, klass.Name));
    if(t instanceOf Schedulable) {
      realSchedulables.add(klass);
    }
  } catch(TypeException e) {
    // Ignore
  }
}
    return realSchedulables;
    }
}

And the javascrip

import { LightningElement,wire, track} from 'lwc';
import getClass from "@salesforce/apex/SchedulableClasses.SchedulableClasses";
export default class LightningExampleInputDateTime extends LightningElement {
    @wire (getClass) apexclass;
    @track clickedButtonLabel;
    }

I didn't add the function that exist above to javascript cause it doesn't deploy to the org

  • What is Object cls argyument? – Pranay Jaiswal Jul 8 '19 at 11:34
  • @PranayJaiswal it is the class apex that i want to scheduel – Rami Abou Khalil Jul 8 '19 at 11:38
3

Your issue is not with @AuraEnabled; it's with the Apex type system.

public static string plan(String jobName, String date,Object cls){
    Object m = new cls() ;
    String jobId= system.schedule(jobName, date, m);
    return jobId;
}

This won't work in any context, because Apex does not allow that style of dynamic typing - an Object value is not a valid operand for the new operator.

Instead, you need to pass the name of the class as a String, acquire the type reference using Type.forName(), and call newInstance() on it. Note that this requires the Schedulable class have an accessible no-argument constructor; if the constructor is private or is managed and not global, you won't be able to construct it in Apex.

  • I changed the class plan and now it works i'll add it right now – Rami Abou Khalil Jul 8 '19 at 15:24
  • @RamiAbouKhalil Changing your question like that makes this answer not make sense. If you'd like to provide the final form, I'd suggest appending it at the end of your question to preserve the original context. You may also choose to accept this or any answer if it's solved the problem to your satisfaction. – David Reed Jul 8 '19 at 15:25
4

A class can implement a static, Aura enabled, method, and still implement the Schedulable interface.

But best practice is to create a controller with your @AuraEnabled method that calls a class that implements the Schedulable interface.

global class MySchedulableController implements Schedulable {

    // runs a scheduled job at midnight Sept. 3rd. 2022
    public static String CRON_EXP = '0 0 0 3 9 ? 2022';

    @AuraEnabled
    public static Id doSchedule () {
        // call schedule here
        MySchedulableController m = new MySchedulableController();
        Id jobId = System.schedule('Merge Job', sch, m);
        return jobId;
    }

    global void execute (SchedulableContext sc) {
        // do your scheduled thing here
    }
}
  • 1
    I'm not sure if i 100% get what you mean. but here's whtat i did public class plan { @AuraEnabled public static string plan(String jobName, String date,Object cls){ Object m = new cls() ; String jobId= system.schedule(jobName, date, m); return jobId; } } After doing that I couldn't depoly my files to org – Rami Abou Khalil Jul 8 '19 at 11:14
  • Please add your example to your original post. Also, for what you pasted in your comment, it seems that you want an Aura enabled method to schedule a class, is that correct? You should also edit your original post to reflect that. It looks to me that you wanted to do all this in the same class. – Renato Oliveira Jul 8 '19 at 11:19
  • I edited my main post. I'm trying to create a class that is AuraEnabled that is able to schedule my apex class that implement schedulable. – Rami Abou Khalil Jul 8 '19 at 11:25
  • In addition your method doesn't work i can't deploy to the org... – Rami Abou Khalil Jul 8 '19 at 12:13

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.