I want to call a method that I have created, that makes HTTP API callout to and fetches data.
Apex method:
@AuraEnabled(cacheable=true)
public static String events (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token){
try{
// GET API NAMES OF THE SELECTED DIMEONSIONS
getSites(webSite, token);
Schema.DescribeFieldResult objFieldDescribe = Object__c.Dimensions__c.getDescribe();
List<Schema.PicklistEntry> lstPickListValues = objFieldDescribe.getPickListValues();
String elementValue;
String elementLabel;
List<String> apiNamesList = new List<String>();
for (Schema.PicklistEntry objPickList : lstPickListValues) {
elementLabel = objPickList.getLabel();
if (stringColumns.contains(elementLabel)){
elementValue = objPickList.getValue();
apiNamesList.add(elementValue);
}
}
// CUSTOM ENDPOINT
String customEndpoint = webSite + 'test';
// CUSTOM BODY
Data data = new Data();
data.website_id = siteMap.get(selectedSiteName);
// BODY column
List<Data.Column> columnWrapper = new List<Data.Column>();
for (String column_id : apiNamesList) {
Data.Column c = new Data.Column();
c.column_id = column_id;
columnWrapper.add(c);
//system.debug(column_id + ' c ' + c);
//system.debug(columnWrapper);
}
data.offset = 0;
data.columns = columnWrapper;
data.date_from = date_From;
data.date_to = date_to;
data.format = 'json';
// MAKE THE API CALL
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','Bearer ' + token);
req.setHeader('Accept-Encoding', 'gzip');
req.setEndpoint(customEndpoint);
req.setBody(JSON.serialize(data));
Http h = new Http();
HTTPResponse res = h.send(req);
if (res.getStatusCode() == 200){
Map<String,Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
//List<Object> responseData = (List<Object>) results.get('data');
// Return data List<Object>
//System.debug(responseData);
return res.getBody();
}
else {
System.debug(res.getBody());
System.debug(res.getStatusCode());
return null;
}
}
catch (exception ex){
return null; //return the exception
}
}
How can I make this method schedulable, so that I create the job and the user has 'say-so' when to execute this?
What I've tried so far:
global static void fetchEvents_schedule (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token, String mode,
String scheduleStartDate_Day, String scheduleStartDate_Month, String scheduleStartDate_Year,
String scheduleEndDate_Day, String scheduleEndDate_Month, String scheduleEndDate_Year,
String prefferedTime_Minutes, String prefferedTime_Hour)
{
PTEST phca = new PTEST ();
// Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year
try {
if(mode == 'Daily'){
String CRON_EXP = '0 ' + prefferedTime_Minutes + ' ' + prefferedTime_Hour + ' * ' + scheduleStartDate_Month + '-' + scheduleEndDate_Month + ' * ' + scheduleStartDate_Year + '-' + scheduleEndDate_Year;
String jobID = system.schedule('Scheduled Fetch Events Daily Job', CRON_EXP, phca);
ApexClass.events(webSite, selectedSiteName, stringColumns, date_from, date_to, token);
}
else if(mode == 'Weekly'){
}
else if(mode == 'Monthly'){
}
}
catch(Exception e){
}
}
// ====================================================== execute ==============================================================
global static void execute(SchedulableContext sc){
ApexClass phc = new ApexClass();
}
This is based on this solution - Link 1 I'm trying to call the 'worker' method from the class that implements 'schedulable' and to @wire that to the LWC but it doesn't seem to work. Also, I'm building dynamic CRON expression so that the user has input on when to start and finish the execution.
My main question is - how can I make that method schedulable?
Thank you.
EDIT 1 - 10/27/2022
I have null for par1, par2 and par3 when the scheduled job executes. What might be the problem?
global with sharing class HttpCallout Implements Schedulable {
public static String parameter1;
public static String parameter2;
public static String parameter3;
public static void schedule(String par1, String par2, String par3){
HttpCallout hc = new HttpCallout();
parameter1 = par1;
parameter2 = par2;
parameter3 = par3;
system.debug(par1);
String CRON_EXP = '0 39 10 27 10 ?';
String jobID = System.schedule('Scheduled Job', CRON_EXP, hc);
}
global static void execute(SchedulableContext sc){
firstMethod(parameter1, parameter2, parameter3);
}
@future(callout=true)
global static void firstMethod(parameter1, parameter2, parameter3) {
String str = HttpCall.getBearerKey(parameter1, parameter2, parameter3);
system.debug(par1);
}