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
Lightning UserLightning User 

UserInfo.getSessionId() returns null

Hi all,

   This one puzzles me..plain and simple, the code works but I'm getting null for UserInfo.getSessionId(). Someone tell me why?! :robotmad: Thanks. 

public class AccountUpdater {

//Future annotation to mark the method as async.
@Future(callout=true)
public static void updateAccount(String id, String name) {

//construct an HTTP request
HttpRequest req = new HttpRequest();
User currentUser = [select ServerURL__c from User where id = :UserInfo.getUserId()];
req.setEndpoint('https://example.com/dbaccess/connect.php?sessionid=' + UserInfo.getSessionId() +
'&serverurl=' + currentUser.ServerURL__c + '&module=test&id=' + id);
req.setMethod('GET');

//send the request
Http http = new Http();
HttpResponse res = http.send(req);

//check the response
if (res.getStatusCode() == 200) {
//update account
Account acc = new Account(Id=id);
acc.Description = res.getBody();
update acc;
} else {
System.debug('Callout failed: ' + res);
}
}
}

 

 

David VPDavid VP

My guess is : You're running this method with @future (asynchronously). That means that it will get called by the system and not in the user's context.

 

David

Lightning UserLightning User

Ok, thanks for telling me why it might not show up...but I'd like to know how to get it to show up too... because I took out the @Future(callout=true) and I got this error:

 

 

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger descriptionUpdater caused an unexpected exception, contact your administrator: descriptionUpdater: execution of AfterUpdate caused by: System.CalloutException: Callout from triggers are currently not supported.: Class.AccountUpdater.updateAccount: line 16, column 24

 

 

 

 

Message Edited by Gillberg on 06-15-2009 10:04 AM
David VPDavid VP

Yup,

 

You can't do external callouts from Triggers (read up on it in the docs).

The way to solve this is to keep the @future method, do the getUserInfo() in your trigger and then pass the userId as a String to your @future method. You can then query there for the rest of the userinfo you need with that userId.

 

 

 

David

Lightning UserLightning User
Thanks for the tip, I used UserInfo.getSessionId() in the trigger and passed it to the method and now I see it.
sforce2009sforce2009
That was helpful for me even. thanks for that
asgirisoftasgirisoft

David,

 

I have a future callout from a time-based trigger. This callout in turn calls a Visualforce page.

On the trigger, UserInfo.getSessionId() returns null. Please suggest a way to obtain the sessionid?

What is the validity time for a SFDC sessionid?

 

Andi Giri

Softsquare

www.softsquare.biz

Federico LarsenFederico Larsen

Hi I am trying to do quiet the same thing.

The problem is the the trigger is being fired by a Time-Dependent workflow with a field update.

 And in that context the sessionid is null in trigger, then a null sessionid  is passed.

 

 Any insight?

 

thanks in advance.