# getTask

### Description

@short: Returns the task object

@signature: getTask: (id: string | number) =\> Task

### Parameters

- `id` - (required) *string | number* -    the task id

### Returns
- ` obj` - (Task) - the task object

### Example

~~~jsx
gantt.addTask({
    id:7,
    text:"Task #5",
    start_date:"02-09-2013",
    duration:28
}, "pr_2");

gantt.getTask(7);
//->{id:7, text:"Task #5", start_date:"02-09-2013", duration:28, 
//   parent:"pr_2", $source:[1,5], $target:[8,13], ...}
~~~

### Details

The task's object return by the **getTask()** method contains 2 important properties you may use to get links related to the task: 

- **$source** - links that comes out from the task.
- **$target** - links that comes into task.

The properties are autogenerated and store ids of the coming-in and coming-out links.

~~~js
const taskObj = gantt.getTask("t1");
 
const sourceLinks = taskObj.$source;        //-> ["l1","l4"] - ids of coming-out links  
const targetLinks = taskObj.$target;       //-> ["l5","l8"] - ids of coming-into links
~~~


## Error

The **getTask** method expects a task with a required "id" is loaded into Gantt. Therefore, if no task with this "id" is found, the method will produce an error message: "Task not found id = ID". 

~~~js
const task = gantt.getTask("fake-id");
...
~~~


We advise you to fix causes of this error before trying to get the task object. To do that, you need to check whether the task exists via the [isTaskExists](api/method/istaskexists.md) method:

~~~js
if(gantt.isTaskExists("fake-id")){
   const task = gantt.getTask("fake-id");
   ...
}
~~~

But you can also [disable these messages before shipping your application to end users](faq.md#an-error-alert-appears-in-the-right-top-corner) via the [show_errors](api/config/show_errors.md) config:

~~~js
gantt.config.show_errors = false;
~~~

### Related API
- [getTaskByTime](api/method/gettaskbytime.md)
- [getTaskNode](api/method/gettasknode.md)
- [isTaskExists](api/method/istaskexists.md)

### Related Guides
- [Task Object/Id](guides/task-object-operations.md)
- [Getting the Link Object/Id](guides/link-object-operations.md#getting-the-links-related-to-a-certain-task)

