+ Start a Discussion
Jacinta Roberson 4Jacinta 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?
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));
                    })
                } 
            
            }

 
Raghu NaniRaghu Nani
Hi Jacinta,

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
Jacinta Roberson 4Jacinta Roberson 4
Yes, I'm still having this issue. Any help would be appreciated.
Raghu NaniRaghu Nani
can you replace your code with below code while accessing the record.

@wire(getRecord, { recordId: '$recordId', fields: field  })
 asset;
Jacinta Roberson 4Jacinta Roberson 4
User-added image

I tried that but I'm still getting an undefined serial number.
Jacinta Roberson 4Jacinta Roberson 4
Raghu/SFDC Community- Do you have any other suggestions to fix this?
Raghu NaniRaghu Nani
Hi , Can we connect over skype, so I can check what exactly the issue is? skype: raghup101 Regards, Raghu
Jacinta Roberson 4Jacinta Roberson 4
What is your time zone (I’m on Eastern Time)? What times are you available? I will message you in the morning if that’s okay. Sent from Mail for Windows 10
Raghu NaniRaghu Nani
I am in MYT time, Yes I am fine.. Regards, Raghu
Jacinta Roberson 4Jacinta Roberson 4
okay looks like it will be too late for you when I get up in the morning. I'll try to explain the problem better for now....The @wire code provided in the salesforce documentation isn't working for me. The Serial Number value is coming back undefined when I use the google chrome tools to debug. Do you see any other issues with my syntax that could be causing this?
Raghu NaniRaghu Nani
Hi Jacinta,

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');
   } 
}
Jacinta Roberson 4Jacinta Roberson 4
Thanks for your feedback. I have to be able to pass the serial value to the endpoint. How would I implement what you mentioned and still be able to use the serial value I get in the connectedCallBack?
Raghu NaniRaghu Nani
Hi Jacita,

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
Jacinta Roberson 4Jacinta Roberson 4
I'm using connectedCallback() to ensure the code runs every time the record is updated/refreshed. Is there a way to still use the connectedcallback and retrieve the wire data first?
Jacinta Roberson 4Jacinta Roberson 4
Hi Raghu, I have to use connectedCallback so how do I ensure that I get the value of the serial number prior to the connectedCallback code running?
Raghu NaniRaghu Nani
Hi Jacinta,

Did you resolved issue?
Jacinta Roberson 4Jacinta Roberson 4
No, I'm still unsure on how to fix this. I need to keep the connectedcallback function based on the salesforce documentation. How can I get a serial value before the connectedcallback runs?
Jacinta Roberson 4Jacinta Roberson 4
Hi Raghu, Did you have any other suggestions? If not i'll add this post to another forum or create a support ticket.
Raghu NaniRaghu Nani
HI Jacika,

Sorry for late reply, I am trying. Mean while please tryout from other sources also.

Regards,
Raghu
skypeId: raghup101