Adding and posting custom field values using WHMCS API

Yes, It's another achievement, another milestone for someone who are getting tired by searching solution for sending input values of client custom fields ( Eg. from a registration form) or submitting product/service custom fields (eg. from an order form)  through WHMCS API. It was good to play with the API until some confusion arose while submitting custom fields from input forms.

WHMCS has a well documentation around the API Functions. It specifies that custom fields can be posted as a base64 encoded serialized array of custom field values. Like all other attributes it has to be bind with a post field variable, something like $postfields["customfields"]. So according to one WHMCS API Function: Add Order has the sample:

$postfields["customfields"] = base64_encode(serialize(array("1"=>"Google")));


But confusion may come with these array values (array("1"=>"Google")). What does that "1" mean and why "Google" is assigned as a value of "1"?  It's clear that they are key value pairs in array. But how we can set those keys and values? We may think that this "1" might be a "Field Name" that can be setup through WHMCS admin panel (Setup->Custom Client Fields). For example, if we setup a custom field by name 'VAT' from the panel and want to use it in the customized order form or in registration form then $postfields["customfields"] need that key value pairs. How we can specify that? That's the point where article will explain.

Suppose we added two client custom fields named "VAT" and "DLN" It may come in our mind that the custom fields should write as key value pairs and the array for the custom fields should look like the following:

$customfields = array(
    'VAT' => "value1",
    'DLN' => "value2"
);

$postfields["customfields"] = base64_encode(serialize($customfields));

Does it submit VAT and DLN value either? No, it doesn't. It won't find any reference with VAT and DLN (which was setup from admin panel) with the action ($postfields["action"]) we are performing. All these keys refer to the ids of the individual custom fields. So how can we find that id? The steps are:

1. Login to your WHMCS admin panel.
2. Navigate Setup->Client Custom Fields
3. If there is no custom fields yet create a new one; say named "VAT".
4. After creating the field, see the HTML source from the page by right clicking on page and click view source (in Firefox).
5. Find the text "VAT".
6. You will find something like this line of HTML code (<input type="text" size="30" value="VAT" name="fieldname[11]">)
7. This fieldname[] with id may vary by users admin panel. So track the id from fieldname[11] and yes it's 11. If you find something like <input type="text" size="30" value="VAT" name="fieldname[xx]"> then 'xx' will be the id you have to use in your $customfields array. The array now should look like:

$customfields = array(
    "11" => "123456",
    "12" => "678945"
);

$postfields["customfields"] = base64_encode(serialize($customfields));

Now the reference built up and you will have those values in the WHMCS panel while you are viewing the client details. Similarly, custom fields for Products/Services the similar way should work.

If you find 11, 12, 33, 34, 35, 50 as an ID of custom fields from the view source then process them into array and use it in base64 encoded serialized array and call API functions to perform the rest of the task.

Those who have Firebug installed with their firefox browser they can easily inspect that field using 'inspect element' and can see the id from there. I am not sure if there is another way to track this id, I mean still couldn't find it.

If this article doesn't cover anyone's need please feel free to contact me.

Cheers.

Comments

  1. Nice one!

    An alternative would be to check the database for `tblcustomfields`.`fieldname` ;)

    ReplyDelete
    Replies
    1. True, if the database access is not limited to the developer. Thanks for your comment!! :)

      Delete
  2. Thank you for the tip. We have struggled for several hours toget this done before I found your post. The Whmcs docs should clearly describe the way a custom field is identified.

    ReplyDelete
  3. THANKS SO MUCH for this article - you helped me a lot!

    ReplyDelete
  4. I have an issue that is for me the values are updated in db through API. But when I take client profile from WHMCS admin panel it is not showing the updated value. I have a drop sown of how did you find us and this is showing the first value, But if I change the value and save it from WHMCS admin it is showing the selected value.

    ReplyDelete
  5. Thanks a lot for the hint that one has to find the individual ids of the custom fields! It drove me almost crazy... ;)

    ReplyDelete
  6. Thanks for a great article, how can I add custom fields that allow users to attach attachments during the order process? I am using WHMCS as well

    ReplyDelete
  7. whmcs documentation is bad...

    ReplyDelete
  8. I have created form with custom fields .Do you know what could be form action URL?

    ReplyDelete
  9. Caution: if you are working with products/services custom fields, keep in mind that each product has it ownn fields. So for example "internalID" field for "Small Server" will have ID = 6 but "internalID" field for "Big Server" will have ID = 10

    ReplyDelete
  10. this was the best solution that i find

    ReplyDelete
  11. the cusotmer fileds is used to for create additional fields for product , not for domains.

    ReplyDelete
  12. Thanks and I have a neat supply: Who Repairs House Windows cost to remodel entire house

    ReplyDelete

Post a Comment

Popular posts from this blog

Custom Twitter Feed in Webpage using Twitter API and Javascript

Changing Master Page ContentPlaceHolder Contents From Content Pages