Unable to add custom fields while creating new task in asana API using PHP

Hi,

I am trying to add custom fields in asana task through API using PHP, I have followed the JSON format as per the website: https://developers.asana.com/docs/#create-a-task. I have attached my code to this post. Could someone please help me? I am not getting any error but it’s not creating task.

$api = 'xxxxxxxxxxxxxxxxx';
$url = 'https://app.asana.com/api/1.0/tasks';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Don't verify SSL connection
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //         ""           ""
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

$cust = array(
    "mantis_due_date" => "2020-02-03",
    "mantis_url" => "This is some text"
  );
$data = array(
    "data" => array(
	
        "workspace" => "xxxxxxxxxxxxxx",
        "projects" => array('xxxxxxxxxxxx'),
		"custom_fields" => $cust,
        "name" => "add custom field",
        "notes" => "notes1",
        "assignee" => "xxxxxxxxxxx",
		"due_on"=> "2020-03-02"		
 )   
);
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        
$html = curl_exec($curl);   
curl_close($curl);

$html = json_decode($html);
var_dump($html);

Thank you!

I am wondering if you can add the fields at the same time as creating the task. @Phil_Seeman am i right?

I’m not familiar with PHP so I can’t comment on the correctness of the specific syntax, but to @Bastien_Siebman’s comment, that could be the problem. Are those two custom fields already attached to the project? To add a custom field to a task, you first need to:

  1. Create the custom field if it doesn’t currently exist.
  2. Add the custom field to the project if it’s not already attached to the project.

Also, I would try the call without the custom field portion and see if that works.

1 Like

@Phil_Seeman, yes it’s working now after creating custom fields in project

2 Likes