本文介绍了如何使用 Salesforce 在案例类型中实施完整搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

与 SF Global Search 类似,我想在文本搜索字段中输入 2-3 个字符,它会在 Level1-3 字段中找到匹配的文本,并在选择时填充 Level 1-3 字段.

这是顶级课程

 公共类 PickListHandler {@AuraEnabled公共静态列表getLevel1(){列表tempLst1 = new List();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'));}返回 tempLst1;}@AuraEnabled公共静态列表getLevel2(string strName){列表tempLst2 = new List();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'));}返回 tempLst2;}@AuraEnabled公共静态列表getLevel3(string strName1,string strName2){列表tempLst3 = new List();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'));}返回 tempLst3;}@AuraEnabledpublic static String savecasetype(string level1,string level2,string level3,string id){字符串 strMsg='successfull';尝试{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);插入对象;}捕获(异常前){strMsg='错误';}返回字符串;}}

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

 

Apex 类

public with share class Stack64129038 {@AuraEnabled(可缓存=真)公共静态列表search(String searchTerm, List selectedIds){if(String.isBlank(searchTerm) || searchTerm.length() 记录 = [SELECT Id, Name, Level_1__c, Level_2__c, Level_3__cFROM Case_Type_Data__cWHERE Level_1__c LIKE :t OR Level_2__c LIKE :t OR Level_3__c LIKE :t按 Level_1__c、Level_2__c、Level_3__c 排序限制 20];/* 你也可以试验 SOSL?记录 = [FIND :('*' + searchTerm + '*') 在所有字段中返回 Case_Type_Data__c(Id, Name, Level_1__c, Level_2__c, Level_3__c)][0];*/列表结果 = 新列表();for(Case_Type_Data__c ctd:记录){results.add(new LookupSearchResult(ctd.Id, 'Case_Type_Data__c', 'standard:case_wrap_up', ctd.Name,String.join(new List{ctd.Level_1__c, ctd.Level_2__c, ctd.Level_3__c}, '; ')));}返回结果;}}

Aura 组件(html 部分)

<aura:attribute access="private";类型=列表"名称=选择"默认=[]"/><aura:attribute access="private";类型=列表"名称=错误"默认=[]"/><lightning:card title=新案例类型"><lightning:recordEditForm aura:id="myForm";objectApiName="ERT_Case_Type__c";onsubmit="{!c.onSubmit}";onsuccess={!c.onSuccess}"><闪电:消息/><c:Lookup selection="{!v.selection}";onSearch="{!c.lookupSearch}";onSelection="{!c.useSelected}";错误={!v.errors}"标签=搜索"placeholder =搜索案例类型数据"/><lightning:inputField aura:id="Level_1__c";字段名称=Level_1__c";/><lightning:inputField aura:id="Level_2__c";字段名称=Level_2__c";/><lightning:inputField aura:id="Level_3__c";字段名称=Level_3__c";/><lightning:button class="slds-m-top_small";变体=品牌"类型=提交"名称=保存"标签=保存"/></lightning:recordEditForm></lightning:card></aura:component>

Aura 组件 - JS 控制器部分

({查找搜索:功能(组件,事件,助手){//获取触发搜索事件的查找组件const lookupComponent = event.getSource();const serverSearchAction = component.get('c.search');lookupComponent.search(serverSearchAction);},useSelected: 函数(组件,事件,助手){const selection = component.get('v.selection');const 错误 = component.get('v.errors');如果(选择.长度){if(errors.length){//清除错误,如果有的话component.set('v.errors', []);}让级别 = 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:函数(组件,事件,助手){调试器;event.preventDefault();//停止表单提交var fields = event.getParam('fields');fields.Case__c = component.get('v.recordId');//链接到这个"案件component.find('myForm').submit(fields);},onSuccess:函数(组件,事件,助手){var toastEvent = $A.get("e.force:showToast");toastEvent.setParams({标题":成功!",消息":Case Type保存OK,刷新",类型":成功"});toastEvent.fire();$A.get('e.force:refreshView').fire();//重新加载页面}})

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

解决方案

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

这篇关于如何使用 Salesforce 在案例类型中实施完整搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:54