You need to sign in to do that
Don't have an account?
Need help with UserInfo.getSessionID()
Hi,
I am using UserInfo.getSessionID() method in a trigger to figure out if the trigger was called within a
specif user context (see code below).
So, In a cotroller class, I store the session information in a custom setting variable. In trigger code, I compare the stored sessionID with userInfo.getSessionID().
The issue is that I am getting different Session ID in the trigger code for the same user/session.
I don't understand how the sessionID could be different fro the same user in different place of the application.
I would appreicate any help in solving this problem.
Thanks,
\bidhan
Note: In the following trigger code, the if statement allways evalute to FALSE as SessionID__c never macthes with UserInfo.getSessionID().
trigger CreateSubEntity on Entity__c (after insert) {
Entity__c e = Trigger.new[0];
Config1__c[] configs = [select id, name,SessionID__c,
parentID__c from config1__c where SetupOwnerId = :UserInfo.getUserId()];
if ((configs.size() >= 1)
&& (configs[0].parentID__c != '')
&&(configs[0].SessionID__c == UserInfo.getSessionID())) {
Sub_Entity__c aSubEntity = new Sub_Entity__c();
aSubEntity.ParentEntity__c = configs[0].parentID__c;
aSubEntity.child__c = e.id;
insert aSubEntity;
}
}
Another observation: I created a custom formula field for a custom object. The formula field includes a call to GETSESSIONID() function.
When i print that field to a debug log file in a apex class and a trigger -- I get completely different values for the same field -- i.e, the output for apex class is completely different from out from the trigger.
I am trying to figure out user/session context in a trigger. I thought by storing userID and SessionID I should be able solve this problem. However, if userInfo.getSessionID() returns difffert values in trigger for the same user and in the same session -- not sure how I what to do. I have tried many different options and so far, no luck.
I would appreciate any help.
Thanks.
Thanks,
\bidhan
As you've found out, you can infer nothing useful from sessionId. Why isn't just userId enough ?
I need the sessionID as the same user may invoke the trigger from a different entry point like the data-loader. Since the context object is not cleaned up after a insert operation, any request coming outside the UI (data loader or any other program by the same user) will cause problem. So, why is not the context getting cleaned up – although I can handle cleaning up the context in most cases – I have no control when the user click on the browser back button or simply navigates away from to a different tab
.
The Problem I am trying to solve is to be able to create a Tree structure – for example (Entity --> (Sub) Entity). One of the requirements is that I can’t use any custom VisualForce page for creating new Entity; it would be really easy if I write custom page and write custom save and save_new methods.
Ideal solution: Overwrite save and save_new. Save can be implemented by using saveURL and passing the parent-entity ID (saveURL gives you newID – which is child ID). However, save_new_url, there is no newID; So, I can’t don anything about save_new – so, I am out of luck.
Alternate Solution (not so good): Setup a session context for the user (parented, sessionID, userID); write a trigger for the object (Entity). If the trigger get’s executed in the user/session context – establish the parent child relationship. Problem: Doesn’t work if sessonID is used.
As I have mentioned before, all I want to do is to create child object within the context of a parent object. I have parentID. when a child is being created, I just have to create a new entry in the junction table and establish parent-child relationship.