2

As per the Salesforce documentation, Post-Install script can perform callouts using an async operation only.

https://help.salesforce.com/articleView?id=apex_post_install_script.htm&type=5

So, I tried to do callout with future method. But, i'm not getting the session id in the future callout because of which i'm getting error while doing callout.

Note that the documentation states that a post install script:

can’t access Session IDs

so the session Id can't be included in a call to a future method.

I also tried with annotating the class as without sharing, but it didn't work. (InstallHandler runs under a special ghost user. What rights does it have?)

Is there any other way to do callout from Post-install script?

1

As far as I'm aware, you can only have a Session ID in a context where you're running as a real user. The post-install user is not a real user.

What pressing need do you have during post-install to go sending a Session ID out? Are you looping back to an org API or calling out to another system that needs to integrate back to Salesforce?

In any case, if you can't do the intended flow, you'll have to do what most other packages do: Offer a setup screen where an admin can click a button to complete initial setup steps. And frankly this is probably better than doing it automatically on their behalf during install, because you can get their consent explicitly.

  • Thanks @Charles T for your resposne. In my case, I want to do a callout(Org Tooling-API) and as not all the app user's are sysem administrator-it gives error for non admin user's. So, I tried to do that callout from Post-Install script. But no luck. Also, I think to introduce a setup screen. But looking at the current number of existing customer's it doesn't seems good solution. Can you help me with this. – Hemant Keni Jan 28 at 13:52
  • Ideally only the admin who installs or upgrades it should need to access the setup page to complete the steps. Why do you need the tooling API on post install? – Charles T Jan 28 at 14:09
  • @ Charles T, I'm totally agree with you that setup page needs to be completed by Admin user only. But, Our package don't have any setup page. So, we will have to introduce new setup page just for doing callout & the no. of existing customers will have to open that setup page once. Currently,We are using tooling API to get the Field ID in order to navigate to a standard new record page with auto-populated fields. – Hemant Keni Jan 28 at 14:33
  • 2
    Aha, well why didn't you lead with that? :) So you're using this for a URL hack, right? If you're referring to the "00N" field IDs, you don't need Tooling API for that. You can query: [SELECT DeveloperName,QualifiedApiName,NewUrl,(SELECT Id, DeveloperName, DurableId FROM Fields) FROM EntityDefinition WHERE QualifiedApiName = 'MyObj__c' LIMIT 1] and then split the Durable ID field to take only the second part after the period. – Charles T Jan 28 at 15:18
  • @ Charles T, You have made my day. Now, I can get the Field Id in SOQL with the above query. Still, I'm curious to know that is there any way to do a callout from post-install script. – Hemant Keni Jan 29 at 10:04
0

This works in general for @future methods though I don't know if there are any gotchas for the PostIntall case:

  ...
  // Call the future method
  myFutureMethod(UserInfo.getSessionId());
  ...

// Session Id passed in to future method
@future(callout=true)
public static void myFutureMethod(String sessionId) {
   ...
   // Use session Id here
   ...
}
  • Keith C, Thanks for your response. But as mentioned in the below salesforce documentation, we can't get session Id in the PostInstall script. (help.salesforce.com/…) Is there any other alternate solution for this? – Hemant Keni Jan 28 at 11:30
  • @HK Ah I see. I'll add that point to your question and give it a +1 too. – Keith C Jan 28 at 11:58
  • What about Named Credentials? In lightning we use named creds as lightning session is not API enabled? – Pranay Jaiswal Jan 28 at 19:29

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.