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
veena vaniveena vani 

How to autopopulate the custom object fields?

Hi,
i am new to lightning as i am trying to autopopulate custom object fields... i have 2 custom objects those are LEAD and OPPORTUNITY i created an convert to opportunity quick action button in lead object. while coverting to opportunity the related lead fields should autopopulate in the lightning component.

here is my code:
component:
<aura:component controller="createOpp" implements="force:lightningQuickAction,force:hasRecordId" access="global">        
    <aura:attribute name="opp" type="Opportunity__c[]" default="{ 'sobjectType': 'Opportunity__c'}"/>  
    <aura:attribute name="lelist" type="Lead__c[]" default="{ 'sobjectType': 'Lead__c'}"/> 
    <aura:attribute name="er" type="boolean" default="false"/>
    <aura:attribute name="op" type="Opportunity__c" default="{ 'sobjectType': 'Opportunity__c', 
                         'Name': '',
                         'Agent__c': '',
                         'Account__c': '', 
                         'Agreements__c': '',
                          'Agent_Commission__c': '',
                          'Delivery_Terms__c': '',
                          'Description__c': '',
                          'End_Customer__c': '',
                           'End_Market__c': '',
                            'Expected_Launch__c': '',
                            'Potential_Price__c': '',
                            'Potential_Quantity__c': '',
                            'ProductName__c': '',
                            'Project_Type__c': '',
                             'Product_Specifications__c': '',
                             'UOM__c': '',}"/>
    <aura:attribute name="lea" type="Lead__c" />
    <aura:attribute name="recordId" type="Id" default="{ 'sobjectType': 'Lead__c' }"/>
    <aura:attribute name="dateValidationError" type="boolean" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:handler name="init" value="{!this}" action="{!c.mapOpp}"/>
 <div class="slds-box">
        <div class="slds-col slds-has-flexi-truncate" align="left">
        <table>
        <lightning:layout class="slds-page-header slds-page-header--object-home">
        <lightning:layoutItem >
        <lightning:icon iconName="action:new_opportunity" size="small" alternativeText="Indicates approval"/>
        </lightning:layoutItem>
        <lightning:layoutItem padding="horizontal-Large">
             <h1 class="slds-page-header__title slds-m-right--Large slds-align-middle slds-truncate" title="Create Opportunity"><b>Create Opportunity</b></h1>
             <h2 class="slds-text-heading-medium">New Opportunity</h2>
        </lightning:layoutItem>
        </lightning:layout>
        </table>
        </div>
        <form class="slds-form--inline"> <br/><br/>
            <center> <lightning:button variant="brand" 
                                       disabled="{!v.dateValidationError}" 
                                       label="Save" onclick="{!c.Save}"/>
                     <lightning:button label="cancel" 
                                       iconName="cancel"
                                       iconPosition="centera"
                                       variant="brand" onclick="{!c.cancel}"/></center><br/><br/>
          <body>
          <p class="slds-text-title--caps  slds-line-height--reset" style="color:Blue"><b>Opportunity Information</b></p><br/><br/>
           <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout"><br/>
          
          <div class="slds-p-around_medium">
          <!-- <label class="slds-form-element__label" >Opportunity Name</label>-->
          <div class="slds-form-element__control">
              <ui:inputText value="{!v.opp.Name}" label="Opportunity Name"/>
          </div>
          </div>    
           
          <div class="slds-p-around_medium">
          <label class="slds-form-element__label" >Stage</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.Stage__c}" />
          </div>
          </div>
        <div class="slds-p-around_medium">
          <br/> <label class="slds-form-element__label" for="inputNameSample1">Account:</label>
          <div class="slds-form-element__control" style="width:30%;height:20px;">
          <force:inputField value="{!v.opp.Account__c}" />
          </div>
          </div><br/>
       <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">Project Type:</label>
          <div class="slds-form-element__control" >
          <force:inputField value="{!v.opp.Project_Type__c}" />
          </div>
          </div><br/>
          <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">Agreements:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.Agreements__c}" />
          </div>
          </div><br/>
        <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">End Customer:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.End_Customer__c}" />
          </div>
          </div><br/><br/>
        <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">End Market:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.End_Market__c}" />
          </div>
          </div><br/>
          <div class="slds-p-around_medium"> 
          <br/><label class="slds-form-element__label"   for="inputNameSample1">Sample Required</label>
          <div class="slds-form-element__control" >
          <force:inputField value="{!v.opp.Sample_Required__c}" />
          </div>
          </div><br/><br/>
          </table><br/><br/>
          
          <p class="slds-text-title--caps  slds-line-height--reset" style="color:Blue"><b>Business Potential Information</b></p><br/><br/>
          <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout"><br/>
          <div class="slds-p-around_medium">
          <label class="slds-form-element__label" for="inputNameSample1">Product Name:</label>
          <div class="slds-form-element__control" style="width:30%;">
          <force:inputField value="{!v.opp.ProductName__c}" />
          </div>
          </div><br/><br/>
          <div class="slds-p-around_medium">
          <label class="slds-form-element__label" for="inputNameSample1">Product Specification:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.Product_Specifications__c}" />
          </div>
          </div><br/>
        
          <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">Potential Quantity:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.Potential_Quantity__c}" />
          </div>
          </div><br/>
        <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">Potential Price:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.Potential_Price__c}" />
          </div>
          </div><br/><br/>
        <div class="slds-p-around_medium">
          <div class="slds-form-element__control">         
         <lightning:input class="{! v.dateValidationError ? 'slds-has-error' : ''}"
                         type="date"
                         label="Expected Launch:"
                         value="{!v.opp.Expected_Launch__c}"
                         name="date"
                         variant="brand"
                         onchange="{!c.dateUpdate}"/>
              </div>
              </div><br/>
             <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label"  for="inputNameSample1">UOM:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.UOM__c}" />
          </div>
          </div><br/>
          
        
          <div class="slds-p-around_medium">
          <div class="slds-form-element__control" >
           
                  <lightning:input class="{! v.dateValidationError ? 'slds-has-error' : ''}"
                         type="date"
                         label="Sample Required Date:"
                         value="{!v.opp.Sample_Required_Date__c}"
                         name="date"
                         variant="brand"
                         onchange="{!c.dateUpdate}" />
               </div>
          </div><br/>
         <div class="slds-p-around_medium">
          <br/><label class="slds-form-element__label" for="inputNameSample1">Documents Required:</label>
          <div class="slds-form-element__control">
          <force:inputField value="{!v.opp.Documents_Required__c}" />
          </div>
          </div><br/><br/>
              <aura:if isTrue="{!v.dateValidationError}">
             <div class="slds-text-color_error slds-p-left_x-small">
                Date must be in present or in future..
              </div>
           </aura:if>  
          </table><br/><br/>
             <center> <lightning:button label="save" 
                                       iconName="submit"
                                       iconPosition="centera"     
                                       variant="brand" 
                                       disabled="{!v.dateValidationError}" onclick="{!c.Save}"/>
                     <lightning:button label="cancel" 
                                       iconName="cancel"
                                       iconPosition="centera"
                                       variant="brand" onclick="{!c.cancel}"/></center><br/><br/>
        </body>
        </form>
        </div><br/>
   </aura:component>
================
controller:
({
    doInit : function(component, event, helper) {
        helper.fetchOppRecords(component, event, helper);
    },
    mapOpp : function(component, event, helper) {
        
       var Leadid = component.get("v.recordId");
       var le = component.get("v.lea");
        var action= component.get("c.getMapLeadFields")
        action.setParams({"op":"v.opp", "leid":"v.recordId"}); 
               action.setCallback(this, function(response){               
           var state=response.getState();
            if(state=="SUCCESS"){
                var opps = response.getReturnValue(); 
                 var op = component.get("v.op"); 
                 var lea=component.get("v.lea.Account__C");
                component.set("v.op.Account__c",lea);    
               component.set("v.opp",opps);
                alert('Fields automapped');
                console.log("opps"+opps);
            }
                else{
                alert("Failed to fetch records from server");
            }
        });
        $A.enqueueAction(action);
    },      dateUpdate : function(component, event, helper) {
        var today = new Date();        
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is 0!
        var yyyy = today.getFullYear();
     // if date is less then 10, then append 0 before date   
        if(dd < 10){
            dd = '0' + dd;
        } 
    // if month is less then 10, then append 0 before date    
        if(mm < 10){
            mm = '0' + mm;
        }
     var todayFormattedDate = yyyy+'-'+mm+'-'+dd;
if(component.get("v.opp.Expected_Launch__c") != '' && component.get("v.opp.Expected_Launch__c") < todayFormattedDate){
            component.set("v.dateValidationError" , true);
            
        }else if(component.get("v.opp.Sample_Required_Date__c") != '' && component.get("v.opp.Sample_Required_Date__c") < todayFormattedDate){
            component.set("v.dateValidationError" , true); 
        }
          
        else{
            component.set("v.dateValidationError" , false);
        }
        },
    cancel : function(component, event, helper) {
        // Close the action panel
        var dismissActionPanel = $A.get("e.force:closeQuickAction");
        dismissActionPanel.fire();
    },
    
    Save : function(component, event, helper) {
    // Validate form fields
    // Pass form data to a helper function
        
            var opsrecs = component.get("v.op");            
            helper.saveOpp (component,opsrecs);             
}

})
===========================
Helper:
({
    fetchOppRecords : function(component, event, helper) {
       var action= component.get("c.getFetchOpp")
               action.setCallback(this, function(response){           
           var state=response.getState();
            if(state==="SUCCESS"){
                var Opplist = response.getReturnValue();
                component.set("v.opp", Opplist);
                console.log("opplist"+Opplist);
            }else{
                alert("Failed to fetch records from server");
            }
        });
        $A.enqueueAction(action);
    },
    saveOpp: function(component, ops) {
    //Save the expense and update the view
    this.upsertOpp(component, ops, function(a) {
         var urlEvent = $A.get("e.force:navigateToURL");         
       var state = a.getState();
            if (component.isValid() && state === "SUCCESS") {
                var Opp = component.get("v.opp");
                Opp.push(a.getReturnValue());
                component.set("v.opp", Opp);
                alert('Lead Converted Successfully');
            }
        urlEvent.setParams({
      "url": "/one/one.app#/sObject/Opportunity__c/list?filterName=00B7F00000AyAh9UAF"
    });
    urlEvent.fire();
    });
},
upsertOpp : function(component, ops, callback) {
  var action = component.get("c.getSaveOppRecs");
  action.setParams({ 
      "ops": ops
  });
  if (callback) {
      action.setCallback(this, callback);
  }
  $A.enqueueAction(action);
}
})
=============================
apxc:
public class createOpp {
    @AuraEnabled
    public Static List<Opportunity__c> getFetchOpp(){
       Return [Select id,Name,Account__c,Project_Type__c,Agreements__c,End_Customer__c,
               Sample_Required__c,ProductName__c,Product_Specifications__c,Potential_Quantity__c,Potential_Price__c,
               Expected_Launch__c,UOM__c,Sample_Required_Date__c,Documents_Required__c from Opportunity__c ]; 
    }
   @AuraEnabled
    public Static List<Lead__c> getMapLeadFields(List<Lead__c> le){  
         system.debug('hello');
        
        Return [Select id,Name,Agent__c,Account__c,Agreements__c,AgentCommission__c,
        DeliveryTerms__c,Description__c,EndCustomer__c,EndMarket__c,ExpectedLaunch__c,
        PotentialPrice__c,PotentialQuantity__c,Product_Name__c,Project_Type__c,Product_Specifications__c,
        UOM__c from Lead__c]; 
       
    }  
    @AuraEnabled
      public static Opportunity__c getSaveOppRecs(Opportunity__c ops){
                
        insert ops;
        return ops;
}
}
===========================================
can anybody help me out with autopopulating the fields..
Thanks & Regards,
Veena.
Shivdeep KumarShivdeep Kumar
Hi Veena,

You are using the atrribute name (recordId and opp) and passed these attribute in JS controller (mapOpp), which is null.
In apex class, getting the lead without any passing any Id in where clause.
Please let me tell you about my understanding :-
You have a quick action button on lead detail page, after clicking on button your component will open with auto populating the some field value of opportunity which is related to current lead record value.

Thanks
Shivdeep