This post explains how to get daily single email message(total and remaining) limits with apex code in salesforce. For that you have to hit the rest api end url(/services/data/v51.0/limits) that will return all the salesforce limits. From that you can only get the email limits and use it. Below are the two apex classes for that. SingleEmailDailyLimit class hit the endpoint with the required headers and generated the json response . SingleEmailDailyLimitWrapper class parses the json response and gives the result in required format. Below is both the apex classes:
SingleEmailDailyLimit.cls :
public class SingleEmailDailyLimit {
public static void mailLimit(){
Http h=new http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://lovesalesforce-dev-ed.my.salesforce.com/services/data/v51.0/limits');
req.setHeader('Authorization', 'Bearer '+userinfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setMethod('GET');
HttpResponse res = h.send(req);
SingleEmailDailyLimitWrapper result = new SingleEmailDailyLimitWrapper();
result = SingleEmailDailyLimitWrapper.parse(res.getBody());
system.debug('Total daily email limit of your org is : '+result.SingleEmail.Max);
system.debug('Remaining daily email limit of your org is : '+result.SingleEmail.Remaining);
}
}
SingleEmailDailyLimitWrapper.cls :
public class SingleEmailDailyLimitWrapper {
public SingleEmail SingleEmail;
public class SingleEmail {
public Integer Max;
public Integer Remaining;
}
public static SingleEmailDailyLimitWrapper parse(String json) {
return (SingleEmailDailyLimitWrapper) System.JSON.deserialize(json, SingleEmailDailyLimitWrapper.class);
}
}
No comments:
Post a Comment