You need to sign in to do that
Don't have an account?
Jacinta Roberson 4
using @wire and field value is returning undefined
Hi Everyone,
I'm using @wire to pull back a fields value and then trying to pass it in an http callout. Unfortunately, the value is coming back undefined in the debugger and I used the salesforce documentation exactly. Does anyone see what I'm missing?
I'm using @wire to pull back a fields value and then trying to pass it in an http callout. Unfortunately, the value is coming back undefined in the debugger and I used the salesforce documentation exactly. Does anyone see what I'm missing?
import { LightningElement, track, api, wire } from 'lwc';//api, wire import { getRecord, getFieldValue } from 'lightning/uiRecordApi'; //import field reference from schema import SERIAL_FIELD from '@salesforce/schema/Asset.Serial_Test__c'; const field = [SERIAL_FIELD]; export default class TestCallout extends LightningElement { @track toDoData; @api recordId; @wire(getRecord, { recordId: '$recordId', field}) asset; //return serial number field value get serial() { return getFieldValue(this.asset.data, SERIAL_FIELD); } //make callout using fetch connectedCallback() { fetch('https://jsonplaceholder.typicode.com/todos?id'+ this.serial, //endpoint passes serial number from current sfdc record { // Request type method:"GET", headers:{ // content type "Content-Type": "application/json", // adding your access token //"Authorization": 'Basic ' + btoa(username + ":" + password), //"Authorization": "OAuth 00DB0000000EfVQ!AQwAQEiiynMU2EsBcS2PhXSQ6KQTTG.Zr0hlDHTFcGcAPqKQOBNDB0rwyASZK44fqIAVe6GrVNZPsAWJ6iqXLNBfSQ.dqvW1", } }) .then((response) => { return response.json(); // returning the response in the form of JSON }) .then((jsonResponse) => { let objData = { title : '', completed : '', }; window.console.log('jsonResponse ===> '+JSON.stringify(jsonResponse)); // retriving the response data let jsonData = jsonResponse[0]; // adding data object objData.title = jsonData.title; objData.completed = jsonData.completed; // adding data object to show in UI this.toDoData = objData; }) .catch(error => { window.console.log('callout error ===> '+JSON.stringify(error)); }) } }
Is this problem still exists?
My under standing was SERIAL_FIELD value is undefined in debug.
Let me know to proceed.
Regards,
Raghu
skype: raghup101
@wire(getRecord, { recordId: '$recordId', fields: field })
asset;
I tried that but I'm still getting an undefined serial number.
I verified, Actually your getting the value into JavaScript after connectedcallback fires.
your looking the vaue in connected callback, Connected call back is firing before wire adapter fires, so you always get the undefined only.
Please refer below code and try to move you code to new function like myOwnFunc in below code. I hope it resolve your problem.
import { LightningElement, api, wire,track } from 'lwc';//api, wire
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
//import field reference from schema
import SERIAL_FIELD from '@salesforce/schema/Asset.Serial_Test__c';
const field = [SERIAL_FIELD];
export default class AssetComponent extends LightningElement {
//@track toDoData;
@api recordId;
@track asset;
@wire(getRecord, { recordId: '$recordId', fields: field})
wiredAsset({error, data}) {
if (error) {
this.error = error;
this.asset = undefined;
} else if (data) {
this.asset = data;
this.error = undefined;
this.myOwnFunc();
}
}
//return serial number field value
get serial() {
return getFieldValue(this.asset, SERIAL_FIELD);
}
//make callout using fetch
connectedCallback() {
// eslint-disable-next-line no-alert
alert(this.asset ? this.serial : 'THis alert from connected call back');
}
myOwnFunc(){
// eslint-disable-next-line no-alert
alert(this.asset ? this.serial : 'THis alert from myOwn Function');
}
}
Sorry, Can you explain bit more clear which your looking.
My sugesstion is, instead of Connectedcallback, create new function and move webservice code into that, and then call that function from wire function.
As i mentioned in mycode, i called myOwnFunc() from wire function.
Regards,
Raghu
Did you resolved issue?
Sorry for late reply, I am trying. Mean while please tryout from other sources also.
Regards,
Raghu
skypeId: raghup101