How to Set field values on lightning:recordEditForm ?

Sample Code:

Sample.cmp:

  1. <aura:component implements="force:appHostable" >  
  2.       
  3.     <aura:attribute name="AccountId" type="String"/>  
  4.       
  5.     <div class="slds-box slds-theme_default">  
  6.           
  7.         <lightning:input type = "text" value = "{! v.AccountId }" label = "Account Id"/><br/>  
  8.         <lightning:button label = "Create Opportunity" variant = "brand" onclick = "{! c.navigateToNewRecForm }"/>  
  9.       
  10.     </div>  
  11.       
  12. </aura:component>  

SampleController.js:

  1. ({  
  2.   
  3.     navigateToNewRecForm : function(component, event, helper) {  
  4.           
  5.         var evt = $A.get("e.force:navigateToComponent");  
  6.         evt.setParams({  
  7.             componentDef : "c:NewRecordForm",  
  8.             componentAttributes: {      
  9.                   
  10.                 AccountId : component.get("v.AccountId")  
  11.                   
  12.             }  
  13.         });  
  14.         evt.fire();  
  15.           
  16.     }  
  17.       
  18. })  

NewRecordForm.cmp:

  1. <aura:component controller="NewRecordFormController">  
  2.       
  3.     <aura:attribute name="AccountId" type="String"/>  
  4.     <aura:attribute name="AccountRec" type="Account"/>  
  5.       
  6.     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>  
  7.       
  8.     <div class="slds-box slds-theme_default">  
  9.           
  10.         <lightning:recordEditForm  objectApiName = "Opportunity"  
  11.                                    onsuccess = "{! c.handleSuccess }">  
  12.             <lightning:inputField fieldName="Name" value="{! v.AccountRec.Name }"/>  
  13.             <lightning:inputField fieldName="StageName"/>  
  14.             <lightning:inputField fieldName="CloseDate"/>  
  15.             <lightning:inputField fieldName="AccountId" value="{! v.AccountRec.Id }"/>  
  16.           
  17.             <lightning:button class="slds-m-top_small"   
  18.                               variant="brand"   
  19.                               type="submit"   
  20.                               name="Create" label="Update" />  
  21.               
  22.         </lightning:recordEditForm>  
  23.               
  24.     </div>  
  25.       
  26. </aura:component>  

NewRecordFormController.js:

  1. ({  
  2.   
  3.     doInit: function (component, event, helper) {  
  4.           
  5.         var action = component.get('c.getAccount');  
  6.         action.setParams({  
  7.               
  8.             acctId : component.get("v.AccountId")  
  9.               
  10.         });  
  11.         action.setCallback(this, function(response) {  
  12.               
  13.             var state = response.getState();  
  14.             if ( state === 'SUCCESS' && component.isValid() ) {  
  15.                   
  16.                 component.set('v.AccountRec', response.getReturnValue());  
  17.                   
  18.             }   
  19.               
  20.         });  
  21.         $A.enqueueAction(action);  
  22.           
  23.     },  
  24.       
  25.     handleSuccess : function(component, event, helper) {  
  26.           
  27.         var params = event.getParams();  
  28.         alert(params.response.id);  
  29.           
  30.     }  
  31.       
  32. })  

NewRecordFormController.apxc:

  1. public class NewRecordFormController {  
  2.       
  3.     @auraEnabled  
  4.     public static Account getAccount(String acctId) {  
  5.           
  6.         return [ SELECT Id, Name FROM Account WHERE Id =: acctId ];   
  7.           
  8.     }  
  9.   
  10. }  

Output:




No comments:

Post a Comment