Solved

Saving contact custom fields giver invalid_field error

So I was trying to follow the API documentation to save a contact with custom fields.

Contact is success created with no custom fields. But when I provide custom fields to it,I get error.


 

$postData = array(
			"name"=> trim($name),
			"email" => trim($email),
			"phone" => trim($phone),
			"mobile" => trim($phone),
			"custom_fields" => array("lb_module_id" => $member_id,
															 "lb_user_id" => $user_id,
															 "lb_module" => $module)
		);
		$url = "https://".$this->domain.".freshdesk.com/api/v2/contacts";
		$this->curl->setOpt(CURLOPT_USERPWD, $this->apiKey.":x");
		$this->curl->setHeader("Content-Type", "application/json");
		$this->curl->post($url, json_encode($postData));
		if ($this->curl->error) {
			var_dump($this->curl->response);
			return 'Error: ' . $this->curl->errorCode . ': ' . $this->curl->errorMessage;
		}
		else {
			return $this->curl->response;
		}

 The error var_dump


 

object(stdClass)[2533]
  public 'description' => string 'Validation failed' (length=17)
  public 'errors' => 
    array (size=3)
      0 => 
        object(stdClass)[2484]
          public 'field' => string 'lbModuleId' (length=10)
          public 'message' => string 'Unexpected/invalid field in request' (length=35)
          public 'code' => string 'invalid_field' (length=13)
      1 => 
        object(stdClass)[2541]
          public 'field' => string 'lbUserId' (length=8)
          public 'message' => string 'Unexpected/invalid field in request' (length=35)
          public 'code' => string 'invalid_field' (length=13)
      2 => 
        object(stdClass)[2542]
          public 'field' => string 'lbModule' (length=8)
          public 'message' => string 'Unexpected/invalid field in request' (length=35)
          public 'code' => string 'invalid_field' (length=13)

 can someone guide me, what am I doing wrong here.


Seems like something ( maybe json_encode ) is converting underscores to camelCase. You should be able to avoid that by changing the names of your custom ticket fields from, say, 'lb_module_id' to 'lbmoduleid'. This is an ugly hack but it should save you time hunting for the cause of the current problem.

 

 Actually i updated the code, but pasted the response from camel casing fields name.

Thought the response was same.


Pardon me for stating the obvious but you have already created those custom fields under Admin > Customer Fields > Contacts tab and you have clicked on [ Save ] ?

You should check what Freshdesk thinks are the custom fields by looking at the output of
curl -v -u Your_API_Key:X -H "Content-Type: application/json" -X GET https://yourdomain.freshdesk.com/api/v2/contact_fields

Reminder: to find Your_API_Key click your profile icon ( upper-right corner ), select 'Profile settings' and look at the bottom of the right sidebar for the "Your API Key" section.


Also, try

curl -v -u Your_API_Key:X -H "Content-Type: application/json" -X POST -d '{ "name" : "Clark Kent", "email" : "superman@freshdesk.com", "custom_fields" : { "test_text" : "some text" } }' https://yourdomain.freshdesk.com/api/v2/contacts

 to create a new contact while setting the 'test_text' custom contact field to "some text" ( replace 'test_text' with one of the custom field names reported by /api/v2/contact_fields ) to make sure the API works as expected.

 

I think i did not do the obvious? Its not in documentation as well

 

Do you see the field names you are expecting in the curl .../api/v2/contact_fields output ?


If yes, can you add new contacts with these custom contact fields using curl ?

yes this works, thank you so much.

Please mark this thread as solved.

Login or Signup to post a comment