0

I created a lightning component for creating a new record using a form. The lightning component is used in 3 different objects (Account, Opportunity and custom object).

In this form, I have this pre populate functionality which populates some of the fields in the form with account's values.

The problem is that I have 3 fields which sometimes they populated and sometimes they don't and the funny thing is that in the console I can see that I have the values - they just don't populate.

The function:

prePopulateFields : function(component){

    var recordId = component.get("v.accountRecordId");
    var action = component.get("c.prePopulate");
    action.setParams({
        accId : recordId,
        recordTypeName : "New"
    });
    action.setCallback(this, function(response){
        var state = response.getState();
        if(state === "SUCCESS"){

            var res = response.getReturnValue();

            if (res){
                var populateFields = JSON.parse(res);
                console.log("@@@populateFields"+JSON.stringify(populateFields));

                Object.keys(populateFields).forEach(function(key) {
                    var field = component.find(key);

                    if (field){
                        console.log("yayy " + key + " " + populateFields[key]);
                        field.set("v.value", populateFields[key]);
                    }
                });
            }


        } else if(state === "ERROR"){
            console.log('Error: ' + JSON.stringify(response.error));
        } else {
            console.log('Unknown problem, state: '+ state + ', error: ' + JSON.stringify(response.error));
        }
    });

    $A.enqueueAction(action);
}

Component:

<aura:component implements="flexipage:availableForAllPageTypes, force:hasRecordId, force:hasSObjectName" access="global" controller="Ctrl_NewCreditCheck">

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="recordTypeId" type="String" />
<aura:attribute name="isMessage" type="boolean" default="false"/>
<aura:attribute name="message" type="String" />
<aura:attribute name="messageType" description="Message type, can be error/warning/info/success" type="String" default="info"/>
<aura:attribute name="accountRecordId" type="String" />
<aura:attribute name="accountRecordFields" type="Account" />
<aura:attribute name="accountRecord" type="Account" />
<aura:attribute name="isValidationError" type="boolean" />
<aura:attribute name="labelsByAPIName" type="map" default="{}"/>
<aura:attribute name="showSpinner" type="boolean" default="false"/>
<aura:attribute name="currentRecordFields" type="sObject" />
<aura:attribute name="currentRecord" type="sObject" />

<!-- account record -->
<force:recordData aura:id="accountRecordData" layoutType="FULL"
              targetRecord="{!v.accountRecord}"
              recordId="{!v.accountRecordId}"
              targetFields="{!v.accountRecordFields}" 
              recordUpdated="{!c.accountReloaded}"/>

<!-- current record -->
<force:recordData layoutType="FULL"
              targetRecord="{!v.currentRecord}"
              recordId="{!v.recordId}"
              targetFields="{!v.currentRecordFields}" 
              recordUpdated="{!c.initAccount}"/>

<article aria-describedby="rlql-headerLabel" class="slds-card slds-card_boundary">
    <lightning:recordEditForm aura:id="newCreditCheckForm" objectApiName="Credit_Check__c" recordTypeId="{!v.recordTypeId}" onload="{!c.loadForm}" onsuccess="{!c.createdSuccess}" onerror="{!c.getError}" onsubmit="{!c.submitForm}">
        <div id="rlql-header" class="slds-card__header slds-grid">
            <header class="slds-media slds-media_center slds-has-flexi-truncate">
                <div class="slds-media__body">
                    <h2 id="rlql-headerLabel" class="slds-text-heading--small">New Credit Check</h2>
                </div>
                <aura:if isTrue="{!!v.isMessage}">
                    <div class="slds-no-flex">
                        <lightning:button variant="brand" 
                                            type="submit"
                                            label="Create"
                                            title="Submit"
                                            />
                    </div>
                </aura:if>
            </header>
        </div>
        <!-- <lightning:messages /> -->
        <aura:if isTrue="{!or(v.isMessage, v.isValidationError)}">
            <div class="{!'slds-notify slds-notify--alert slds-banner slds-theme--alert-texture slds-theme--'+v.messageType}" role="alert">
                <!-- <lightning:icon iconName="{! 'utility:'+ v.messageType }" size="x-small" alternativeText="{!v.messageType}" variant="inverse" class="iconContainer"/>&nbsp; -->
                <span class="slds-text-body-regular">
                    <h2>
                        <aura:unescapedHtml value="{!v.message}" />
                    </h2>
                </span>

            </div>
        </aura:if>

        <div class="slds-card__body_inner">
            <aura:if isTrue="{!!v.isMessage}">

                <lightning:accordion>
                    <lightning:accordionSection name="A" label="Credit Details">
                        <lightning:inputField fieldName="Account__c" aura:id="Account__c"/> 
                        <lightning:inputField fieldName="Credit_Type__c" class="customRequired none" aura:id="Credit_Type__c"/> 
                        <lightning:inputField fieldName="Expected_Daily_Spend__c" class="customRequired none" aura:id="Expected_Daily_Spend__c" onchange="{!c.calculateRecommendedCreditAmount}"/> 
                        <lightning:inputField fieldName="Recommended_Credit__c" aura:id="Recommended_Credit__c"/> 
                        <lightning:inputField fieldName="Requested_Credit_Amount_New__c" aura:id="Requested_Credit_Amount_New__c"/>
                        <lightning:inputField fieldName="Requested_Credit_Amount__c" aura:id="Requested_Credit_Amount__c" class="slds-hide"/>
                        <lightning:inputField fieldName="Minimum_Prepayment_Required__c" aura:id="Minimum_Prepayment_Required__c"/> 
                        <lightning:inputField fieldName="Prepayment_Amount__c" aura:id="Prepayment_Amount__c"/> 
                        <lightning:inputField fieldName="Reason_for_Dismissing_Prepayment__c" aura:id="Reason_for_Dismissing_Prepayment__c"/> 
                        <lightning:inputField fieldName="Office__c" aura:id="Office__c"/> 
                    </lightning:accordionSection>

                    <lightning:accordionSection name="B" label="Account Details">
                        <lightning:inputField fieldName="Billing_Country__c" class="customRequired none" aura:id="Billing_Country__c"/> 
                        <lightning:inputField fieldName="Billing_City__c" class="customRequired none" aura:id="Billing_City__c"/> 
                        <lightning:inputField fieldName="Billing_Street__c" class="customRequired none" aura:id="Billing_Street__c"/> 
                        <lightning:inputField fieldName="Billing_Zip_Code__c" class="customRequired none" aura:id="Billing_Zip_Code__c"/> 
                        <lightning:inputField fieldName="Legal_Entity_Name__c" class="customRequired none" aura:id="Legal_Entity_Name__c"/> 
                        <lightning:inputField fieldName="Phone__c" aura:id="Phone__c"/> 
                    </lightning:accordionSection>

                    <lightning:accordionSection name="C" label="Background">
                        <lightning:inputField fieldName="Public_Company__c" class="customRequired none" aura:id="Public_Company__c"/> 
                        <lightning:inputField fieldName="Company_Size__c" class="customRequired none" aura:id="Company_Size__c"/> 
                        <lightning:inputField fieldName="Subsidiary_or_Related_to_a_Large_Company__c" class="customRequired none" aura:id="Subsidiary_or_Related_to_a_Large_Company__c"/> 
                        <lightning:inputField fieldName="Mother_or_Related_Company__c" aura:id="Mother_or_Related_Company__c"/> 
                        <lightning:inputField fieldName="Working_Worked_with_IS__c" class="customRequired none" aura:id="Working_Worked_with_IS__c"/> 
                        <lightning:inputField fieldName="Working_Worked_with_IS_Details__c" aura:id="Working_Worked_with_IS_Details__c"/> 
                        <lightning:inputField fieldName="D_B_Number__c" aura:id="D_B_Number__c"/> 
                        <lightning:inputField fieldName="Nature_of_Relationship_With_Advertiser__c" class="customRequired none" aura:id="Nature_of_Relationship_With_Advertiser__c"/> 
                    </lightning:accordionSection>




                </lightning:accordion>


            </aura:if>
        </div>
    </lightning:recordEditForm>
</article>

You can see that I print to the console the fields and I see them there but they aren't populated in the fields ...

Any idea ?

UPDATE: The 3 fields are "Credit_Type__c", "Requested_Credit_Amount__c", "Billing_Country__c" which are picklist fields.

5
  • Mind sharing the relevant component markup as well?
    – glls
    Commented Sep 5, 2018 at 12:13
  • I added the component, also I updated the original post which the 3 fields that aren't populated are 3 picklist fields. Maybe that matters
    – osos9
    Commented Sep 5, 2018 at 12:56
  • Do you see the values in the console 100% of the time, or is it intermittent?
    – David Reed
    Commented Sep 5, 2018 at 13:06
  • 100% of the times I see the values in the console. Other fields are getting populated, and those 3 specific fields sometimes they are and sometimes not. Maybe it's because they are picklist type? I don't know
    – osos9
    Commented Sep 5, 2018 at 13:12
  • It does look like the issue is with picklist fields. Looking around could see few of such issues. Take a look at this answer and see if this helps.
    – Jayant Das
    Commented Sep 5, 2018 at 13:39

1 Answer 1

-2

you should consider using Lightning Data Service and use Force:RecordData or Force:RecordView to populate fields from Objects.

Please follow this link from Salesforce Documentation. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_load_record.htm

1
  • I'm using Force:RecordData. And also as I said, I get the values but they are not populated (and sometimes they are).
    – osos9
    Commented Sep 5, 2018 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .