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
Himanshu Chhabra 9Himanshu Chhabra 9 

how to insert a Task record using lightning components.

Hi I am new to Salesforce Lightning. I am trying to insert a Task Records uisng Lightning Components But I am getting an error.

Below is my Code:
1.Apex Controller

public class SaveTaskRecords {

 @AuraEnabled
 public static Task save(Task ta) {
  insert ta;
  return ta;

 }
 @AuraEnabled
 public static Task getTask() {
  return [select Id, whoId, ActivityDate from Task Limit 1];
 }

 public static List < String > getTaskStatus() {
  List < String > options = new List < String > ();
  Schema.DescribeFieldResult fieldResult = Task.status.getDescribe();
  List < Schema.PicklistEntry > ple = fieldResult.getPicklistValues();
  for (Schema.PicklistEntry f: ple) {
   options.add(f.getLabel());
  }

  return options;
 }
}

2.Client Side Controller:

({
 saveTask: function(component, event, helper) {
  var newTask = component.get("v.newTask");
  var action = component.get("c.save");
  action.setParams({
   "ta": newTask

  });
  action.setCallback(this, function(a) {
   var state = a.getState();

   if (state === "SUCCESS") {
    var name = a.getReturnValue();
    alert("success");
   } else {
    console.log("Testt" + a.getReturnValue());
    alert("Failed");
    console.log("Testt" + a.getReturnValue());
   }
  });
  $A.enqueueAction(action)
 },

 doInit: function(component, event, helper) {
  var action = component.get("c.getTaskStatus");
  var inputsel = component.find("InputSelectDynamic");
  var opts = [];
  action.setCallback(this, function(a) {
   for (var i = 0; i < a.getReturnValue().length; i++) {
    opts.push({
     "class": "optionClass",
     label: a.getReturnValue()[i],
     value: a.getReturnValue()[i]
    });
   }
   inputsel.set("v.options", opts);

  });
  $A.enqueueAction(action);
 }
})

3. Lightning Component:

<aura:component  controller="SaveTaskRecords">
    <aura:attribute name="newTask" type="Task"  default="{ 'sobjectType': 'Task',
                     'OwnerId': '','Status':'','Subject':'',}"/>
    <form>
        <force:inputField  value="{!v.newTask.OwnerId}" />
        <force:inputField aura:id="contactField" value="{!v.newTask.Status}" />
        <force:inputField aura:id="contactField" value="{!v.newTask.Priority}" />
        <force:inputField aura:id="contactField"  value="{!v.newTask.WhoId}" />
        <ui:inputDate aura:id="contactField" label="Today's Date" class="slds-input" value="{!v.newTask.ActivityDate}" displayDatePicker="true" />
        <lightning:button aura:id="contactField" label="Save Task" onclick="{!c.saveTask}"/>
    </form>
</aura:component>

Can any one please help?
Naveen KNNaveen KN
Hi Himanshu, 

what is the error you are getting? Meanwhile, In the apex save method try to instantiate new object for Task record, set the field values that you are getting from the client side and insert it. See if this works. 

I recommend using console.log at client side js file and system.debug at apex code to see the output of variables and find the exact line where you are getting an error. 

Share the results.

Naveen
Team codengine.in