# Update the Custom Field using API

**URL:** <https://forum.asana.com/t/update-the-custom-field-using-api/364294>\
**Category:** API & Developers\
**Tags:** custom-fields, rules, tasks\
**Created:** [10 March 2023 10:25 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294 "2023-03-10T10:25:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sachin\_Subhash\_Dhama](https://luna1.co/8d76cd.png) [@Sachin\_Subhash\_Dhama](https://forum.asana.com/u/Sachin_Subhash_Dhama)\
**Post date:** [10 March 2023 10:25 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/1 "2023-03-10T10:25:20Z")

</div>

Hi Asana Team,

I have one requirement where i need to update the one text custom field with some specific value automatically whenever task has been created.

The Value of that field will be generated in specific way like ( value of another custom field value + Current Year + Last 6 digits of Asana Code of task ) and need to be updated after creation of that task.

Thanks,  
Sachin

Thanks,  
Sachin

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [10 March 2023 13:18 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/2 "2023-03-10T13:18:41Z")

</div>

Hi @Sachin_Subhash_Dhama,

You would use the “Update Task” API endpoint to accomplish that:

> **[Update a task](https://developers.asana.com/reference/updatetask)**
>
> A specific, existing task can be updated by making a PUT request on theURL for that task. Only the fields provided in the data block will beupdated; any unspecified fields will remain unchanged.When using this method, it is best to specify only those...

Read up on it in the docs, and let us know if you have specific questions.

---

<div class="post-metadata">

**Author:** ![Sachin\_Subhash\_Dhama](https://luna1.co/8d76cd.png) [@Sachin\_Subhash\_Dhama](https://forum.asana.com/u/Sachin_Subhash_Dhama)\
**Post date:** [10 March 2023 14:29 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/4 "2023-03-10T14:29:50Z")

</div>

Hi @Phil_Seeman  
Thanks for the reply.

How do I run this API when task is being created?  
Is there any listeners where I can get the task id which i can use in rest api?  
Also I want to get the custom field value of created task which i can use as a parameter for rest api How do i get it?

Is there any plugin where i can write this code logic?

Thanks,  
Sachin

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [11 March 2023 00:18 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/5 "2023-03-11T00:18:48Z")

</div>

> [@Sachin\_Subhash\_Dhama](#):
>
> How do I run this API when task is being created?  
> Is there any listeners where I can get the task id which i can use in rest api?

Yes - you have two options.

You can use the Events API - how this works is you call it periodically and it will send you all of the task activity which has occurred since the last time you called it.

Or you can use webhooks - how this works is you register to receive a webhook event each time task activity occurs.

You can read about them both in the API docs and decide which one is more appropriate for your situation.

In both cases, yes, the events include the task ID in the data that comes back from Asana. You then use that ID to read the full task record, which will include the custom field values that exist on that task.

There aren’t any plugins per se, you’ll need to write your own code. But if you’re writing in a language for which Asana has a client library available, I’d recommend you use that so as to save yourself from a bunch of code writing.

---

<div class="post-metadata">

**Author:** ![RZ\_T](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/rz_t/32/404_2.png) [@RZ\_T](https://forum.asana.com/u/RZ_T)\
**Post date:** [12 September 2023 14:50 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/6 "2023-09-12T14:50:08Z")

</div>

@Phil_Seeman how are you today? During my research I realized that you are super active here on the forum, I would like to congratulate you because this type of attitude helps us a lot.

I would like to take advantage of this question but try to adapt it to my case.

We are using a PHP script to create the API for creating a task in an existing project, with its collaborators and assignee person, this is working well, but now we would like to update a custom field option, this custom field has already been created in the project, but we are not able to make it work.

I’m sure that what we are not able to do is this step of updating the custom field

Would you happen to have any tips on where we could go?

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [12 September 2023 17:00 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/7 "2023-09-12T17:00:35Z")

</div>

Hi @RZ_T,

Can you post the code you’re using to try and update the custom field? Thanks!

---

<div class="post-metadata">

**Author:** ![RZ\_T](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/rz_t/32/404_2.png) [@RZ\_T](https://forum.asana.com/u/RZ_T)\
**Post date:** [12 September 2023 17:54 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/8 "2023-09-12T17:54:33Z")

</div>

sure @Phil_Seeman find it below

```auto
// First we create the task
						$result = $asana->createTask(array(
							'workspace' => $workspaceId, // Workspace ID
							'name' => $taskName, // Name of task
							'notes' => $taskName.' Created via Portal', // Notes
							'tags' => $tagName, 
							'due_on' => $dueDate,
							'assignee' => $Assignee, // Assign task to... [Now:Cezar - 276228680639587] [Before: Gabriel - 800859108393810 | BRUNA - 674332302363443]
							'followers' => $FollowerArr, //array('193352372098','132064102863','209184463751','276228680639582','800859108393810') // [Cleide | Richard | Operacional | Cezar | Gabriel] ## We add some followers to the task... (this time by ID), this is totally optional
							'memberships' => [
								[
									'project' => $projectId,
									'section' => $sectionID
								]
							],							
							'custom_field' => [
								'gid': '1203395711079141',
								'project' => $projectId,
								'enum_options': [
									 {
										'gid': '1203395711079153',
										'color': 'red',
										'enabled': true,
										'name': 'Quente',
										'resource_type': 'enum_option'
									 }
								 ]
							]
						));
						// As Asana API documentation says, when a task is created, 201 response code is sent back so...
						if ($asana->hasError()) {
							echo 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
							return;
						}
						
						$taskId = $asana->getData()->gid; // Here we have the id of the task that have been created
					
						// Now we do another request to add the task to a project
						$asana->addProjectToTask($taskId, $projectId);
						$asana->addProjectToTask($taskId, $projectId2);
						
						$file_data = array('file'=>$file,'finalFilename'=>$imgName,'mimeType'=>'image/jpeg');
						$asana->addAttachmentToTask($taskId,$file_data);
						
						$asana->commentOnTask($taskId,$wMsg); //post message
						
						if ($asana->hasError()) {
							echo '<font color="#FF0000">Error occurred: '.$asana->responseCode.'. Error while posting data on ASANA.</font>'; //Error while assigning project to task:
							exit;
						} else {
							echo POST_CREATED.' - <a href="https://app.asana.com/0/'.$projectId.'/'.$taskId.'" target="_blank">'.CLICK_HERE.'</a>'; //'Success to add the task to a project.';
							exit;
						}
					}

```

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [12 September 2023 18:44 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/9 "2023-09-12T18:44:53Z")

</div>

Ah, OK. You are supplying too much information for the custom field option.

Instead, do something like this:

```auto
'custom_field' => [
	'1203395711079141' => '1203395711079153'
]

```

I don’t know PHP so I can’t guarantee that’s the exact right syntax, but the point is you only want to supply the `gid` of the custom field and the `gid` of the enum option to set it to.

---

<div class="post-metadata">

**Author:** ![RZ\_T](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/rz_t/32/404_2.png) [@RZ\_T](https://forum.asana.com/u/RZ_T)\
**Post date:** [13 September 2023 01:18 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/10 "2023-09-13T01:18:31Z")

</div>

Great @Phil_Seeman we are going to try

---

<div class="post-metadata">

**Author:** ![RZ\_T](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/rz_t/32/404_2.png) [@RZ\_T](https://forum.asana.com/u/RZ_T)\
**Post date:** [14 September 2023 11:04 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/11 "2023-09-14T11:04:08Z")

</div>

@Phil_Seeman it worked! I would like to thank you very much!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/system/32/276370_2.png) [@system](https://forum.asana.com/u/system)\
**Post date:** [21 September 2023 11:04 UTC](https://forum.asana.com/t/update-the-custom-field-using-api/364294/12 "2023-09-21T11:04:32Z")

</div>

Closed on 21 September 2023 11:04 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
