I've tried alot of solutions for this issue but when I install the package then I received an email with exception info as below:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.
Please find below my code:
global class PostInstallClass implements InstallHandler {
global void onInstall(InstallContext context) {
User u = GetUserById(context.installerId());
try
{
if(!Test.isRunningTest()) UpdatePackageInstallationInfo(context.installerId(), u.Email);
}
catch(Exception e)
{
SendMessage(new String[]{'[email protected]'},'Package failed to install/upgrade','Failed to install/upgraed package. ' + u.Email + ' tried to install/upgraed the package but failed because. Error Details: ' + e);
}
}
public User GetUserById(ID id)
{
User u = [Select Id, Email from User where Id =:id];
return u;
}
@future(callout=true)
public static void UpdatePackageInstallationInfo(String organizationId, String userEmail)
{
String url = 'http://demoapi.sunapplabs.com/api/salesforce/updateinstallpackage?organizationid='+organizationId;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse responseResult = h.send(req);
if(responseResult.getBody() == 'true')
{
SendMessage(new String[]{userEmail, '[email protected]'},'Package install successful','Thanks for installing the package. Response result : '+responseResult);
}
else
{
SendMessage(new String[]{'[email protected]'},'Package failed to install/upgrade','Failed to install/upgraed package. ' + userEmail + ' tried to install/upgraed the package but failed because of failure from MyAPI response. Response result : '+responseResult);
}
}
public static void SendMessage(String[] toAddresses, String subject, String message)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setReplyTo('[email protected]');
mail.setSenderDisplayName('My Package Support');
mail.setSubject(subject);
mail.setPlainTextBody(message);
Messaging.sendEmail(new Messaging.Email[] { mail });
}
}