如何使用 Salesforce 在个案类型中实施完整搜索?

How To Implement Full Search in Case Type using Salesforce?

我需要构建一个解决方案,以在所有 3 个级别字段中的新案例类型数据对象上创建搜索字段,并根据选择进行填充。

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.

这是顶点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;  
            }
                
                 
                
                

            }
            
            

这是选择列表处理程序组件

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

此致, 卡罗琳

您的要求太高了,我们不会有您的自定义对象。这是旧代码,ui:inputSelect is deprecated for 1 year now。我会尽力帮忙,但整个事情也需要你的工作。以及我们可以轻松重现的示例。

我要作弊并使用 Philippe Ozil 的 ready component for the lookup/autocomplete thing。 这意味着您必须先在您的组织中保存 LookupSearchResult class, the whole aura component and 2 aura events,然后才能阅读下文。这是一些准备工作,但它是 battle-tested :)

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

光环成分(html部分)

<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 组件 - JS 控制器部分

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