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;
}
}