+ Start a Discussion
DS777DS777 

Session id

How to get session id in an Apex Trigger code ?
werewolfwerewolf
Why would you want the session ID in an Apex trigger?
DS777DS777
I would like to tag the records with session id and then process them in trigger records only for current session id.
 
 
werewolfwerewolf
Sorry, I don't understand.  By definition the records you will process in your trigger will be only the ones inserted, updated or deleted in the context of the user's current session.
DS777DS777
By marking want to make sure that other session does not try to update the records.
Does SF lock the records?
BoxBox

nope, SFDC will not lock a record from being updated.  Could you explain what you're trying to track?

If you trying to stop a record from being processed by recursive triggers you can look into using a class with static collections to track what IDs have been processed previously, each static member will be there for the life of the thread running the triggers.



DS777DS777
Following is the transaction flow
1. I have an order with lines.
2. I select few lines and process them (Pick,pack and ship in one go or in steps)
3. Now when I select the lines - as SFDC does not lock, other session may try to select the same records and process it.
 
What is the best way to avoid above scenerio?
werewolfwerewolf
Actually Salesforce does lock records from simultaneous updates.  Also, if you're writing Apex you can do a SELECT FOR UPDATE, which will lock those records for the duration of the Apex method or trigger.
Box @ HomeBox @ Home

Ah, i was thinking more along the lines of:

- User A Opens record for viewing
- User B Opens record for viewing
- User A Changes field X and saves
- User B Changes field Y and saves

Changes on X will be lost.

I guess you could write a nice VF page that displays the order items for the users that could help you control a soft lock type fucntion but i havent really thought about this to much as i havent had anyone ask for it yet :)

Ah, i just re-read the whole post again, is the process that looks at the order lines a person, APEX thread or external application?  I have a couple of ideas of how you could control access to line items but they all depend on whats looking at it.



DS777DS777
Order lines is being looked by user. Soft locking is good option which I have used in another application.
Box @ HomeBox @ Home

Got ya, so you need a really transparent way of marking which line items are currently being worked upon.

 

I can think of options but it’s pretty difficult without knowing your business processes and how the user will interact with the system.

 

You could have a VF page that has buttons along the side of all items that haven’t been picked yet and are not currently being picked.

 

Once the user pushes the button it soft locks the record and any other user that loads the page after this action will not be able to select the item for picking.

 

If a user has already had the page loaded and tries to select an item for picking that has been selected by another user since the page loaded, you can raise a visual warning and reload the page which will give the user all of the current items that need picking.



DS777DS777

Thanks for the idea. Instead of session id - a random number will also work.

 

I guess there are ways to achieve it....

Box @ HomeBox @ Home
Yeah i think there are a few options for you, a number prefixed by the User ID so you can quickly find out who is picking the item.  Maybe, using a pool counter and a tracking object which would allow you to do analytics on how many lines are being picked per person etc etc.

Good luck with it!