function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Priyesh Misquith 12Priyesh 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
<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  
Best Answer chosen by Priyesh Misquith 12
ManoharKumar0408ManoharKumar0408

Everything seems correct, try putting JSON.stringify(AccountId ). when passing parameters. 

It will look something like this. 

action.setParams({ 

        "con" : component.get('v.CreateContact'), 
      "AccountId" : JSON.stringify(contc) 
})
 

 

All Answers

ManoharKumar0408ManoharKumar0408
Hi Priyesh, Are you getting accountId here, in this console.log('AccountId '+ contc) ? 
 
Priyesh Misquith 12Priyesh Misquith 12
Yes , I am getting the id in the accountid in the console.log('AccountId '+ contc)
Priyesh Misquith 12Priyesh Misquith 12
Yes , I am getting the id in the accountid in the console.log('AccountId '+ contc)  as 0017F00000dpmbkQAA and in apex controller i am getting value for con as $$contact Contact:{AccountId=[0017F00000dpmbkQAA], Email=, Phone=987654321, LastName=ytrfgg}
ManoharKumar0408ManoharKumar0408

Everything seems correct, try putting JSON.stringify(AccountId ). when passing parameters. 

It will look something like this. 

action.setParams({ 

        "con" : component.get('v.CreateContact'), 
      "AccountId" : JSON.stringify(contc) 
})
 

 

This was selected as the best answer
Priyesh Misquith 12Priyesh Misquith 12
Thank you, it worked.