My objective is to build a LWC component which will display the list of related opportunity records based on account and also enable users to update the stage and amount fields. I am getting the child records displayed correctly,but following are the issues:
- I get an error popup saying: [LWC component's @wire target property or method threw an error during value provisioning. Original error: [opps is not defined]]
- Update is not happening
Apex:
public with sharing class relatedOpp_Account {
@AuraEnabled(cacheable=true)
public static List<Opportunity> displayRelatedOpp_Account(string accId) {
return [select id, name, stagename, amount, accountId from opportunity WHERE AccountId = :accId];
}
}
JS:
import { LightningElement, api,wire, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import displayRelatedOpp_Account from '@salesforce/apex/relatedOpp_Account.displayRelatedOpp_Account';
import NAME_FIELD from '@salesforce/schema/Opportunity.Name';
import STAGENAME_FIELD from '@salesforce/schema/Opportunity.StageName';
import AMOUNT_FIELD from '@salesforce/schema/Opportunity.Amount';
import { updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
export default class UpdateRecord_LWC extends LightningElement {
@api recordId;
//data =[];
@api opps = [];
//columns = COLS;
@wire(displayRelatedOpp_Account, { accId: '$recordId' })
wiredOpps ({error, data})
{
if (error)
{
this.opps = undefined;
}
else if (data)
{
this.opps = data;
console.log(opps);
}
}
handleChange(event)
{
if (!event.target.value) {
event.target.reportValidity();
this.disabled = true;
}
else {
this.disabled = false;
}
}
updateContact()
{
const fields = {};
//fields[ID_FIELD.fieldApiName] = this.contactId;
//fields[NAME_FIELD.fieldApiName] = this.template.querySelector("[data-field='Name']").value;
fields[STAGENAME_FIELD.fieldApiName] = this.template.querySelector("[data-field='StageName']").value;
fields[AMOUNT_FIELD.fieldApiName] = this.template.querySelector("[data-field='Amount']").value;
const recordInput = { fields };
updateRecord(recordInput)
.then(() => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Contact updated',
variant: 'success'
})
);
// Display fresh data in the form
return refreshApex(this.contact);
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.body.message,
variant: 'error'
})
);
});
}
}
HTML:
<template>
<lightning-card title="LdsUpdateRecord" icon-name="standard:record">
<div class="slds-m-around_medium">
<template for:each={opps} for:item="opp">
<tr key={opp.Id}>
<td><lightning-input label="Name" value={opp.Name} data-field="Name" disabled="true" class="slds-m-bottom_x-small"></lightning-input></td>
<td> <lightning-input label="Stage Name" value={opp.StageName} data-field="StageName" onchange={handleChange} class="slds-m-bottom_x-small" required></lightning-input></td>
<td><lightning-input label="Amount" value={opp.Amount} data-field="Amount" onchange={handleChange} class="slds-m-bottom_x-small" required></lightning-input> </td>
<td><lightning-button label="Update Opportunity" variant="brand" onclick={updateContact} disabled={disabled}></lightning-button></td>
</tr>
</template>
</div>
</lightning-card>
</template>