3

I saw the following example of getting record :

    import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS = [
    'Contact.Name',
    'Contact.Title',
    'Contact.Phone',
    'Contact.Email',
];

export default class WireGetRecordDynamicContact extends LightningElement {
    @api recordId;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    contact;

    get name() {
        return this.contact.data.fields.Name.value;
    }
}

I want to fetch a record the same, but i receive a list of fields from the design component, for example "Phone,Industry", so i don't have it defined like in this example.

The problem is i am formatting the fields in connectedCallback()

import { LightningElement, track, api } from 'lwc';

export default class RecordProgressBar extends LightningElement {
    @track progress = 0;
    @api fields;

    fieldsFormatted = [];
    connectedCallback() { // initialize component
        var i;
        var fieldsSplit = this.fields.split(';');

        console.log(this.fieldsFormatted);
        for(i = 0; i < fieldsSplit.length; i++) {
            console.log('a' + fieldsSplit[i] + 'a');
            console.log(fieldsSplit[i].length);
            if(fieldsSplit[i].length > 0 ) {
               this.fieldsFormatted.push('Account.' + this.fieldsSplit[i]);
            }
        }
        console.log(this.fieldsFormatted);
    }
}

So how can i use @wire with fieldsFormatted, if this var is field only after init?

2
  • Dana, do you want the returned data to be in a similar format to JSON value of SObject, like { "Field1": "value1", "Field2":"value2"}? Commented Aug 18, 2019 at 13:28
  • @rahulgawale This would be great, but actually i was hoping i can still use wire in some way or avoid using Apex.
    – Dana Griff
    Commented Aug 18, 2019 at 13:31

1 Answer 1

3

You'll want to add an explicit wire handler to avoid showing errors unnecessarily. Here's a redesigned version of your code that should get you at least mostly where you're trying to go:

import { track, api, wire, LightningComponent } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class MyComponent extends LightningComponent {
    @track progress = 0;
    @api fields;
    @api recordId;
    fieldsFormatted = ['Id'];

    @wire(getRecord, { recordId: '$recordId', fields: '$fieldsFormatted' })
    wireContact({data, error}) {
        if(data) {
            this.record = data;
        }
    }

    connectedCallback() {
        this.fieldsFormatted = (this.fields || 'Id').split(';').map(field => 'Account.'+field);
    }
}
3
  • how can i show the values of "record" dynamically in the component itself?
    – Dana Griff
    Commented Aug 19, 2019 at 10:19
  • @DanaGriff Use getRecordUi to describe the field layout/labels/etc, then build an array of values with the label, data type, and value, and from there, a simple iteration should suffice. It's going to be more code than I can put in a comment, though, so you might need to ask a new question if you get stuck.
    – sfdcfox
    Commented Aug 19, 2019 at 12:14
  • Thank you very much!
    – Dana Griff
    Commented Aug 19, 2019 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .