0

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.

Component

<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.setParams({
        "recordId":recordId
    });
    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);
                helper.recordCreation(component,storeResponse.RecordTypeId);

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

            var getRecorTypeId = component.get("v.newTask.RecordTypeId");
                console.log('getRecorTypeId'+getRecorTypeId);

                helper.recordCreation(component);
            var dismissActionPanel = $A.get("e.force:closeQuickAction");
            dismissActionPanel.fire();

            }    
            else {


                $A.get("e.force:closeQuickAction").fire();
                $A.get("e.force:refreshView").fire();
            }
        }
    });
    $A.enqueueAction(action);

}
})

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");
    console.log('evt%%%%'+evt);
    evt.setParams({
        'entityApiName':'Task',
        'defaultFieldValues': {
            'Status':'Not Started',
            'WhatId':component.get("v.recordId"),
             'OwnerId':component.get("v.recordId").OwnerId,
            'Priority':'Normal',
            'Type':'TEST'
        },

        'recordTypeId':recTypeId
    });
    evt.fire();

}
})

Apex Class

public class Taskcreation{

@AuraEnabled
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'];
    if(!lstRecType.isEmpty()){
        for(RecordType rec: lstRecType){
            mapRecType.put(rec.DeveloperName,rec.Id);    

        }
    }


        if(sObjectparentId.startsWith(oppr_prefix)){
            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;
}

@AuraEnabled
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 at 15:58
  • Yes. I need to populate the field values returning form apex. – chris Jan 10 at 7:27
  • Please note, I have changed the title of the question to reflect your ask here. – Jayant Das Jan 10 at 14:15
1

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) {
    ...
    ...
    evt.setParams({
        'entityApiName':'Task',
        'defaultFieldValues': { // make sure the field names start with Uppercase
            'Status':task.Status,
            '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.