0

Can't Serialize getSlots results into a JSON string and i get this error 'System.JSONException: Type cannot be serialized' ! please help !

This is my code :

public class BookingServiceFslFlow {

    @InvocableMethod(label='AskGetSlotMehtod' description='cette methode à pour but de retourner une liste de rendez-vous qui seront disponibles sur FSL') 
    public static List<string> bookingmethodAsk(List<Id> saId){
        ServiceAppointment sa = [SELECT Id, EarliestStartTime, DueDate FROM ServiceAppointment 
                                 WHERE Id =:saId[0]];
        system.debug('sa '+sa);
        Id schedulingPolicyId = 'a0Y5E000007aLZvUAM';
        Id operatingHoursId   = '0OH5E000000CqVCWA0';
        Timezone tz = UserInfo.getTimeZone();
        system.debug('tz'+tz);
        
        List<FSL.AppointmentBookingSlot> slots = FSL.AppointmentBookingService.GetSlots(sa.Id, schedulingPolicyId, operatingHoursId, tz, false);
        system.debug('slots '+slots);
        string listeRDVatFSL = JSON.serialize(slots);
        List<string> lst;
        lst.add(listeRDVatFSL);
        system.debug('lst '+lst);
        
        System.debug('Returned ' + slots.size() + ' appointment slots');
        for(integer i=0; i<slots.size(); i++){
            system.debug('Slot:'+i+' Start: '+slots[i].Interval.Start+' Finish:'+ slots[i].Interval.Finish+' Grade: '+slots[i].Grade);
        }
        return lst;
    }
}
2
  • What does class FSL.AppointmentBookingSlot look like? Jul 21, 2021 at 12:30
  • @SanderdeJong It's from Field Service Lightning, specifically this. TMYK.
    – sfdcfox
    Jul 21, 2021 at 13:34

1 Answer 1

1

Some standard objects can't be serialized. You need to manually copy it to something that can be serialized.

class ABS {
  public Decimal grade;
  public DateTime startTime, endTime;
  ABS(Decimal grade, FSL.Interval interval) {
    this.grade = grade;
    this.startTime = interval.start;
    this.endTime = interval.finish;
  }
}

...

List<FSL.AppointmentBookingSlot> slots = FSL.AppointmentBookingService.GetSlots(sa.Id, schedulingPolicyId, operatingHoursId, tz, false);
ABS[] results = new ABS[0];
for(FSL.AppointmentBookingSlot slot: slots) {
  results.add(new ABS(slot.grade, slot.interval));
}
String absAsJSON = JSON.serialize(results);
2
  • Thank you for your code , i create a new class ABS , then I call it in my code for FSL lik you do. I have an error that the constructor is not defined ? please i m new in apex Jul 22, 2021 at 7:16
  • @AdilProWebMaster That was my fault; I didn't realize that Interval was a FSL.Interval instead of a DateTime. The new corrected version should work correctly.
    – sfdcfox
    Jul 22, 2021 at 8:10

You must log in to answer this question.

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