0

I have created a component and i created a onchange event on opportunity field it called a controller method and this controller method called a apex method with opp id and return acc id so i can populated on it but i am not able to pass the opp id to the apex method.

Component

This component has record edit from

                        <lightning:inputField fieldName="Account__c" aura:id="accountIdField" value= "{!v.AccId}" />
                    </lightning:layoutItem>
                    <lightning:layoutItem size="12" smallDeviceSize="6" mediumDeviceSize="6" largeDeviceSize="6" padding="around-medium">
                        <lightning:inputField fieldName="Opportunity__c" aura:id="opportunityIdField" value= "{!v.OppId}" onchange="{!c.getaccountId}" />
                    </lightning:layoutItem>

Controller

    {
       // console.log(evt.target.value);
      var opp=component.find("opportunityIdField").get("v.value");
        var accid=component.find("accountIdField").get("v.value");
        alert("oppId  "+opp+ 'accId  '+accid);
        var action=component.get("c.getAccId");
        action.setParam({"opId" : opp});
        action.setCallback(this,function(response){
            let state=response.getState();
            let resultId = response.getReturnValue();
             
            if(state=='SUCCESS'){alert('success'+resultId);}
        });
        $A.enqueueAction(action);
    }

Apex Class

 @AuraEnabled
    public static id getAccId(String opId)
    {
        id accid;
        system.debug('called'+opId);
        if(opId!=null)
        { accid = [select id,account.id,name from opportunity where id=:opId].account.id;
         system.debug('Account id '+accid);
         return accid;
        }
        else{system.debug('not found result');
             return null;
            }
    }

1 Answer 1

1

Doc says for lightninng:inputfield:- Use event.getParam("value") to retrieve the Id of the selected contact record on the lookup field. Although the Id is returned in an array, multi-select lookups are currently not supported.

So to get the changed lookup record id, use event.getParam("value")[0] instead of component.find("opportunityIdField").get("v.value") .It will fix your issue.

<lightning:recordEditForm objectApiName="Case" onsuccess="{!c.handleSuccess}">
    <lightning:inputField fieldName="SuppliedName"/>
    <lightning:inputField fieldName="ContactId" onchange="{!c.handleChange}"/>
    <lightning:inputField fieldName="Description" />
    <div class="slds-m-top_medium">
        <lightning:button label="Create Case" type="submit"/>
    </div>
</lightning:recordEditForm>

and JS

({
        handleChange : function(component, event, helper) {        
            var lookupId = event.getParam("value")[0];
        },
        handleSuccess : function(component, event, helper) {
            var record = event.getParam("response");
            var apiName = record.apiName; // returns "Case"
            var caseId = record.id; // returns the Id of the new record
        },
    })

You must log in to answer this question.

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