How To Implement Full Search in Case Type using Salesforce?

瘦欲@ 提交于 2020-11-25 03:50:20

问题


I need to build out a solution to create a search field on the new Case Type Data object in all 3 of the Level fields and populate based on selection.

Similar to SF Global Search I would like to type 2-3 characters in the text search field and it would find the matching text in the Level1-3 fields and when selected the Level 1-3 field would populate.

This is the apex class

                 public class PickListHandler {
                @AuraEnabled
                public static List<String> getLevel1(){
                List<String> tempLst1 = new List<String>();
                    for(AggregateResult  ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c  group by Level_1__c])
                {
                    tempLst1.add(''+ar.get('Level_1__c'));
                }

                return tempLst1;
                  
                  
                } 
                
                @AuraEnabled
                public static List<String> getLevel2(string strName){
                List<String> tempLst2 = new List<String>();
                   for(AggregateResult  ar : [select Level_2__c,COUNT(id) from Case_Type_Data__c where Level_1__c=:strName  group by Level_2__c])
                {
                   tempLst2.add(''+ar.get('Level_2__c'));
                }

                return tempLst2;
                  
                } 
                
                @AuraEnabled
                public static List<String> getLevel3(string strName1,string strName2){
                 List<String> tempLst3 = new List<String>();
                  for(AggregateResult  ar : [select Level_3__c,COUNT(id) from Case_Type_Data__c  where Level_1__c=:strName1 and Level_2__c=:strName2 group by Level_3__c])
                {
                   tempLst3.add(''+ar.get('Level_3__c'));
                }

                return tempLst3;
                  
                  
                } 
                     
                 @AuraEnabled
                 public  static String  savecasetype(string level1,string level2,string level3,string id){
                 string strMsg='successfull';
                      try{
                 ERT_Case_Type__c obj=new ERT_Case_Type__c();
                 Obj.Case__c = id;
                 System.debug('CASE  = '+ Obj.Case__c); 
                 Obj.Level_1__c=level1;
                 System.debug('Level1  = '+ Obj.Level_1__c); 
                 Obj.Level_2__c=level2;
                 System.debug('Level2  = '+ Obj.Level_2__c); 
                 Obj.Level_3__c=level3;
                 System.debug('Level3  = '+ Obj.Level_3__c); 
                 Insert obj;
              
                 }
                 
                catch(Exception ex){
                        strMsg='error';
                    }
                 return strMsg;  
            }
                
                 
                
                

            }
            
            

This is the Picklist handler component

                <aura:component controller="PickListHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
                    <!-- Actions-->
                    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
                    <!-- variable-->
                    <aura:attribute name="lstLevel1" type="String[]" />
                     <aura:attribute name="lstLevel2" type="String[]" />
                      <aura:attribute name="lstL3" type="String[]" />
                    <span> Level 1</span>
                    <ui:inputSelect aura:id="ddLevel1" change="{!c.getLvl1}">
                        <ui:inputSelectOption label="-Select-" value="true"/>        
                        <aura:iteration items="{!v.lstLevel1}" var="value">          
                            <ui:inputSelectOption label="{!value}" text="{!value}" />
                        </aura:iteration>
                    </ui:inputSelect>
                    <span>Level 2</span>
                    <ui:inputSelect aura:id="ddLevel2" change="{!c.getSelectedValue}">
                        <ui:inputSelectOption label="-Select-" value="true"/>        
                        <aura:iteration items="{!v.lstLevel2}" var="value">          
                            <ui:inputSelectOption label="{!value}" text="{!value}" />
                        </aura:iteration>
                    </ui:inputSelect>
                     <span>Level 3</span>
                    <ui:inputSelect aura:id="ddLevel3" >
                        <ui:inputSelectOption label="-Select-" value="true"/>        
                        <aura:iteration items="{!v.lstL3}" var="value">          
                            <ui:inputSelectOption label="{!value}" text="{!value}" />
                        </aura:iteration>
                    </ui:inputSelect>
                   <lightning:button variant="brand" label="Save" onclick="{!c.onConfirm}" />
                </aura:component>

Regards, Carolyn


回答1:


You're asking for a lot, we wouldn't have your custom object. And this is old code, ui:inputSelect is deprecated for 1 year now. I'll try to help a bit but the whole thing needs your work too. And examples we can reproduce easily.

I'm going to cheat and use Philippe Ozil's ready component for the lookup/autocomplete thing. It means you'd have to save LookupSearchResult class, the whole aura component and 2 aura events in your org before reading below. That's some prep work but it's battle-tested :)

Apex class

public with sharing class Stack64129038 {
    @AuraEnabled(cacheable=true)
    public static List<LookupSearchResult> search(String searchTerm, List<String> selectedIds){
        if(String.isBlank(searchTerm) || searchTerm.length() < 2){
            return null;
        }
        String t = '%' + searchTerm + '%'; // decide how you want to search, "starts with", "includes" or what
        
        List<Case_Type_Data__c> records = [SELECT Id, Name, Level_1__c, Level_2__c, Level_3__c
            FROM Case_Type_Data__c
            WHERE Level_1__c LIKE :t OR Level_2__c LIKE :t OR Level_3__c LIKE :t
            ORDER BY Level_1__c, Level_2__c, Level_3__c
            LIMIT 20];
        
        /* You could also experiment with SOSL?
        records =  [FIND :('*' + searchTerm + '*') IN ALL FIELDS 
            RETURNING Case_Type_Data__c(Id, Name, Level_1__c, Level_2__c, Level_3__c)][0];
        */
        
        List<LookupSearchResult> results = new List<LookupSearchResult>();
        for(Case_Type_Data__c ctd : records){
            results.add(new LookupSearchResult(ctd.Id, 'Case_Type_Data__c', 'standard:case_wrap_up', ctd.Name,
                String.join(new List<String>{ctd.Level_1__c , ctd.Level_2__c, ctd.Level_3__c}, '; ')
            ));
        }
        return results;
    } 
}

Aura component (html part)

<aura:component implements="force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="Stack64129038">
    <aura:attribute access="private" type="List" name="selection" default="[]"/>
    <aura:attribute access="private" type="List" name="errors" default="[]"/>

    <lightning:card title="New Case Type">
        
        <lightning:recordEditForm aura:id="myForm" objectApiName="ERT_Case_Type__c" onsubmit="{!c.onSubmit}" onsuccess="{!c.onSuccess}">
        <lightning:messages />
        <c:Lookup selection="{!v.selection}" onSearch="{!c.lookupSearch}" onSelection="{!c.useSelected}" errors="{!v.errors}" label="Search" placeholder="Search Case Types Data"/>
        <lightning:inputField aura:id="Level_1__c" fieldName="Level_1__c" />
        <lightning:inputField aura:id="Level_2__c" fieldName="Level_2__c" />
        <lightning:inputField aura:id="Level_3__c" fieldName="Level_3__c" />
        <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="save" label="Save" />
    </lightning:recordEditForm>
    </lightning:card>
</aura:component>

Aura component - JS controller part

({
    lookupSearch : function(component, event, helper) {
        // Get the lookup component that fired the search event
        const lookupComponent = event.getSource();
        const serverSearchAction = component.get('c.search');
        lookupComponent.search(serverSearchAction);
    },

    useSelected: function(component, event, helper) {
        const selection = component.get('v.selection');
        const errors = component.get('v.errors');
        
        if (selection.length) {
            if(errors.length){  // Clear errors, if any
                component.set('v.errors', []);
            }
            let levels = selection[0].subtitle.split('; ');
            component.find('Level_1__c').set('v.value', levels[0]);
            component.find('Level_2__c').set('v.value', levels[1]);
            component.find('Level_3__c').set('v.value', levels[2]);
        }
    },
    onSubmit: function(component, event, helper) {
        debugger;
        event.preventDefault();       // stop the form from submitting
        var fields = event.getParam('fields');
        fields.Case__c = component.get('v.recordId'); // link to "this" Case
        component.find('myForm').submit(fields);
    },
    onSuccess: function(component, event, helper){
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Success!",
            "message": "Case Type saved OK, refreshing",
            "type": "success"
        });
        toastEvent.fire();
        $A.get('e.force:refreshView').fire(); // reload page
    }
})


来源:https://stackoverflow.com/questions/64129038/how-to-implement-full-search-in-case-type-using-salesforce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!