lwc-recipes icon indicating copy to clipboard operation
lwc-recipes copied to clipboard

how to use dynamic field and dynamic value as a default value

Open Sourabh42 opened this issue 3 years ago • 3 comments

Summary

how to add dynamic value and dynamic field in navigation state context

Salesforce Org Type

Developer Edition Org

Steps To Reproduce

No response

Current Behavior

No response

Expected Behavior

No response

Relevant Log Output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Sourabh42 avatar May 09 '22 07:05 Sourabh42

Welcome! 👋

Thank you for posting this issue. 🙇🏼‍♂️ We will come back to you latest within the next 48h (working days). Stay tuned!

github-actions[bot] avatar May 09 '22 07:05 github-actions[bot]

Hi @Sourabh42, Thank you for the idea. We will review and implement it.

satyasekharcvb avatar May 12 '22 03:05 satyasekharcvb

Hi @Sourabh42 - The defaultValues is just a JavaScript object with field names as keys, and field values as values. So you should be able to dynamically create or modify that JavaScript object before passing it to the Navigate function.

For example, here is how you could modify the NavToNewRecordWithDefaults recipe to send dynamic fields and values.

export default class NavToNewRecordWithDefaults extends NavigationMixin(
    LightningElement
) {
    // An object that stores default values for the sObject
    obj = {
        FirstName: 'Morag',
        LastName: 'de Fault',
    };

    companyName = 'Demo';
    newFieldName = 'Company';

    connectedCallback(){
        // Dynamically adding a static field name and value
        this.obj.LeadSource = 'Other';
        this.obj.LastName = 'Some other name';

        // Dynamically adding a dynamic field name and value
        this.obj[this.newFieldName] = this.companyName;
    }

    navigateToNewContactWithDefaults() {

        const defaultValues = encodeDefaultFieldValues(this.obj);

Hope this answers your question. If not, please elaborate on your question and I'll be happy to help.

adityanaag3 avatar May 20 '22 12:05 adityanaag3

I hope the above answer is helpful. As there is no further reply for a while closing this issue.

satyasekharcvb avatar Sep 09 '22 06:09 satyasekharcvb