You need to sign in to do that
Don't have an account?
Priyesh Misquith 12
passing the account id from lightning component to apex controller
I need to help to pass account id from the CreateContactreateController.js to the createContact.apxc
CreateContactreate.cmp
CreateContactreateController.js
createContact.apxc
I am getting AccountId as null in apex controller and in console log i am getting following error. "Value provided is invalid for action parameter 'AccountId' of type 'String'"
Thanks in advnace
CreateContactreate.cmp
<aura:component controller="CreateContact" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" > <aura:attribute name="AccountId" type="String" /> <aura:attribute name="CreateContact" type="Contact" default="{ sObject : 'contact', LastName:'', AccountId:'', Email: '', Phone:'' }" /> <div class="slds-p-bottom_large slds-p-left_large" style="width:500px"> <lightning:recordEditForm aura:id="recordEditForm" recordId="{!v.recordId}" objectApiName="Contact"> <lightning:inputField fieldName="LastName" aura:id="LastName" value="{!v.CreateContact.LastName}" /> <lightning:inputField fieldName="AccountId" aura:id="AccountId" value="{!v.CreateContact.AccountId}"/> <lightning:inputField fieldName="Email" aura:id="Email" AccountIdvalue="{!v.CreateContact.Email}"/> <lightning:inputField fieldName="Phone" aura:id="Phone" value="{!v.CreateContact.Phone}"/> <lightning:button aura:id="submit" type="submit" label="Save record" onclick="{!c.doSave}" class="slds-m-top_medium" /> </lightning:recordEditForm> </div> </aura:component>
CreateContactreateController.js
({ doSave : function(component, event, helper) { var action = component.get('c.createContact'); var contc = component.get('v.CreateContact').AccountId; console.log('AccountId '+ contc) action.setParams({ con : component.get('v.CreateContact'), AccountId : contc }) action.setCallback(this,function(response){ var state = response.getState(); alert(state); if(state === 'SUCCESS'|| state ==='DRAFT'){ var responseValue = response.getReturnValue(); }else if(state ==='INCOMPLETE'){ }else if(state ==='ERROR'){ var errors = response.getError(); console.log('Error',errors) if(errors|| errors[0].message){ } } },'ALL'); $A.enqueueAction(action); } })
createContact.apxc
public class CreateContact { @AuraEnabled public static contact createContact(contact con,String AccountId) { system.debug('$$contact '+con+' $$Account Id'+AccountId); if (AccountId != null){ //con.AccountId = AccountId; insert con; system.debug(' CON '+con); } return con; } }
I am getting AccountId as null in apex controller and in console log i am getting following error. "Value provided is invalid for action parameter 'AccountId' of type 'String'"
Thanks in advnace
Everything seems correct, try putting JSON.stringify(AccountId ). when passing parameters.
It will look something like this.
All Answers
Everything seems correct, try putting JSON.stringify(AccountId ). when passing parameters.
It will look something like this.