0

Hey I'm writing a little integration to my app, and was looking for some good practices of handling exceptions when there is a blank or a missing parameter passed in request body, but couldn't find any satisfying examples. Here is my peace of code which is responsible for inserting custom object visit__c to the org, it has one two Lookup fields which are crucial for me, Client_Contact__c which is just a contact in org and represents the client of visit and Owner which represent the doctor.

QUESTION

Is there any sufficient and maintainable approach of handling missing parameters passed to POST methods in request body ?

Here is my code:

@HttpPost
global static Id createVisit(String dateOfVisit, String startTime,
                             Id dentistId, Id clientId, String description) {
    RestResponse res = RestContext.response;
    Visit__c visit = new Visit__c();
    Date visitDate = Date.valueOf(dateOfVisit);
    Integer hourStart = Integer.valueOf(startTime.substring(0, 2));
    Integer minutesStart = Integer.valueOf(startTime.substring(3, 5));
    Datetime startDatetime = Datetime.newInstance(visitDate, Time.newInstance(hourStart, minutesStart, 0, 0));
    if(dentistId != null) {
        visit.OwnerId = dentistId;
    }
    if(clientId != null) {
        visit.Client_Contact__c = clientId;
    }
    visit.StartDateTime__c = startDatetime;
    visit.EndDateTime__c = startDatetime.addHours(1);
    visit.Description__c = description;
    insert visit;
    return visit.Id;
}
  • If a required parameter is null or otherwise invalid, return a 400 error (400 means "bad request") and do not attempt to process the data. Otherwise, if you are able to continue, make sure that you check for null values before attempting to use the values. Adrian's answer is an excellent demonstration of this. – sfdcfox Nov 13 '17 at 21:06
1

You can increase your flexibility by deserializing your request body. Something like the below. You also likely need to add some null checks.

class Payload
{
    final Id dentistId, clientId;
    final String dateOfVisit, startTime, description;
}
@HttpPost
global static Id createVisit()
{
    String body = RestContext.request.requestBody.toString();
    Payload payload = (Payload)JSON.deserialize(body, Payload.class);

    Datetime startDatetime, endDatetime;
    if (payload.dateOfVisit != null && payload.startTime != null)
    {
        Integer hourStart = Integer.valueOf(payload.startTime.substring(0, 2));
        Integer minuteStart = Integer.valueOf(payload.startTime.substring(3, 5));
        startDatetime = Datetime.newInstance(
            Date.valueOf(dateOfVisit),
            Time.newInstance(hourStart, minuteStart, 0, 0)
        );
        endDateTime = startDatetime.addHours(1);
    }
    // and so on
}
| improve this answer | |

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.