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[] {'tushar.id@gmail.com'};
     mail.setToAddresses(toAddresses);
     mail.setReplyTo('support@id.com');
     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

  • I am unable to understand what should i write instead of system.callout exception to fire it – Tushar Narang Feb 2 '16 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. – Kiran Machhewar Feb 2 '16 at 12:11
  • PLease tell me how to manage it ? – Tushar Narang Feb 2 '16 at 12:17
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[] {'tushar.id@gmail.com'};
     mail.setToAddresses(toAddresses);
     mail.setReplyTo('support@id.com');  
     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.

  • This is exactly what i did – Tushar Narang Feb 4 '16 at 7:35

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.