Difference between revisions of "Service Properties"

From WHMCS Documentation

(Created page with "Service Properties Introduced in WHMCS 7.2 is the ability to associate a module with a product addon. As part of this, custom fields were also added. To make it simple to int...")
 
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Service Properties
+
<div class="docs-alert-info"><i class="fa fa-question-circle"></i>We added service properties in WHMCS 7.2.</div>
  
Introduced in WHMCS 7.2 is the ability to associate a module with a product addon. As part of this, custom fields were also added. To make it simple to integrate with Custom Fields and modules, WHMCS has added serviceProperties to the Service and Addon models so that a custom field can by dynamically created as it is needed. This removes the need for module developers to instruct on specific custom fields to be created.
+
In WHMCS 7.2 and later, you can associate [[Product Addons|product addons]] with provisioning modules.  
  
Service Properties can also save specific fields that may be directly associated with a service, but might not exist with an addon. See Supported Fields below for more detail.
+
Many provisioning modules require storage of additional information relating to a product. Service properties are key/value pairs for a specific instance of a product or addon, and they facilitate storing and working with extra data. Service properties use custom fields to store this data in a consistent format that provisioning modules can access programmatically.
  
 
===Using Service Properties===
 
===Using Service Properties===
  
Included in the params array passed to each module function is now a model key. This is the model of either the Service or Addon that the action is being run against.
+
A [https://developers.whmcs.com/advanced/db-interaction/ model instance] that represents the service or addon that the module is performing an action against is passed to all module function invocations as part of the module parameters.
  
====Scenario====
+
For example:
As a module developer, I wish to store an Order Number in a custom field which may be used for future commands.
 
  
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
Line 22: Line 21:
 
{
 
{
 
     try {
 
     try {
         //Actions already completed here to create the order and provision it
+
         // Perform actions to provision service and receive back an order number
         //The data is being stored in the $order variable.
+
         $orderNumber = '12345';
  
        $orderNumber = $order['order_number'];
+
         // Save order number to Service Properties
 
 
         // store order number against the current model and service properties
 
 
         $params['model']->serviceProperties->save(['Order Number' => $orderNumber]);
 
         $params['model']->serviceProperties->save(['Order Number' => $orderNumber]);
  
Line 39: Line 36:
 
{
 
{
 
try {
 
try {
//Utilise Service Properties to get the data from the name Order Number
+
// Utilise Service Properties to retrieve the Order Number
        $orderNumber = $params['model']->serviceProperties->get('Order Number');
+
$orderNumber = $params['model']->serviceProperties->get('Order Number');
  
        //Actions to be completed here to suspend the order
+
// Perform actions using order number here
 
 
        return 'success';
 
    } catch (\Exception $e) {
 
        return $e->getMessage();
 
    }
 
  
 +
return 'success';
 +
        } catch (\Exception $e) {
 +
            return $e->getMessage();
 +
        }
 
}
 
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The 'save' method looks for a custom field with the name "Order Number" and saves the data passed to it. If the custom field does not exist, then it is created as an '''Admin Only''' field, and the data is stored.
+
The <tt>save</tt> method will look up a custom field with the given name. If it does not find an existing field, it will create a new custom field to store the value.
The 'get' method will find the data for the passed field name.
 
  
 
===Supported Fields===
 
===Supported Fields===
  
When using service properties on a Service (ie a directly created product, and not an addon), the appropriate field in tblhosting will be updated, and a custom field will not be created.
+
When using service properties against a service (for example, a directly-created product and not an addon), the system may use a dedicated core field rather than a custom field. This occurs for the following field names.
  
 
* Username
 
* Username
Line 71: Line 66:
 
* Last Update
 
* Last Update
  
Using these field names with an addon will still create the appropriate custom field.
+
Using these field names with an addon will create a custom field.

Latest revision as of 20:54, 3 May 2022

We added service properties in WHMCS 7.2.

In WHMCS 7.2 and later, you can associate product addons with provisioning modules.

Many provisioning modules require storage of additional information relating to a product. Service properties are key/value pairs for a specific instance of a product or addon, and they facilitate storing and working with extra data. Service properties use custom fields to store this data in a consistent format that provisioning modules can access programmatically.

Using Service Properties

A model instance that represents the service or addon that the module is performing an action against is passed to all module function invocations as part of the module parameters.

For example:

/**
 * @param array $params
 *
 * @return string
 */
function samplemodule_CreateAccount(array $params)
{
    try {
        // Perform actions to provision service and receive back an order number
        $orderNumber = '12345';

        // Save order number to Service Properties
        $params['model']->serviceProperties->save(['Order Number' => $orderNumber]);

        return 'success';
    } catch (\Exception $e) {
        return $e->getMessage();
    }
}

function samplemodule_SuspendAccount(array $params)
{
	try {
		// Utilise Service Properties to retrieve the Order Number
		$orderNumber = $params['model']->serviceProperties->get('Order Number');

		// Perform actions using order number here

		return 'success';
        } catch (\Exception $e) {
            return $e->getMessage();
        }
}

The save method will look up a custom field with the given name. If it does not find an existing field, it will create a new custom field to store the value.

Supported Fields

When using service properties against a service (for example, a directly-created product and not an addon), the system may use a dedicated core field rather than a custom field. This occurs for the following field names.

  • Username
  • Password
  • Domain
  • License Key
  • Dedicated IP
  • Disk Usage
  • Disk Limit
  • Bandwidth Usage
  • Bandwidth Limit
  • Last Update

Using these field names with an addon will create a custom field.