Create/Update Company with a custom field

I am attempting to automatically create a Company in FreshDesk when a new customer is created in our CRM system.

This works if I do not include the custom field, so I know the issue is with how I am formatting the json:

{“name”:“aaa-Test 9”,“domains”:[“xxxxxxx.com”], “renewal_date”:“2022-07-27”, “custom_fields” : { “account” : “000001” }}

I also tried:
{“name”:“aaa-Test 9”,“domains”:[“xxxxxxx.com”], “renewal_date”:“2022-07-27”,“account” : “000001” }

In both cases I get “Bad Request”. I get the same results if I create a customer manually and attempt to update the “account” field.

I have confirmed that the “account” field exists, and attempted to update an existing

What is the syntax for the JSON as it relates to custom fields?

Hi @JeromeL

Can you share the request method snippet that you are using to create the company

Regards,
Mughela Chandresh

I can, but as I mentioned, if I change the JSON to omit the custom field (“account”) it works.

    private void CreateFreshDeskCustomer(string name, string emailDomain, string renewal, string account) {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + _FreshDeskDomain + ".freshdesk.com/api/v2/companies/");
        request.ContentType = "application/json";
        request.Method = "POST";
        request.Headers["Authorization"] = "Basic " + _FreshDeskAuthInfo;
        string json = "{\"name\":\"" + name + "\",\"domains\":[\"" + emailDomain + "\"], \"renewal_date\":\"" + renewal + "\", \"custom_fields\" : { \"account\" : \"" + account + "\" }}";

        byte[] byteArray = Encoding.UTF8.GetBytes(json);
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream. 
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object. 
        dataStream.Close();
        try {
            WebResponse response = request.GetResponse();
            // Get the stream containing content returned by the server.
            //Send the request to the server by calling GetResponse. 
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access. 
            StreamReader reader = new StreamReader(dataStream);
            // Read the content. 
            string Response = reader.ReadToEnd();
            //return status code
            LblMsg.Text += String.Format("Status Code: {1} {0}", ((HttpWebResponse)response).StatusCode, (int)((HttpWebResponse)response).StatusCode);
            //return the response 


            var freshdeskid = Response.Substring(Response.IndexOf("\"id\":") + 5, 11);


        }
        catch (Exception ex) {
            LblMsg.Text += "ERROR<br>";
            LblMsg.Text += ex.Message;
            LblMsg.Text += json;
            LblMsg.Text += "<p>";
            LblMsg.Text += json2;
        }

        
    }

Can you try giving the exact JSON in a variable and then stringify it using JSON.stringify instead of you manually stringifying the JSON data?

For example :

const jsonData = {
  name: name,
  domains: [
    emailDomain
  ],
  renewal_date: renewal,
  custom_fields: {
    account: account
  }
}

const data = JSON.stringify(jsonData);

While I wasn’t able to use JSON.stringify (that’s a javascript function, I’m writing in C#), I was able to resolve my issue. I my field (account number) was set to an integer, but I was sending an invalid integer (000001).
Ultimately here’s the JSON that I used:

{“name”:“aaa-Test 9”,“domains”:[“[xxxxxxx.com]”], “renewal_date”:“2022-07-27”,“custom_fields”: {“account”:1}}

Thanks for the help!

1 Like

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