0

In my LWC component JS, I have used @wire to query the current values of 2 fields (name and mobile) direct from a record in Salesforce. I am saving these values into a list.

I have another method getEmpWrapperList inside connectedCallback() which queries same fields (name and mobile) but it's using existing Apex class because it needs to query these info from a legacy system & not directly from Salesforce records.

I want to combine both results into one list - results from my @wire and the one from getEmpWrapperList.

Basically, I would want to combine savedEmpObj and this.empWrapperList results. But since connectedCallback() and @wire are self-invoked, how do I do achieve this?

emp.JS

    @wire(getRecord, { recordId: '$record.Id', fields: ["Object__c.Name__c","Object__c.Mobile__c"] })
    empRecord({ error, data }) {
        if (error) {
            console.log('error in wire', error);
        } else if (data) {
            this.empRecordName = data.fields.Name__c.value;
            this.empRecordMobile = data.fields.Mobile__c.value;

            let savedEmpObj = {};
            savedEmpObj.empName = this.empRecordName;
            savedEmpObj.empMobile = this.empRecordMobile;

        }
    }
    connectedCallback() {
    
          getEmpWrapperList({ empid: this.empid, org: this.org, empcode: this.empcode })   //Apex class to fetch data from integrated system
            .then(result => {
                this.empWrapperList = result;    //contains Name, Mobile from integrated system
                this.error = undefined;
            })
            .catch(error => {
                this.error = error;
                this.empWrapperList = undefined;
            });
    }
Jofbr
  • 455
  • 3
  • 23
newbiedev
  • 3
  • 3
  • Try this option: Add alert in both of these methods and check which one is executed first. save the list in the first method and combine this list to the second method results before rendering it to the UI. Considering output of both the methods are in same format, or else you have to convert the format before combining. – Naveen K N Jun 20 '21 at 06:15

1 Answers1

0

You could try to use an Apex class for combining those two lists by wiring records to your custom apex method getEmpList(String id) instead of using standard getRecord function and other method for getting data from the legacy system. LWC would look like this:

@track
empList;

@wire(getEmpList, { id: '$record.Id' }
wiredEmpList(value) {
  this.empList = value;
  if(value.error) {
    this.empList.error = value.error;
  } else if (value.data) {
    this.empList.data = value.data;
  }
}
Kamila O
  • 300
  • 2
  • 8