0

I have a lightning component form that creates an Opportunity record from Account. On the form, I have an input field which is the Account lookup field that I am prepopulating with the ID of the specific account record where the lightning component form was triggered. It is prepopulating on the form successfully but my problem is after hitting save, I check on the created Opportunity record and the account lookup field is null. Is there something I need to add/change in order to relate the Opportunity on the Account?

CMP:

<aura:component controller="myCTRL" access="global" implements="flexipage:availableForAllPageTypes,force:appHostable,force:lightningQuickAction,lightning:isURLAddressable,flexipage:availableForRecordHome,lightning:actionOverride">
<lightning:navigation aura:id="navService"/>

  <aura:attribute name="recordId" type="Id"/>
  <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<div aura:id="myform"  aria-hidden="false" role="dialog">
    <section role="dialog" tabindex="-1" class="slds-modal slds-fade-in-open slds-modal_small" aria-labelledby="modal-heading-01" aria-modal="true">
        <div class="slds-modal__container">
            <div class="slds-modal__content slds-p-around_medium slds-wrap" id="modal-content-id-2">                   
                    <lightning:recordEditForm aura:id="recordEditForm" objectApiName="Opportunity" onsubmit="{!c.handleSubmit}">
                    <div class="slds-grid">
                        <div class="slds-col slds-size_1-of-2">
                            <lightning:inputField aura:id="accId" fieldName="AccId" value="{!v.recordId}" required="true"/>
                        </div>
                    </div>
                </lightning:recordEditForm>
            </div>               
            <div class="slds-modal__footer">
                <div class="slds-grid slds-grid_align-center">
                    <button class="slds-button slds-button_neutral" aria-label="Cancel and close" onclick="{!c.cancel}">Cancel</button>
               
                    <button class="slds-button slds-button_brand" type="submit" onclick="{!c.saveOpp}">Save</button>
                </div>
            </div>
        </div>
    </section>
    <div class="slds-backdrop slds-backdrop_open" role="dialog"></div> 
</div>

</aura:component>

JS CTRLR:

({

doInit: function(component) {
    var pageRef = component.get("v.pageReference");
    var state = pageRef.state; // state holds any query params
    var base64Context = state.inContextOfRef;
    
    if (base64Context.startsWith("1\.")) {
        base64Context = base64Context.substring(2);
    }
    var addressableContext = JSON.parse(window.atob(base64Context));
    component.set("v.recordId", addressableContext.attributes.recordId);
},

handleSubmit: function (component, event) {
    event.preventDefault();

    const fields = event.getParam('fields');
    fields.AccId = component.get('v.recordId');
    component.find('recordEditForm').submit(fields);
},

saveOpp: function(component, event) {

    var newOpp = component.get("v.newOpp");
    var action = component.get("c.createOpportunity");

    action.setParams({
        "opp": newOpp
    });

    // Configure the response handler for the action
    action.setCallback(this, function(response) {
        var state = response.getState();

        if(state === "SUCCESS") {
            console.log("Opportunity created successfully");
            var name = response.getReturnValue();
            var recordId = component.get("v.recordId");
            //display toasts
        }
    });

    $A.enqueueAction(action);
}

})

1 Answer 1

0

While using fieldName on lightining:inputField it should be

The API name of the field to be displayed.

Documentation

On opportunity account lookup name is AccountId. Rename AccId to AccountId and it should work.

 <lightning:inputField aura:id="accId" fieldName="AccountId" value="{!v.recordId}" required="true"/>
2
  • Hi @Amit! Thank for your answer. Yes that was actually the original code I have and it is not working so I tried to change it to AccId. But yes it should be AccountId. It's being pre-populated on the form but not populated on the actual record when user hit save button
    – Charlie
    Commented Mar 29, 2022 at 12:30
  • You need to invoke handleSubmit method Commented Mar 29, 2022 at 16:44

You must log in to answer this question.

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