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"/> -->
<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.