0

We have a Campaign which has a field that counting available sits.

When a user is filling it personal data within a form, and submit the page, he redirects to a payment site (other system).

I want to give the user 15 min to pay to keep his sit, after that, if he paid I have a service from the payment system that notified me that he paid.

But if he didn't pay (like closing the browser) I will never know.

So I'm scheduling an apex class to 15 min later and check the criteria.

My issue is - In the last 15 min of the hour, it throws me the next error :

Minute and Second values must be between 0 and 59

What I'm doing is :

  • After the user is filling the form - I'm scheduling the class to run after 15 min

    String hour = String.valueOf(Datetime.now().hour());
    String min = String.valueOf(Datetime.now().minute() + 15); 
    String sec = String.valueOf(Datetime.now().second());
    String dayOfWeek = Datetime.now().format('E');
    String year = String.valueOf(Datetime.now().year());
    String month = String.valueOf(Datetime.now().month());
    String day = String.valueOf(Datetime.now().day());
    
    String nextFireTime = sec + ' ' + min + ' ' + hour + ' ' +day + ' ' +month + ' ? '  + year;
    
    
    String jobID = System.schedule('A new Job To Schedual -> ' + memberId, nextFireTime, new SchedualApexClass(memberId));
    
  • Schedule Class - the class who runs after 15 min

.

global class SchedualApexClass implements Schedulable {
    Id memberId;
global SchedualApexClass(Id memberId) {
    this.memberId = memberId;        

}


global void execute(SchedulableContext ctx){
    System.debug(ctx);
    if(this.memberId!=null){

        changeCMStatus(this.memberId,ctx);
    }
}

  private void changeCMStatus(Id memberId,SchedulableContext ctx){

    System.debug('memberId -> ' + memberId);

    if(memberId!=null){ 

        List<CampaignMember> cm = [SELECT Id,Status FROM CampaignMember WHERE Id=:memberId AND Status='Registered'];

        if(cm!=null && cm.size()>0){
            cm[0].Status = 'Canceled';
            update cm;
        }
        
    }


  } 
}

Salesforce docs also wrote about this issue here (In the NOTE) - https://help.salesforce.com/articleView?id=000314061&mode=1&sfdcIFrameOrigin=null&type=1

I'm just trying to check if the minutes is above 59 - I'll increment the hour and add the addition of the min as a new min.

Hope it will be fine. until than, Any workaround on this?

1 Answer 1

2

You're using too complicated approach to get next fire time. Try this one:

Datetime workTime = System.now().addMinutes(15);
String nextFireTime = '' + workTime.second() + ' ' + workTime.minute() + ' ' + workTime.hour()
    + ' ' + workTime.day() + ' ' + workTime.month() + ' ? ' + workTime.year();
String jobID = System.schedule('A new Job To Schedual -> ' + memberId, nextFireTime, new SchedualApexClass(memberId));
1
  • Yes That great! Thanks a lot @Mariia :)
    – Salvation
    Oct 12, 2020 at 8:42

You must log in to answer this question.

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