0

I have the requirement to get a list of field values for a specific account and use the values to navigate to the new contact screen via lwc. My js code is like this :

    import { LightningElement,api,wire } from 'lwc';
    import { NavigationMixin } from 'lightning/navigation';
    import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
    import { getRecord } from 'lightning/uiRecordApi';
    
    const ACCOUNT_FIELDS = ['Phone','BillingStreet','BillingCity','BillingPostalCode','BillingCountry'];
    
    export default class Qa_newContact extends NavigationMixin(LightningElement) {
        @api recordId;
        @wire(getRecord, { recordId: '$recordId', fields: ACCOUNT_FIELDS }) record;
        
    
        @api invoke() {
            console.log("## new contact button clicked");
            //urlencode the field values for protection
            const defaultValues = encodeDefaultFieldValues({
                AccountId : this.recordId,
                Phone : this.record.data.fields.Phone.value
            });
    
            this[NavigationMixin.Navigate]({
                type: 'standard__objectPage',
                attributes: {                
                    actionName: 'new',
                    objectApiName: 'Contact'
                },
                state: {
                    defaultFieldValues: defaultValues
                }
            });
        }
    }

When the code is run, I am obtaining the following error message :

LWC QUICK ACTION]Cannot read properties of undefined (reading 'value')

I am suspecting that the invoke method is running before the wired method, thus, record variable is undefined. How to use the wired variable with the invoke method, assuming the wired variable is set before that ?

Or is there a better way to do what I am trying to do ?

1 Answer 1

0

Got it working :

  1. First error, it should be this.record and not just record
  2. The fields name should contain the object type also like 'Account.Phone'and not only 'Phone'.

You must log in to answer this question.

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