I Need to create a component to create a Task record in the custom object based on conditions. The condition is if the related record(contact record's) record type is New then only The button should work (should pop up with some fields of task), Otherwise, it should show a pop-up message with some message.

When I click on the button in lightning standard pop up is coming and whatever value I gave in Helper with those predefined values it is populating but I need to pass field values from Apex class.


<aura:component controller="Taskcreation" implements="lightning:actionOverride,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasSObjectName" access="global">

<aura:attribute name="recordId" type="Id" />
<aura:attribute name="newTask" type="Task" default="{'sobjectType':'Task'}"/>
<aura:attribute name="tasks" type="Task[]"/>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>  

Component Controller

  doInit : function(component,event,helper){
    var recordId = component.get("v.recordId");
    console.log('parent id--123'+recordId);

    var userId = $A.get("$SObjectType.CurrentUser.Id");
    console.log('user id:####:'+userId);    
    var action = component.get("c.populateTask");      
    action.setCallback(this, function(response) {
        if (response.getState() ===  "SUCCESS") {  
            if(response.getReturnValue() != null ) {
                console.log('return %%%%%'+response.getReturnValue());
                 var storeResponse = response.getReturnValue();
                console.log('storeResponse recordtype'+storeResponse.RecordTypeId);

            component.set("v.newTask", storeResponse);

            var getRecorTypeId = component.get("v.newTask.RecordTypeId");

            var dismissActionPanel = $A.get("e.force:closeQuickAction");

            else {



Component Helper

recordCreation : function(component,recTypeId) {
    var userId = $A.get("$SObjectType.CurrentUser.Id");
    console.log('user id:####:'+userId);
    var windowHash = window.location.hash;

    var evt = $A.get("e.force:createRecord");
        'defaultFieldValues': {
            'Status':'Not Started',



Apex Class

public class Taskcreation{

public static Task taskcreate(Id recordId){

    Task newTask = new Task();
    List<RecordType> lstRecType = new List<RecordType>();
    Map<String,Id> mapRecType = new Map<String,Id>();
    Opportunity opprRec = new Opportunity();
     Account accRec = new Account();
     Account accRec1 = new Account();
     Lead leadRec = new Lead();
    String sObjectparentId = '';
    String cRecType = '';

    String taskRecType = '';

    String acc_prefix = Schema.SObjectType.Account.getKeyPrefix();

    sObjectparentId = recordId;

    lstRecType = [SELECT Id,DeveloperName FROM RecordType WHERE sobjecttype = 'Task'];
        for(RecordType rec: lstRecType){


            custobje = [SELECT RecordType.Name,Type,RecordTypeId,Contact,StageName,OwnerId FROM customobject_c
                       WHERE Id=: recordId LIMIT 1 ]; 

            if(recordId != null){
                cont = [select id,Type,RecordType.Name from contact where Id =: opprRec.contact LIMIT 1];

                if(cont.RecordType.Name == 'New'){
             taskRecType = 'Renewal_Task_Type';
             newTask.Subject = 'testkk'; 
              newTask.whatId = con.Id;
              newTask.Status = 'Not Started'; 
              newTask.Priority = 'Normal';
              newTask.OwnerId = cont.OwnerId; 

    return newTask;

public static Task saveTask(Task task){
    upsert task;
    return task;

  • 1
    Are you saying that you want to populate the defaultFieldValues while creating a new Task from the values fetched from your Apex? And that it is not working currently?
    – Jayant Das
    Jan 9 '19 at 15:58
  • Yes. I need to populate the field values returning form apex.
    – chris
    Jan 10 '19 at 7:27
  • Please note, I have changed the title of the question to reflect your ask here.
    – Jayant Das
    Jan 10 '19 at 14:15

whatever value I gave in Helper with those predefined values it is populating but I need to pass field values from Apex class.

You have almost achieved part of it. You are already getting the new Task values returned from Apex, you just need to set those in your JS function against the defaultFieldValues. In your current form, your recordCreation : function(component,recTypeId) function accepts just a record type id. Instead of that, change it to as below:

// modify this function to accept the Task object returned from Apex
recordCreation : function(component, task) {
        'defaultFieldValues': { // make sure the field names start with Uppercase
            'WhatId' : task.WhatId,
            'Priority':task.Priority // set any other fields

And then you call this function by passing the response that you are getting from the apex method public static Task taskcreate(Id recordId) as:

var storeResponse = response.getReturnValue(); // returns the Task instance that you created
helper.recordCreation(component, storeResponse); // pass the complete response, i.e., the Task instance that you received from Apex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.