0

I am trying to send an email whenever an exception occurs. The issue I think is the catch part is not firing. as the email are going when written outside catch part/

public class WebServiceCallout {

      @future (callout=true)
    public static void sendNotification
        (String name,
         String email , String Address,  string CountryId )        

    {

        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        JSONGenerator gen=JSON.createGenerator(true);
        gen.writeStartObject(); //It is used for making starting object(‘{‘)
        gen.writeFieldName('updateAccountName');
            gen.writeStartObject();
            gen.writeStringField('AccountName',name);
            gen.writeStringField('Email',email);
            gen.writeStringField('Address',Address==null?'':Address);
            gen.writeStringField('Country_of_Headquarters__c',CountryId);
            gen.writeEndObject();//It is used for making end of object(‘}’)
        gen.writeEndObject();//It is used for making end of object(‘}’)

        req.setEndpoint('http://myservice.azurewebsites.net//Service//UpdateNewAccount');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(gen.getAsString());

        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);

           ApexPages.addMessages(e);

     Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
     String[] toAddresses = new String[] {'[email protected]'};
     mail.setToAddresses(toAddresses);
     mail.setReplyTo('[email protected]');
     mail.setSenderDisplayName('Apex error message');
     mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
     mail.setPlainTextBody(e.getMessage());
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        }

    }


}

Stacktrace

enter image description here

3
  • I am unable to understand what should i write instead of system.callout exception to fire it Feb 2, 2016 at 12:03
  • 1
    Tushar as per response you are not getting any exception. You are getting bad response as a part of response. So here it would be better to check response status and act on it, like sending email. Feb 2, 2016 at 12:11
  • PLease tell me how to manage it ? Feb 2, 2016 at 12:17

1 Answer 1

1

Glad that you found it. Anyways I had prepared solution.

@future (callout=true)
public static void sendNotification(String name,String email , String Address,  string CountryId ){
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    JSONGenerator gen=JSON.createGenerator(true);
    gen.writeStartObject(); //It is used for making starting object(‘{‘)
    gen.writeFieldName('updateAccountName');
    gen.writeStartObject();
    gen.writeStringField('AccountName',name);
    gen.writeStringField('Email',email);
    gen.writeStringField('Address',Address==null?'':Address);
    gen.writeStringField('Country_of_Headquarters__c',CountryId);
    gen.writeEndObject();//It is used for making end of object(‘}’)
    gen.writeEndObject();//It is used for making end of object(‘}’)

    req.setEndpoint('http://myservice.azurewebsites.net//Service//UpdateNewAccount');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json');
    req.setBody(gen.getAsString());

    try {
        res = http.send(req);
        if(res.getStatus()='Bad Request' || res.getStatusCode() == 400){
            sendErrroMail('Bad Request Error');
        }
    } catch(System.CalloutException e) {
         System.debug('Callout error: '+ e);
         sendErrroMail(e.getMessage());              
    } 


}

public static void sendErrroMail(String errorMessage){              
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     String[] toAddresses = new String[] {'[email protected]'};
     mail.setToAddresses(toAddresses);
     mail.setReplyTo('[email protected]');  
     mail.setSenderDisplayName('Apex error message');
     mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
     mail.setPlainTextBody(errorMessage);
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

Hope this helps you.

1
  • This is exactly what i did Feb 4, 2016 at 7:35

You must log in to answer this question.

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