function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AMRITA P PandaAMRITA P Panda 

How to use the list from apex class in the helper?

Apex class:-
  @AuraEnabled
    public static List < sObject > fetchLookUpValues(String searchKeyWord, String ObjectName, List<sObject> ExcludeitemsList, String UserId) 
    {
        try{
            
            List <Group > Careagentgroup;
            List <Group > noncareagentgroup;
            String searchKey = '' + searchKeyWord + '%';
            List <String> lstOfGroupIds = new List <String> ();
            List <Group> lstOfQueue;
            List <GroupMember> lstOfSubGroupId;
            Set<id> idGroup = new Set<id>();
            lstOfSubGroupId =  [SELECT GroupId from GroupMember WHERE UserOrGroupId=: UserId LIMIT 500];
            if(lstOfSubGroupId.size()>0){
            for(GroupMember gm : lstOfSubGroupId){
                idGroup.add(gm.GroupId); 
            }
            }
            lstOfQueue =  [SELECT Id from Group WHERE Id IN (Select Groupid from GroupMember where userOrGroupId in : idGroup) LIMIT 500];
            if(lstOfQueue.size()>0){
            for(Group q : lstOfQueue){
                lstOfGroupIds.add(q.Id);
            }
            }
            
            List<string> lstExcludeitems = new List<string>();
            for(sObject item : ExcludeitemsList ){
                lstExcludeitems.add(item.id);
            }
            Careagentgroup=[select name from group where name like'care%' AND Name LIKE: searchKey AND Id NOT IN : lstExcludeitems AND Id NOT IN : lstOfGroupIds];
            noncareagentgroup=[select name from group where Name LIKE: searchKey AND (NOT name LIKE 'care%') AND Id NOT IN : lstExcludeitems AND Id NOT IN : lstOfGroupIds];
            list<user> u=[select id,name,profile.name from user where id=:UserId];
            List <sObject> returnList = new List <sObject> ();    

                if(u[0].profile.name=='Service Care Agent'){
                    for (sObject obj: Careagentgroup) {
                                returnList.add(obj);
                            }
                }
                else{
                    for (sObject obj: noncareagentgroup) {
                                returnList.add(obj);
                            }                    
                    }
                return returnList;    
        }
        catch(Exception e){
                
            System.debug('The following exception has occurred: ' + e.getMessage());
            return null;
        }
    }

helper :-

({
     searchHelper : function(component,event,getInputkeyWord,UserId) {
        var action = component.get("c.fetchLookUpValues");
        action.setParams({
            'searchKeyWord': getInputkeyWord,
            'ObjectName' : component.get("v.objectAPIName"),
            'ExcludeitemsList' : component.get("v.lstSelectedRecords"),
            'UserId' : UserId
        });
        
        //String searchKeyWord, String ObjectName, List<sObject> ExcludeitemsList, List<Group> lstofgrp
        action.setCallback(this, function(response) {
            $A.util.removeClass(component.find("mySpinner"), "slds-show");
            var state = response.getState();
            console.log("state" + state);
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                  //console.log("storeResponse-----" +storeResponse[0].Name );
                if (storeResponse.length == 0) {
                    component.set("v.Message", 'No records found');
                } 
           
                else {
                    component.set("v.Message", '');
                }
                component.set("v.listOfSearchRecords", storeResponse); 

            }
            
        });
        $A.enqueueAction(action);
    },
})

Question is :- I want to take the values present in the "returnlist" from apex class and compare it in the helper and based on the comparision will print the message as " not authorized".
Danish HodaDanish Hoda
Hi Amrita,
You can refer below code:
if (state === "SUCCESS") {
                var storeResponse =[];
                storeResponse = response.getReturnValue();
                  //console.log("storeResponse-----" +storeResponse[0].Name );
                if (storeResponse.length == 0) {
                    component.set("v.Message", 'No records found');
                }else{
                      for(let i=0; i< storeResponse.length; i++){
                          //you can have your comparison logic here
                      }
                ​​​​​​​}

​​​​​​​
sachinarorasfsachinarorasf
Hi Amrita,

I have gone through your problem.
 
({
     searchHelper : function(component,event,getInputkeyWord,UserId) {
        var action = component.get("c.fetchLookUpValues");
        action.setParams({
            'searchKeyWord': getInputkeyWord,
            'ObjectName' : component.get("v.objectAPIName"),
            'ExcludeitemsList' : component.get("v.lstSelectedRecords"),
            'UserId' : UserId
        });
        
        //String searchKeyWord, String ObjectName, List<sObject> ExcludeitemsList, List<Group> lstofgrp
        action.setCallback(this, function(response) {
            $A.util.removeClass(component.find("mySpinner"), "slds-show");
            var state = response.getState();
            console.log("state" + state);
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                  //console.log("storeResponse-----" +storeResponse[0].Name );
                if (!$A.util.isEmpty(storeResponse) && storeResponse != undefined && storeResponse != null) {
                    component.set("v.listOfSearchRecords", storeResponse);
                } 
           
                else {
                    component.set("v.Message", 'No records found');
                }

            }
            else{
                 console.log("Unknown error");
            }
            
        });
        $A.enqueueAction(action);
    },
})


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora