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 

If a user with profile "service care agent" tries to add the same queue which has already been assigned to the user then it should show an error "not authorized" but in all other cases it should show error message as "no records found".

component.cmp

<aura:component controller="UserDetailsController" access="global">
    
    <!--declare attributes--> 
    <aura:attribute name="lstSelectedRecords" type="sObject[]" default="[]" description="Use,for store SELECTED sObject Records"/>
    <aura:attribute name="listOfSearchRecords" type="List" description="Use,for store the list of search records which returns from apex class"/>
    <aura:attribute name="SearchKeyWord" type="string"/>
    <aura:attribute name="objectAPIName" type="string" default=""/>
    <aura:attribute name="IconName" type="string" default="standard:user"/>
    <aura:attribute name="label" type="string" default=""/>
    <aura:attribute name="UserId" type="string"/>
    <aura:attribute name="lstofgrp" type="List" />
    <aura:attribute name="TotalNumberOfRecord" type="integer" default="0"/>
    <aura:attribute name="isSelc" type="boolean" default="true"/>
    <aura:attribute name="selectedRecord" type="sObject" default="{}" description="Use,for store SELECTED sObject Record"/>
    <!--declare events hendlers-->  
    <aura:handler name="oSelectedRecordEvent" event="c:selectedsObjectRecordsEvent" action="{!c.handleComponentEvent}"/>
    <aura:attribute name="Message" type="String" default=""/>
    <lightning:layout verticalAlign="spread">
        <lightning:layoutItem flexibility="auto" size="10" padding="around-medium">
            <div class="c-container" style="width: 560px;" align="Left">
                <div aura:id="searchRes" class="slds-form-element slds-lookup slds-is-close" style="width: 530px;">
                    <label class="slds-form-element__label">{!v.label}</label>
                    <!--This part is for display search bar for lookup-->  
                    <div class="slds-form-element__control_left slds-size_2-of-2" align="Left" style="width: 530px;">
                        <div class="slds-input-has-icon slds-input-has-icon--right">
                            <!-- This markup is for when an record is selected -->
                            <div aura:id="lookup-pill" class="slds-pill-container slds-pill-container-left slds-size_2-of-2 " style="width: 530px;">
                                <aura:if isTrue="{!v.isSelc}">
                                    <div aura:id="lookupField" class="slds-combobox__form-element slds-input-has-icon slds-input-has-icon_right"  style="width: 525px;">                        
                                        <ui:inputText click="{!c.onfocus}"
                                                      updateOn="keyup"
                                                      keyup="{!c.keyPressController}"
                                                      class="slds-lookup__search-input slds-input inputSize"
                                                      value="{!v.SearchKeyWord}"
                                                      placeholder="Search Queue.."/>
                                        <span class="slds-icon_container slds-icon-utility-search slds-input__icon slds-input__icon_bottom">
                                            <lightning:icon iconName="utility:search" size="x-small" alternativeText="icon"/>
                                        </span>
                                    </div>
                                </aura:if>
                                <!--Selected Records Pills Section-->
                                <ul style="list-style-type:none">
                                    <aura:iteration items="{!v.lstSelectedRecords}" var="sr" >
                                        <li>
                                            <div style="width:520px;">
                                            <lightning:pill class="slds-m-around_xx-small pill slds-size_2-of-2 "  label="{!sr.Name}" name="{!sr.Id}" onremove="{!c.clear}" >
                                                <aura:set attribute="media" >
                                                    <lightning:icon iconName="standard:groups" size="x-small" alternativeText="icon"/>
                                                </aura:set>
                                            </lightning:pill>
                                                </div>
                                        </li>
                                    </aura:iteration>
                                </ul>
                            </div> 
                        </div>
                    </div> 
                    <!--This part is for Display typehead lookup result List-->  
                    <ul style="min-height:40px;margin-top:0px !important" class="slds-listbox slds-listbox_vertical slds-dropdown slds-dropdown_fluid slds-lookup__menu slds" role="listbox">
                        <lightning:spinner class="slds-hide" variant="brand" size="small" aura:id="mySpinner"/>
                        <Center> {!v.Message}</Center> 
                        <aura:iteration items="{!v.listOfSearchRecords}" var="singleRec">
                            <!--Child Component--> 
                            <c:UserQueueLookupResult oRecord="{!singleRec}" IconName="{!v.IconName}"/>
                        </aura:iteration>
                    </ul>
                </div> 
            </div>
        </lightning:layoutItem >
    </lightning:layout>
</aura:component>


Helper.js

({
     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);
    },
})


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;
        }
    }