Dynamically switching companies w/ SetCompany

Hi! This is likely a far more advanced topic but I hope someone has done a deep dive into it in the past.

In an effort to automate the mechanism to switch between Kinetic companies I’m having to do a deeper dive into context and session management. The goal is to programmatically perform the switch as the action of clicking between them is cumbersome and slow. I’m also making the assumption that there’s absolutely no way to do this in Application Studio.

As a full-stack developer I’ve been reverse engineering how Kinetic stores the current session context and it seems like it’s a mix between server-side storage and SessionStorage/LocalStorage.

It seems that there’s a SetCompany call available vie the Ice.Lib.SessionModSvc. I can touch the endpoint and supply the company ID which performs the switch and returns a parameters object containing relevant details about the switch and the new company.

I don’t believe I can trigger arbitrary fetch calls with Application Studio so I’m thinking of implementing this as a bookmarklet in the browser.

My questions are as follows:

  • Calling the fetch will still require a page refresh, which isn’t ideal. Is there a way to call the switch via the Console (a function trigger or something similar)? I could run that via the bookmarklet. I realize that the SessionStorage needs to ultimately retain the new session info so a fetch + updating Storage is necessary as to not confuse the f/e since they’ll be out of sync.
  • Do you foresee any downsides to this method? I’m still reverse engineering the solution and I don’t know what I don’t know yet.
  • Is there definitely not an easier way to handle this? Why does Kinetic store session in the database as well as the client, this is a terrible pattern for session management that most modern web applications have moved away from. I imagine it’s a legacy requirement of the classic interface.

Thanks,

David

Not quite sure I am following - application studio has a REST widget and a page navigate-to widget, those can’t do what you need?

Oh I didn’t realize that. Even if I trigger the REST widget the client-side storage doesn’t get updated so I’m in the same situation. Ideally there would be a widget in Application Studio that performs the entire SetCompany switch for me. Am I dreaming? :stuck_out_tongue:

hmm, you can prod the Session-set widget and see if it can do what you want. I would find where you can change companies in the UI and trace out how its being done there. That would probably clarify what options you have.

Session set is useless.
I wish it were possible to change companies in app studio but as far as I know this is impossible. You can create a temporary context within a function to do stuff in another company or site. But not in app studio.

(now that I have told you its impossible, PLEASE prove me wrong lol)

Agreed, we do it quite a bit using the temporary context, via C# code in BPMs, but I haven’t had the need to do it from the UI.

We are a 1 company deal, so the effort and incentive equation is not favorable :rofl:

@josecgomez and @klincecum like to break free, maybe the challenge is interesting enough to draw them in

Yeah I’d love to see this proven out. If not I have to implement some funky hackery to make this happen. This is a pretty fundamental issue for our team because we switch between companies frequently in our customer service department.

Could you have too different browsers open, one for each company? or are there too many companies for that

I think this is the single biggest weakest of Epicor - it SUCKS when you have tons of companies and sites. People do learn to live with the pain over time. However the pain is compounded when moving to the browser as the session context does not behave appropriately at times. For example reports will no longer generate when switching companies in the browser, baq designer won’t open in the correct context, etc. So I will be here rooting for you.
Deer Popcorn GIF

1 Like

This doesn’t work - Epicor gets confused and mangles the sessions. In addition it uses up multiple licenses.

Dangit, the suckage is too strong, foiled at every turn

1 Like

Yeah exactly. The server-side session management is ultimately what creates conflict. You’d have to use two separate browsers and two users which adds to licensing fees.

This is honestly so disappointing.

1 Like

You don’t need two separate user accounts though, you just have to click enable multiple sessions in user account maintenance. However 2 concurrent licenses are consumed.But I have found that it will randomly end up in the wrong company at times and its just too confusing. Constantly having to close everything and start over to get in the right context.

Yes, about ten thousand of them. There’s a metric ton of stuff in LocalStorage, SessionStorage, cookies, and other client bits. Getting all of it updated exactly right without breaking something is non-trivial, and even then you’d still need to notify the Angular app that the session/company object changed.

Honestly, this feels like using a cannon to kill a mosquito just to avoid a quick page refresh. I’ve got 8 companies myself, and yes, it’s annoying to bounce back to the home page to switch. Epicor is already working on letting us switch within an app, but there’s no telling when that will land.

The simplest, safest option is a bookmarklet that just swaps the Company query param in the URL and refreshes. Every Kinetic app carries &Company=XXX in the URL, so replacing that and reloading is enough to update cache, session, and storage. The refresh is necessary but honestly not a big deal, and it avoids pointing a loaded shotgun at your foot.

Here’s a very minimal example:

javascript:(function(){var href=location.href,m=/([?&]company=)([^&#]*)/i.exec(href);var cur=m?decodeURIComponent(m[2]):'';var nv=prompt('New company value',cur);if(nv==null||nv==='')return;function setCompany(u,val){if(/([?&]company=)([^&#]*)/i.test(u)){return u.replace(/([?&]company=)([^&#]*)/ig,function(_,p1){return p1+encodeURIComponent(val)});}var i=u.indexOf('#');if(i>-1){var pre=u.slice(0,i),hash=u.slice(i+1);hash+=hash.indexOf('?')>-1?'&company='+encodeURIComponent(val):'?company='+encodeURIComponent(val);return pre+'#'+hash;}return u+(u.indexOf('?')>-1?'&':'?')+'company='+encodeURIComponent(val);}var newUrl=setCompany(href,nv);if(newUrl===href){newUrl+=(newUrl.indexOf('?')>-1?'&':'?')+'__force_reload__='+Date.now();}try{location.replace(newUrl);}catch(e){location.href=newUrl;}setTimeout(function(){try{location.reload(true);}catch(_){location.reload();}},50);})();

CompanySwitch

PS: the same can be done within App Studio but you’d have to modify each app to add this functionality to some button click, or use a browser extension or (if you are on prem.. add a bit of Javascript to index.html in the Application folder)

7 Likes

I don’t find that this works with certain screens though. In your example, if you then click the menu and launch baq designer, does baq designer open in the right context?

Isn’t the BAQ screen made differently than all the other apps? Probably why

1 Like

it will be changed next release, there will be company in the URL too

3 Likes

BAQ Designer is a completely different App there is no company context in the URL

1 Like