0

I'm trying to learn lightning and in this process I'm developing a component akin to a small to-do list with subject and activity date (idea taken from paul battison's blog) which I've put in account record page using app builder. However i'm getting some error which i'm not able to understand. Can someone help as to where i'm going wrong?

[P.S - Updated code, this is working now]

Component:

<aura:component controller="TaskController" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" >

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

<ui:inputText aura:id="taskSubject" label="Task Subject" value="{!v.newTask.Subject}"/>
<ui:inputDateTime aura:id="taskDate" label="Activity Date" value="{!v.newTask.ActivityDate}" displayDatePicker="true"/>
<ui:button label="Add Task" press="{!c.createNewTask}"/>

<aura:iteration items="{!v.tasks}" var="item">
    {!item.Subject}, {!item.ActivityDate}<br/>
</aura:iteration>

</aura:component>

JS controller:

    ({
    doInit: function(component, event, helper) {
    var action = component.get("c.loadTasks");
    var whatId = component.get("v.recordId");
    action.setParams({
        "recordId":whatId
    });
    action.setCallback(this,function(response){
        var state = response.getState();
        if(component.isValid() && state === "SUCCESS"){
            component.set("v.tasks",response.getReturnValue());
        }
        else{
            console.log("Failed with state "+state);
        }
    });
    $A.enqueueAction(action);

    component.set("v.newTask.WhatId",component.get("v.recordId"));
},
createNewTask : function(component, event, helper) {
    var action = component.get("c.saveTask");
    var newTask = component.get("v.newTask");
    action.setParams({
        "task": newTask
    });
    action.setCallback(this,function(response)({
        var state = response.getState();
        if(component.isValid() && state === "SUCCESS"){
            var items = component.get("v.tasks");
            items.push(response.getReturnValue());
            component.set("v.tasks",items);
        }
        else{
            console.log("Failed with state "+state);
        }
    });
    $A.enqueueAction(action);
}
})

Apex controller:

public with sharing class TaskController {
  @AuraEnabled
  public static List<Task> loadTasks(Id recordId){
    return[SELECT Subject, ActivityDate FROM Task WHERE WhatId=:recordId];
  }
  @AuraEnabled
  public static Task saveTask(Task task){
    upsert task;
    return task;
   }
}
2
  • can you describe the error more you are getting? Commented Jan 26, 2017 at 19:15
  • Errors i see in console: Event.setParams(): 't'('1485454782370') is not a valid parameter. Valid parameters are 'token', 'querystring' Callback failed: aura://ComponentController/ACTION$getComponent Suppressed Error: AuraError::Uncaught Unable to find 'doInit' on 'compound://c.Task'.
    – Sagnik
    Commented Jan 26, 2017 at 19:23

1 Answer 1

1

1) Take away the Init handler if you are not using it.

2) Do not use same function name for create and save in your client side controller.

3) In your second CreateNewTask function You messed up here :

change this from:

action.setCallback({

to:

action.setCallback(this,function(response){
7
  • Thanks. I modified but still this is not working! I've updated the code now.
    – Sagnik
    Commented Jan 26, 2017 at 19:26
  • what is the error that you see?
    – Rao
    Commented Jan 26, 2017 at 19:26
  • My bad. Just noticed that both the functions in my controllers are named createTask and there's no doInit function. Have modified the code. Retrieval is working fine now but saving isn't.
    – Sagnik
    Commented Jan 26, 2017 at 19:32
  • thats because you are passing recordID to the apex controller and it is null. assign a default value of account/lead related to an existing task and see if the values show on your page
    – Rao
    Commented Jan 26, 2017 at 19:35
  • 1
    Yeah. So, I created a record page on account using app builder and tested it with an account with already existing tasks, it is working fine.
    – Sagnik
    Commented Jan 26, 2017 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .