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
Vineet GoelVineet Goel 

Lightning component Force:Createrecord case is not prepopulating field values

Component:
<aura:component controller="RemovefromDNCcontroller"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader" access="global">
    <ltng:require styles="{!$Resource.quickActionStyle}"/>
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="Preval" type="RemovefromDNCcontroller" />
    <aura:attribute name="Preaccount" type="Id"/>
    <aura:attribute name="Precontact" type="Id"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:if isTrue = "{!v.Preval.messageDisplay}">
        <div class="slds-notify slds-notify_alert slds-theme_alert-texture slds-theme_info" role="alert">
            {!v.Preval.message}
        </div>
    </aura:if>
</aura:component>

Controller.js

({
    doInit : function(component, event,helper) {
        var action = component.get("c.Oppdetails");
        action.setParams({"OppId": component.get("v.recordId")});
        action.setCallback(this, function(response) {
            var responsedata = response.getReturnValue();
            var accid = response.getReturnValue().caseaccId;
            var conid = response.getReturnValue().caseContactId;
            var state = response.getState();
            console.log('accid==='+accid);
            console.log('conid==='+conid);
            helper.fetchAccdetail(component,event,helper,accid);
            helper.fetchCondetails(component, event, helper,conid);
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.Preval", response.getReturnValue());
                helper.createCasDetails(component,event,helper,responsedata);
            }
        });
        $A.enqueueAction(action);
    },
})

Helper.js
({
    fetchAccdetail : function(component, event, helper,accid) {
        console.log('accid helper==='+accid);
        var action = component.get("c.accountPreCheck");
        action.setParams({
            "AccountcheckId" : accid
        });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            console.log('response acc===='+response.getReturnValue());            
            if (state === "SUCCESS") {
                component.set("v.Preaccount", response.getReturnValue());
                //var Acc = component.get("v.Preaccount");
                //console.log("Acc---"+Acc);
            }
        });
        $A.enqueueAction(action);
    },
    fetchCondetails : function(component, event, helper,conid) {
        console.log('conid helper==='+conid);
        var action = component.get("c.contactPreCheck");
        action.setParams({
            "ContactcheckId" : conid
        });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            console.log('response con==='+response.getReturnValue());            
            if (state === "SUCCESS") {
                component.set("v.Precontact", response.getReturnValue());
                //var con = component.get("v.Precontact");
                //console.log("con---"+con);
            }
        });
        $A.enqueueAction(action);
    },
    createCasDetails: function(component,event,helper,responsedata){
        console.log("responsedata---"+responsedata.messageDisplay);
        if(responsedata.messageDisplay == false){
            var Acc = component.get("v.Preaccount");
            var con = component.get("v.Precontact");
            console.log("Acccase---"+Acc);
            console.log("concase---"+con);
            var createCaseEvent = $A.get("e.force:createRecord");
            createCaseEvent.setParams({
                "entityApiName": "Case",
                "defaultFieldValues": {
                    'Case_Type__c'     : 'Do Not Call List Request',
                    'Account'        : Acc,
                    'Contact'        : con,
                    'Opportunity__c': responsedata.caseOppId
                }
            });
            createCaseEvent.fire();
        }
    },
})

Apex Class:

public with sharing class RemovefromDNCcontroller {

    @AuraEnabled public string caseaccId{get;set;}
    @AuraEnabled public string caseContactId{get;set;}
    @AuraEnabled public string caseOppId{get;set;}
    @AuraEnabled public string message{get;set;}
    @AuraEnabled public boolean messageDisplay{get;set;}
    
    @AuraEnabled 
    public static RemovefromDNCcontroller Oppdetails (Id OppId){
        RemovefromDNCcontroller rfd = new RemovefromDNCcontroller();
         rfd.messageDisplay = false;
        Opportunity o = [select id, Name,ownerId, AccountId, Account.PersonContactId, Do_Not_Call__c from Opportunity where Id =: OppId];
        system.debug('o----'+o);
        if(o.Do_Not_Call__c == true){
            system.debug('rfd 24'+ rfd.message);
            rfd.caseOppId = o.Id;
            rfd.caseContactId = o.Account.PersonContactId;
            rfd.caseaccId = o.AccountId;
            system.debug('caseContactId ==='+rfd.caseContactId);
            system.debug('caseaccId ==='+rfd.caseaccId);
            return rfd;
        }else{
            rfd.message = system.Label.Remove_from_DNC_Alert;
            rfd.messageDisplay = true;
            system.debug('rfd 32'+ rfd.message);
            return rfd;
            
        }
        
    }
    @AuraEnabled 
    public static Id accountPreCheck (Id AccountcheckId){
        system.debug('AccountcheckId--'+AccountcheckId);
        return AccountcheckId;
    }
    @AuraEnabled 
    public static Id contactPreCheck (Id ContactcheckId){
        system.debug('ContactcheckId--'+ContactcheckId);
        return ContactcheckId;
    }
}

In Helper, method createCasDetails, values in 
console.log("Acccase---"+Acc);
console.log("concase---"+con);
are not coming due to which values in Account and Contact are not pre-populating.
Please help.
Vikash GoyalVikash Goyal
Hi Vineet,

1. In your controller.js, i don't know what you are trying to do with following 2 lines :
helper.fetchAccdetail(component,event,helper,accid);
helper.fetchCondetails(component, event, helper,conid);
    When you have already accid and conid then why you are sending them to apex controller and setting in an aura attribute, while you can directly pass 'accid' and 'conid' in 'createCasDetails' method.

2. Actual Problem Solution :
In lightning, when you call an apex method it is queued for processing i.e. runs in a separate thread i.e. asynchronous call.

So in your case when you call an apex method through "helper.fetchAccdetail(component,event,helper,accid); " it creates a separate thread for that and goes to next line of code which is "helper.fetchCondetails(component, event, helper,conid);"  here it again creates a separate thread and goes to next line without waiting for callback of apex methods where you are calling "helper.createCasDetails(component,event,helper,responsedata);"

Due to asynchronous calls, "v.Preaccount" and "v.Precontact" are not set when you in "createCasDetails" method and you got null values for account and contact.

So to avoid this kind of situation either make calls synchronously or merge multiple apex calls in single apex method.

To make synchronous calls, call 2nd method in callback method of method 1 i.e. from controller call  "helper.fetchAccdetail(component,event,helper,accid); " then in callback of 'fetchAccdetail' call 'fetchCondetails' and then in callback of 'fetchCondetails' call 'createCasDetails' :
e.g : 

   fetchAccdetail : function(component, event, helper,accid, conid) {
        var action = component.get("c.accountPreCheck");
        action.setParams({
            "AccountcheckId" : accid
        });
        
        action.setCallback(this, function(response){
            var state = response.getState();         
            if (state === "SUCCESS") {
                component.set("v.Preaccount", response.getReturnValue());
                this.fetchCondetails(component, event, helper,conid);
            }
        });
        $A.enqueueAction(action);
    },

Hope this help you to understand the concept.

Thanks
Vineet GoelVineet Goel
Hi Vikash

According to the first point, I have got the values of accid and conid through responsedata parameter, as per code below:

Cmp
===================
<aura:component controller="RemovefromDNCcontroller"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader" access="global">
    <ltng:require styles="{!$Resource.quickActionStyle}"/>
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="Preval" type="RemovefromDNCcontroller" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="Preaccount" type="Object"/>
    <aura:attribute name="Precontact" type="Object"/>
    
    
    <aura:if isTrue = "{!v.Preval.messageDisplay}">
        <div class="slds-notify slds-notify_alert slds-theme_alert-texture slds-theme_info" role="alert">
            {!v.Preval.message}
        </div>
    </aura:if>
</aura:component>

Controller.js
===================
({
    doInit : function(component, event,helper) {
        var action = component.get("c.Oppdetails");
        action.setParams({"OppId": component.get("v.recordId")});
        action.setCallback(this, function(response) {
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.Preval", response.getReturnValue());
                var responsedata = response.getReturnValue();
                var state = response.getState();
                helper.createCasDetails(component,event,helper,responsedata);
            }
        });
        $A.enqueueAction(action);
    },
})

Helper.js
===================
({
    createCasDetails: function(component,event,helper,responsedata){
        console.log("responsedata---"+responsedata.messageDisplay);
        if(responsedata.messageDisplay == false){
            component.set("v.Preaccount",responsedata.caseaccId);
            component.set("v.Precontact",responsedata.caseContactId);
            var Acc = component.get("v.Preaccount");
            var con = component.get("v.Precontact");
            console.log("Acccase---"+Acc);
            console.log("concase---"+con);
            var createCaseEvent = $A.get("e.force:createRecord");
            createCaseEvent.setParams({
                "entityApiName": "Case",
                "defaultFieldValues": {
                    'Case_Type__c'     : 'Do Not Call List Request',
                    'Account'        : Acc,
                    'Contact'        : con,
                    'Opportunity__c': responsedata.caseOppId
                }
            });
            createCaseEvent.fire();
        }
    },
})

Apex Class
================
public with sharing class RemovefromDNCcontroller {

    @AuraEnabled public string caseaccId{get;set;}
    @AuraEnabled public string caseContactId{get;set;}
    @AuraEnabled public string caseOppId{get;set;}
    @AuraEnabled public string message{get;set;}
    @AuraEnabled public boolean messageDisplay{get;set;}
    
    @AuraEnabled 
    public static RemovefromDNCcontroller Oppdetails (Id OppId){
        RemovefromDNCcontroller rfd = new RemovefromDNCcontroller();
         rfd.messageDisplay = false;
        Opportunity o = [select id, Name,ownerId, AccountId, Account.PersonContactId, Do_Not_Call__c from Opportunity where Id =: OppId];
        system.debug('o----'+o);
        if(o.Do_Not_Call__c == true){
            system.debug('rfd 24'+ rfd.message);
            rfd.caseOppId = o.Id;
            rfd.caseContactId = o.Account.PersonContactId;
            rfd.caseaccId = o.AccountId;
            system.debug('caseContactId ==='+rfd.caseContactId);
            system.debug('caseaccId ==='+rfd.caseaccId);
            system.debug('caseOppId ==='+rfd.caseOppId);
            return rfd;
        }else{
            rfd.message = system.Label.Remove_from_DNC_Alert;
            rfd.messageDisplay = true;
            system.debug('rfd 32'+ rfd.message);
            return rfd;
            
        }  
    }
}

But now, component is not responding on quick action, shows blank page without error