0

Created A component to Create a New Contact And Added it to Account Page layout. now i want to Connect That new contact to Account in which we have.

Apex

public class CreateContact_Ctrl {
@AuraEnabled
    public static void getContact(string serverLastName,string serverEmail,string serverPhone){
     
            
        Contact con = new Contact();
        system.debug('value of lastName ==>' + serverLastName);
        system.debug('value of last Email ==>'+ serverEmail);
        system.debug('value of last Phone ==>'+ serverPhone);
        
        con.LastName = serverLastName ;
        con.Email = serverEmail ;
        con.Phone = serverPhone ;
        con.Department = 'Created Via Component';
            
        
         insert con;
    }
}

Aura Component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" 
                controller="CreateContact_Ctrl" access="global" >
   
    <aura:attribute name="lastname" type = "String"/>
    <aura:attribute name="Email" type = "String"/>
    <aura:attribute name="Phone" type = "String"/>
    
    
     <lightning:card footer="Click Save button to Create New Record" title="Create Contact">
        <aura:set attribute="actions">
            <lightning:button label="save" onclick="{!c.createContact}"/>
        </aura:set>
         
        <p class="slds-p-horizontal_small">            
            <lightning:input name="input3" label="Enter LastName" placeholder="type here..." value="{!v.lastname}"/>
            <lightning:input name="input3" label="Enter Email" placeholder="type here..." value="{!v.Email}"/>
            <lightning:input name="input3" label="Enter Phone" placeholder="type here..." value="{!v.Phone}"/>
        </p>
    </lightning:card>
</aura:component>

Aura Controller

({
    createContact : function(component, event, helper) {
        //console.log('method Called');
        var clientLastName = component.get("v.lastname");
        var clientEmail = component.get("v.Email");
        var clientPhone = component.get("v.Phone");
        //var clientAccount = component.get("v.Account");
        
        //call Apex Class Method
        var action = component.get("c.getContact");
        
        //Pass parameter to apex Method (First Time).....
        action.setParams({
            "serverLastName":clientLastName,
            "serverEmail":clientEmail,
            "serverPhone":clientPhone    
        });
        
              
            //Placing the request to server to run it....
            $A.enqueueAction(action,false);
            action.setCallback(this,function(respons){
            var state = respons.getState();
            if(state=== "SUCCESS"){
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams(
            {
            title: 'info',
            message:'record Created Successfully !!!',
            duration:'200',
            key:'info_alt',
            type:'info',
            mode:'dismissible'
        });
        toastEvent.fire();
        }
            
        });
        
    }
})
2
  • can you narrow down what error you are getting? your questions is very broad and asking just how to develop something. – anxiousAvocado May 17 at 2:13
  • i want to add new Contact (which created by component) to Add in existing Account – mirza Kahekashan May 17 at 9:38
0

So you need to add the Account link to the new Contact. To do that, use recordId param you have implicitly on your Aura Component

Aura component: add this near other aura:attribute rows

    <aura:attribute name="recordId" type = "String"/>

It will be prefilled automatically with Account Id Then send this Id to the server (in JS controller):

        action.setParams({
            "serverLastName":clientLastName,
            "serverEmail":clientEmail,
            "serverPhone":clientPhone,
            "serverAccountId":component.get("recordId")    
        });

Then use it in Apex

    public static void getContact(string serverLastName,string serverEmail,string serverPhone, Id serverAccountId){
     
         // ...  leave the beginning of the method as is
        
        con.accountId =  serverAccountId;           
        insert con;
    }
2
  • Record Created As before but not Added To Existing Account – mirza Kahekashan May 17 at 9:54
  • @mirzaKahekashan, can you add the console.log to see what goes into the params and check if the account Id is filled? – Jack Veromeev Jun 2 at 14:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.