0

i want to pass a string from my javascript-controller to apex controller so that it can be used as a global variable(string) in Apex controller below is the code

{.....user is selecting a group name from the list of groups and im passing the group id of the selected group to apex controller so that i can use the group id to do the further operations on the server side such as connect, disconnect, send message, get all the users from the group etc....}

i want to use the group id in more than one meethod in my apex controller

controller.js

connect:function(component, event, helper){
        var selectedItem = component.get("v.selectedvalue");
        var action = component.get("c.getGroupId");
        // want to use selectedItem as a global variable (string) in apex controller
        //action.setParams({postmsg1:selectedItem});
        $A.enqueueAction(action);
    }

any help will be appreciated

2
  • 2
    What exactly are you trying to accomplish? Please read about the X-Y Problem for more information on the type of detail we're looking for.
    – sfdcfox
    Apr 8, 2021 at 2:59
  • user is selecting a group name from the list of groups and im passing the group id of the selected group to apex controller so that i can use the group id to do the further operations on the server side such as connect, disconnect, send message, get all the users from the group etc
    – Tejas
    Apr 8, 2021 at 3:03

1 Answer 1

1

Each transaction is a clean slate as far as global/static variables are concerned. You can't store "state" on the server or in between requests. This has a distinct advantage that Lightning can perform much better than Visualforce (up to 10x faster, in fact). To be absolutely crystal clear, you cannot store data in global variables, as they will not persist across transactions. You must pass in the value(s) you want to use for each separate transaction/Apex call.

4
  • is it possible to store the group Id any how in javascript,aura or apex? so that once user select the group he can perform further tasks on the group
    – Tejas
    Apr 8, 2021 at 3:42
  • 1
    @Tejas If all these other tasks like "connect", "send message" etc. need to happen on a button click, you should be passing the "groupId" in each of those calls to apex (you can retrieve it from your component.get("")). As sfdcfox alluded to, there's a shift in how to solve problems in Lightning vs Visualforce. There's no concept of a saved state between requests. Whilst this is LWC, you might find some ways of how this is done in some examples in github.com/trailheadapps/ebikes-lwc
    – markgarg
    Apr 8, 2021 at 10:24
  • 1
    @Tejas You store the Group Id in JavaScript, either as attributes (Aura) or class members (LWC). You would then pass this in as a parameter for each method you write so you can do further operations with the Group.
    – sfdcfox
    Apr 8, 2021 at 11:31
  • i figured it out that it can be done with two methods for this..1)by storing groupId in javascript (as @sfdcfox suggested)....2)by storing the group id in a record in salesforce database and acess it using select query....
    – Tejas
    Apr 9, 2021 at 3:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .