0

I need to map the AccountId when a ContactId is selected inside a lightning:recordEditForm. First I tried using an "onchange" event in the lightning:inputField, even that I was able to connect with the JS Controller and from there with the JS Helper I was unable to get response from the @AuraEnabled Method in my APEX controller.

-Solution

Moved the logic to an aura:valueChange. This is working as intended now, but my event fire twice every time I select a contact. Below is the code and the returns from the developer Console.

-New Problem & Question

Do you know why that event is being fired twice when I select a contact from my Contact lookup?

HTML COMPONENT:

<aura:handler name="change" value="{!v.proposal.Contact__c}" action="{!c.contactChange}"/>
<lightning:recordEditForm aura:id="form" objectApiName="Proposal__c" recordId="{!v.proposal.Id}" onsubmit="{!c.handleSubmit}" onsuccess="{!c.handleOnSuccess}" onerror="{!c.handleOnError}">
    <lightning:messages />

    <h3 class="slds-section-title--divider">Broker and Contact</h3>
    <div class="slds-grid slds-gutters">
        <div class="slds-col">
            <lightning:inputField fieldName="Contact__c"  value="{!v.proposal.Contact__c}"/>
            <lightning:inputField fieldName="Account__c" value="{!v.proposal.Account__c}"/>

        </div>
        <div class="slds-col">
            <lightning:inputField fieldName="Broker_Contact__c" value="{!v.proposal.Broker_Contact__c}"/>
            <lightning:inputField fieldName="Broker_Account__c" value="{!v.proposal.Broker_Account__c}"/>           

        </div>
    </div>

JS CONTROLLER (PLEASE NOTICE THAT MY HELPER CALL IS COMMENTED OUT)

    contactChange: function(cmp, evt, helper) {
      var contactId = evt.getParam("value");
      console.log('old value ' + evt.getParam("oldValue") + ' new value ' + evt.getParam("value"));
      var oldValue = evt.getParam("oldValue");
      var newValue = evt.getParam("value");
      if(oldValue !== newValue) {
        console.log('got here');

        //helper.autoFill(cmp, evt, contactId);
    }
},

JS HELPER

autoFill: function(component, event, contactId) {       
    console.log('got to autofill ' + contactId);
    var action = component.get("c.getContact");
    action.setParams({
        "conId": contactId 
    });
    action.setCallback(this, function(response){
        var state = response.getState();
        console.log('this is is state + ' + state);
        if(state === "SUCCESS") {
            var accountId = response.getReturnValue().AccountId;
            component.set("v.proposal.Account__c", accountId);
        }else {
            console.log('error');

        }
    });
    $A.enqueueAction(action); 
},

RETURNS FROM THE CHROME CONSOLE enter image description here

COMPONENT EXAMPLE

Prefill the Account on Contact selection

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.