3

I'm trying to get values from a Record Page and pass that to an Apex class. My question is, since the component works for single record and I have to get some fields from that record and use it in an apex class, what is the best aproach? Pass just the record Id and query the fields in apex (just one record here) or get those fields using using import (getFields)... I'm tryning to do the last option and it isn't working. Could someone help me please? Below I'm providing a sample of what I'm trying to do.

import { LightningElement, api, wire} from 'lwc';
import testMethod from '@salesforce/apex/testClassController.testMethod';
import {getRecord, getFieldValue} from 'lightning/uiRecordApi';
import OBJ_FIELD1 from '@salesforce/schema/Contact.testField1__c'
import OBJ_FIELD2 from '@salesforce/schema/Contact.testField2__c'

const FIELDS = [
    OBJ_FIELD1
    OBJ_FIELD2
];

export default class testJS extends LightningElement {
    @api recordId;
    

    @wire(getRecord, { recordId:'$recordId', testFields: FIELDS})
    loadFields({error, data}){
        if(error){
            
        }else if(data){
            const paramField1 = getFieldValue(data, OBJ_FIELD1);
            const paramField2 = getFieldValue(data, OBJ_FIELD2);
        }
    }
    
    handleClick(){
        testMethod({recId : this.recordId}).then(response => {
            
        }).catch(error => {
            console.log('Error: ' +error.body.message);
        });
    }   
}

1 Answer 1

3

"Best Approach" is likely dependent on what you need to do. If you intend to use these (or other) field values directly in the LWC, it probably makes sense to use getRecord/getFieldValue. If you are only fetching the values to use in the Apex Controller, it is probably simpler to just pass the recordId to your controller and query there. If if makes sense for your controller method response to include any data you need from the record, it may also make sense to pass only the recordId to Apex, and pass back all data needed in the page.

As to why your current attempt is not working, it is not clear what you mean. In the code shown, you are not using the fetched field values stored in paramField1 and paramField2; you are only passing recordId to the controller method in handleClick. If you tried passing them and didn't see any data in apex, try temporarily dumping your results to the Javascript console. Also, you aren't doing anything with the error response, so it's possible there is an error you are not seeing. Try updating your wired method like so:

@wire(getRecord, { recordId:'$recordId', testFields: FIELDS})
    loadFields({error, data}){
        console.log('loadFields, recordId: ', this.recordId);
        if(error){
            console.log('error', JSON.parse(JSON.stringify(error)));
        }else if(data){
            console.log('data', JSON.parse(JSON.stringify(data)));
            const paramField1 = getFieldValue(data, OBJ_FIELD1);
            const paramField2 = getFieldValue(data, OBJ_FIELD2);
            console.log('paramField1', paramField1);
            console.log('paramField2', paramField2);
        }
    }

A couple of things to note in the above:

  • I'm outputting recordId every time this runs. You are likely to see this run more than once, and the first time it runs, recordId will be blank. That's just a thing LWC does. Once the recordId is populated it should run again.
  • when debugging in JS, use a comma instead of plus after a label; this allows your browser's javascript console to show the whole object in a collapsable inspector.
  • when debugging LWC objects, you often only get the Proxy object, which is useless; wrap in JSON.parse(JSON.stringify(x)) to see the actual data.

Based on the above, you may find:

  • there is an error, which you can address
  • there is no error, but there is no data. Confirm that recordId was populated. If not, check your targetConfig. If so, check CRUD/Sharing/FLS. Inspecting the console-logged data object may also be useful.
  • there is data, but it isn't passed to Apex. Check how you are passing it - apex param names are case-sensitive when passed from JS.
1
  • Thanks for the help Jason.
    – user92872
    Feb 11, 2021 at 17:18

You must log in to answer this question.

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