无法通过 LWC 表格通过名称检索机会

问题描述 投票:0回答:1

创建一个表单,允许您按名称查找机会并在表格中显示有关该机会的必要信息。

因此,我使用 Lightning Web 组件创建一个表单,以在用户按名称搜索时检索机会。 找到的结果必须返回带有选项卡或数据表的机会字段(StageName、Amount...)(我使用选项卡)

重要提示:LWC将显示在相关版块或首页的所有帐户上。 结果不正确,它返回条件为 false“无结果”

<template>
    <lightning-card title="Liste d'opportunités du compte" icon-name="standard:opportunity">

        <div class="slds-p-horizontal_small">
            <!--BOUTON SEARCH-->
            <lightning-input type="search" label="Rechercher" value={searchOppo} onchange={handleKeyChange}
                placeholder="Search..."></lightning-input>
            <lightning-button label="Search" onclick={handleSearch} variant="brand"></lightning-button>
        </div>
        <div if:true={messageResult} class="slds-p-horizontal_small">
            <p class="slds-p-horizontal_small">No result found</p>
        </div>
        <!--CONDITIONNEMENT TO DISPLAY RESULTS-->
        <template if:true={showSearchValues}>
            <div class="slds-p-horizontal_small">
                <ul class="slds-list_vertical-space">
                    <template for:each={opportunityList} for:item="actObj">
                        <li key={actObj.Id} onclick={handleParentSelection} data-value={actObj.Id}
                            data-label={actObj.Name} class="slds-p-around_small slds-text-link" style="cursor:pointer;">
                            {actObj.Name} </li>
                    </template>
                </ul>
            </div>
          
            <div class="slds-table">
                <table class="slds-table slds-table_bordered slds-table_cell-buffer">
                    <thead>
                        <tr class="slds-line-height_reset">
                            <th class="slds-text-title_caps" scope="col"> Name </th>
                            <th class="slds-text-title_caps" scope="col"> Stage </th>
                            <th class="slds-text-title_caps" scope="col"> Close Date </th>
                            <th class="slds-text-title_caps" scope="col"> Amount </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="slds-hint-parent">
                            <td data-label="Name">{selectedOpportunity.Name}</td>
                            <td data-label="Stage">{selectedOpportunity.StageName}</td>
                            <td data-label="Close Date">{selectedOpportunity.CloseDate}</td>
                            <td data-label="Amount">{selectedOpportunity.Amount}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </template>
    </lightning-card>
</template>
import { LightningElement, wire, track, api } from 'lwc';

import findOpportunityByAccount from '@salesforce/apex/OpportunitySearchControllerLWC.findOpportunityByAccount';

export default class displayOpportunityList extends LightningElement {

    @track opportunityName ='';

    @track opportunityList;

    @track opportunityId;

    @track messageResult = false;

    @track showSearchValues =false;

    @track selectedOpportunity;

    @api accountId;

    accountId;

    opportunities = [];

    error;

    @wire(findOpportunityByAccount, { accountId: '$accountId' }) opportunities;

    wiredOpportunities({ error, data }) {

        if (data) {

            this.opportunityList = data;

            this.showSearchValues = data.length > 0;

            this.messageResult = data.length === 0 && this.opportunityName !== '';

        } else if (error) {

            console.log(error);

        }

    }

    // METHOD //

 handleKeyChange(event) {

    this.opportunityName = event.target.dataset.label;

}
handleSearch() {
    // Perform some validation if needed
    findOpportunityByAccount({ 
        accountId: this.accountId,
        opportunityName: this.opportunityName 
    })
    .then(result => {
        this.opportunityList = result;
        this.showSearchValues = result.length > 0;
        this.messageResult = result.length === 0 && this.opportunityName !== '';
        this.error = undefined;
    })
    .catch(error => {
        this.error = error;
        this.opportunityList = [];
        this.showSearchValues = false;
        this.messageResult = true; // Show message in case of error
    });
}


}
public with sharing class OpportunitySearchControllerLWC {
    @AuraEnabled(cacheable=true)
    public static List<Opportunity>  findOpportunityByAccount(String opportunityName) {
        // Filter opportunities based on the opportunityName
        return [SELECT Id, Name, StageName, Amount FROM Opportunity WHERE Name LIKE :('%' + opportunityName + '%')];
    }
}
javascript salesforce apex soql lwc
1个回答
0
投票

在您的 Apex 类中,您的参数是

String opportunityName
,但是在您的 LWC 中,wire 方法正在传递值
accountId

如果您尝试基于

AccountId
进行检索,我认为您想要执行以下操作:

@AuraEnabled(cacheable=true)
public static List<Opportunity>  findOpportunityByAccount(String accountId) {
    // Filter opportunities based on the opportunityName
    return [SELECT Id, AccountId, Name, StageName, Amount FROM Opportunity WHERE AccountId = :accountId];
}

或者,如果您确实想按

Opportunity.Name
进行搜索,则可以将连线方法调用编辑为正确的变量。

© www.soinside.com 2019 - 2024. All rights reserved.