You need to sign in to do that
Don't have an account?
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);
}
}
}
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
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
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
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
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.