function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pierre Marie DartusPierre Marie Dartus 

Send callout onInstall package

Hello,

Is it possible to send an HTTPrequest at package installation ? Every time an Exception is raised : You have uncommitted work pending. Please commit or rollback before calling. My goal is to notify my server that someone installed the package to setup the application.

Here is a code example : 
global void onInstall(InstallContext context) {      
        
        try {
        	Http h = new Http();
	        String endpoint = 'myURL';
	        
	        // Send the request
	        HttpRequest req = new HttpRequest();
	        req.setEndpoint(endpoint);
	        req.setMethod('POST');
	        req.setbody('MyData');
	        
	        HttpResponse res = h.send(req);
	        
        } catch (Exception ex) {
        	Messaging.SingleEmailMessage emailTobeSent = new Messaging.SingleEmailMessage();
			list<String> listEmailMembers = new list<String>();
			listEmailMembers.add('myEmail@adress.com');
			emailTobeSent.setToAddresses(listEmailMembers);
			emailTobeSent.setSubject('Post install exception');
			emailTobeSent.setHtmlBody('Message : ' + ex.getMessage() + 'Line no : ' + ex.getLineNumber() + ' getStackTraceString ' + ex.getStackTraceString() + '' + ex.getCause());
			Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailTobeSent});
        }
    }

Any Idea ? Thanks !

Pierre-Marie

Best Answer chosen by Pierre Marie Dartus
Swati GSwati G
Hi,

It is mentioned in the documentation that InstallHandler Interface can only perform callouts using an async operation. The callout occurs after the script is run and the install is complete and committed. So, you can call a futher method to perform the callout in post installation script.

All Answers

Swati GSwati G
Hi,

It is mentioned in the documentation that InstallHandler Interface can only perform callouts using an async operation. The callout occurs after the script is run and the install is complete and committed. So, you can call a futher method to perform the callout in post installation script.
This was selected as the best answer
Pierre Marie DartusPierre Marie Dartus
It works !
Thanks for the anwser.
Pradeep dPradeep d

Hi,

Am having similar kind of issue, will you please help me out.

Thanks