0

How do I catch an error if the insert goes wrong in my rest API or is this not needed since I get an API respone?

  @HttpPost
    global static ID createInvoice (
        String customerId,
        String addressId,
        String invoiceId,
        String invoiceType,
        String invoiceTypeLocalized,
        String invoiceDate,
        String paymentDueDate,
        String invoiceNumber,
        String startDate,
        String endDate,
        String periodDescription,
        Decimal amount,
        Decimal vatAmount,
        Decimal totalAmount 
    ) {
        invoice__c thisinvoice = new invoice__c (
            customerId__c = customerId,
            addressId__c = addressId,
            invoiceId__c = invoiceId,
            invoiceType__c = invoiceType,
            invoiceTypeLocalized__c = invoiceTypeLocalized,
            invoiceDate__c = Date.valueOf(invoiceDate.replace('T',' ')),
            paymentDueDate__c = Date.valueOf(paymentDueDate.replace('T',' ')),
            invoiceNumber__c = invoiceNumber,
            startDate__c = Date.valueOf(startDate.replace('T',' ')),
            endDate__c = Date.valueOf(endDate.replace('T',' ')),
            periodDescription__c = periodDescription,
            amount__c = amount,
            vatAmount__c = vatAmount,
            totalAmount__c = totalAmount
        );
        insert thisInvoice;
        return thisInvoice.Id;
    }  
0

Just put you insert operation in the try block and catch the exception if any i.e.

   invoice__c thisinvoice = new invoice__c (
customerId__c = customerId,
addressId__c = addressId,
invoiceId__c = invoiceId,
invoiceType__c = invoiceType,
invoiceTypeLocalized__c = invoiceTypeLocalized,
invoiceDate__c = Date.valueOf(invoiceDate.replace('T',' ')),
paymentDueDate__c = Date.valueOf(paymentDueDate.replace('T',' ')),
invoiceNumber__c = invoiceNumber,
startDate__c = Date.valueOf(startDate.replace('T',' ')),
endDate__c = Date.valueOf(endDate.replace('T',' ')),
periodDescription__c = periodDescription,
amount__c = amount,
vatAmount__c = vatAmount,
totalAmount__c = totalAmount
);

TRY{
    insert thisInvoice;
}CATCH(Exception e){
    System.debug('Your exception message here....'+e.getMessage());
}

return thisInvoice.Id;
  • Thanks! how do i cover this in testclass? – Thomas Jun 25 '18 at 8:02
  • 1
    this code will get covered as soon as you insert an invoice__c record from test class. Only try block will get covered. If you want to cover the catch block, insert an invoice__c record that may throw an exception. You may try it with not passing value to a field which is required! – Saumya Gaikwad Jun 25 '18 at 8:10
  • This code would return a null if the record is not inserted. – Javier García Manzano Jun 25 '18 at 8:28
  • The code will will return a null if it fails on insert. – Enrik Hysko Jun 25 '18 at 8:34
3

I recommend that you use a custom wrapper to return the values. This will help standardize your API. Something like this:

global class WebResponse {
  global String message;
  global Id recordId;
  global Boolean success;
}
@HttpPost
    global static WebResponse createInvoice (
        WebResponse result = new WebResponse();
        result.success = false;
        ...
        try {
            insert thisInvoice;
            result.success = true;
            result.recordId = thisInvoice.Id;
        } catch(DMLException e) {
            result.message = e.getMessage();
            RestContext.response.statusCode = 400;
        }
        return result;
    }

This method allows a standard interface, and people calling your API can choose to switch between JSON and XML, which Salesforce will automatically serialize for you. Also, the 400 status code is used to indicate an error. You should always specify a non-2xx code when an error happens. This will make it easier to understand that there was a problem.

  • Yeah, this would be the definitive approach. Anyway the owner of the question has accepted another answer, which isn't complete and it's wrong btw (I will comment what's wrong there). – Enrik Hysko Jun 25 '18 at 8:26
0

You are doing an insert, so it's a DML statement. So you can expect a DMLException error and you can make something like this:

 invoice__c thisinvoice = new invoice__c(
 ...


 try{
    insert thisInvoice;
 }
 catch(DMLException ex){
    System.debug('Something unexpected happened during the insert of invoice record: '+ex.getMessage());
 }

It's better to catch e specific type of exception, not the generic 'Exception'.

Check out this post for further understanding.

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.