Create ticket with custom fields


i am using freshdesk API with PHP to create ticket with custom_fields but i am getting error

{"description":"Validation failed","errors":[{"field":"deviceid","message":"Unexpected/invalid field in request","code":"invalid_field"}]}

below is the code

$custom_fields = array("deviceid" => 374029384092);

$ticket_data = json_encode(array(
'description' => 'testing with custom.......',
'subject' => 'custom fiels here....',
'email' => 'sora@yopmail.com',
'priority' => 3,
'status' => 2,
'source' => 3,
'custom_fields' => $custom_fields
));



This topic has been closed for comments

11 replies

Userlevel 4
Badge +12

Hello Zain,


Did you get a chance to look at our documentation for obtaining the field names? You can use the endpoint listed here to get the name of the custom field and then pass it on to the actual request.


Cheers!

@aravind, if i got you point correctly , are you saying that i can only use those field names that are listed in the link given by you, if i want to pass e.g(ABC) as a custom field when creating ticket , then it wont be accepted. did i get your point correctly?



@aravind, I think i didn't get your point, documentation says that i just need to pass the key value pair to custom fields and i am doing the same thing but still i am getting the error. please elaborate a bit more , that why exactly i am getting this error?

Userlevel 4
Badge +12

Zain,


Would you be able to share the X-request ID for the request? That would help us to troubleshoot this further.


Cheers!

$custom_fields = array("custom_text" =>"haksdasjhdk");

$ticket_data = json_encode(array(
'description' => 'testing with custom.......',
'subject' => 'custom fiels here....',
'email' => 'sora@yopmail.com',
'priority' => 3,
'status' => 2,
'source' => 3,
"custom_fields" => $custom_fields
));


$url = "https://$yourdomain.freshdesk.com/api/v2/tickets";

$ch = curl_init($url);

$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

$info = curl_getinfo($ch);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = $server_output;

if($info['http_code'] == 201) {
echo "Tickets has been created, the response is given below."."<br>";
echo '<pre>', print_r($response);
} else {
if($info['http_code'] == 404) {
echo "Error, Please check the end point \n";
} else {
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers;
echo "Response are ".$response;
}
}

curl_close($ch);

this is my whole code i am using to create a ticket with custom fields. will it be helpful ?


still waiting for the response

Userlevel 4
Badge +12

Hello,


Please find the error log for the API request below.


API Unpermitted Parameters. Params : {"description"=>"testing with custom.......", "subject"=>"custom fiels here....", "email"=>"sora@yopmail.com", "priority"=>3, "status"=>2, "source"=>3, "custom_fields"=>{"custom_text"=>"haksdasjhdk"}, "version"=>"v2", "format"=>"json", "action"=>"create", "controller"=>"tickets", "ticket"=>{"description"=>"testing with custom.......", "subject"=>"custom fiels here....", "email"=>"sora@yopmail.com", "priority"=>3, "status"=>2, "source"=>3, "custom_fields"=>{"custom_text"=>"haksdasjhdk"}}} Exception: ActionController::UnpermittedParameters Exception Message: found unpermitted parameters: custom_text


Looks like the field name of the custom text field might be different from what you've passed in the request. Can you please help me with a screenshot of the Admin -> Ticket Fields page?


custom_text is the value that indicates that it is a custom text field but the API request needs the field name to be passed. Please find an example field value below:


{"id":35000717962,"name":"cf_testing","label":"Show only to special users","description":"","position":6,"required_for_closure":false,"required_for_agents":false,"type":"custom_text","default":false,"customers_can_edit":true,"label_for_customers":"Show only to special users","required_for_customers":false,"displayed_to_customers":true,"created_at":"2018-04-18T22:42:29Z","updated_at":"2018-07-06T06:19:40Z"}



In order to use this custom field in API request, the array has to be constructed in this way:


$custom_fields=array("cf_testing"=>"haksdasjhdk");


Hope this is clear!



Cheers!

image

here is the screen shot of admin ticket fields page

I just tried the code with creating custom fields as you mention but still i am getting the same error even tough i have added cf_ before the field name

$custom_fields = array("cf_custom_text"=>"haksdasjhdk");

$ticket_data = json_encode(array(
'description' => 'testing with custom.......',
'subject' => 'custom fiels here....',
'email' => 'sora@yopmail.com',
'priority' => 3,
'status' => 2,
'source' => 3,
"custom_fields" => $custom_fields
));


and here is my data format
{"description":"testing with custom.......","subject":"custom fiels here....","email":"sora@yopmail.com","priority":3,"status":2,"source":3,"custom_fields":{"cf_customtext":"haksdasjhdk"}}


hi Aravind,

I have tried the solution you provided to me, and i have shared the screen shot in my previous comment(not posted yet).  $custom_fields=array("cf_testing"=>"haksdasjhdk");
this solution is not working for me


Userlevel 4
Badge +12

Hi Zain,


Can you please add me as an agent in your account? My email is aravind.s@freshworks.com. I can try logging in and help you construct the API call to include the correct denomination.


The custom field name has to be included in the array with the prefix 'cf'. For example, my custom field name is testing and so my array params would look like this:


$custom_fields=array("cf_testing"=>"haksdasjhdk");


If the custom field name varies, say for example, it is 'account ID' , then the query would be:


$custom_fields=array("cf_account_ID"=>"haksdasjhdk");


Hope this helps!


Cheers!

m too having a same problem


Below is my code


$custom_fields = array("cf_Company" => $cid);
                    $ticket_data = json_encode(array(
                        "description" => $description,
                        "subject" =>$subject,
                        "email" =>$email,
                        "priority" => 3,
                        "status" => 2,
                        "custom_fields" => $custom_fields
                    ));
                    $url = $freshdesk_url."tickets";
                    $ch = curl_init($url);

                    $header[] = "Content-type: application/json";
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                    curl_setopt($ch, CURLOPT_HEADER, true);
                    curl_setopt($ch, CURLOPT_USERPWD, "$freshdesk_api_key:$freshdesk_password");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                    $server_output = curl_exec($ch);
                    $info = curl_getinfo($ch);
                    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
                    $headers = substr($server_output, 0, $header_size);
                    $response = substr($server_output, $header_size);

                    if($info['http_code'] == 201)
                    {
                        $jsonarray['status'] = 'success';
                        $jsonarray['message'] = 'data available';
                        $ary = array();
                        $ary = json_decode($response);
                        $jsonarray['data'] = $ary;
                    }
                    else
                    {
                        if($info['http_code'] == 404)
                        {
                            $jsonarray['status'] = 'failure';
                            $jsonarray['message'] = 'Error, Please check the end point';
                        } else {
                            $jsonarray['status'] = 'failure';
                            $jsonarray['message'] = 'Error, HTTP Status Code : '.$response;
                        }
                    }
                    curl_close($ch);


Erro what m getting


{"status":"failure","message":"Error, HTTP Status Code : {\"description\":\"Validation failed\",\"errors\":[{\"field\":\"cf_Company\",\"message\":\"Unexpected/invalid field in request\",\"code\":\"invalid_field\"}]}"}



Please help


screenshot_282_29_34930.png