Jump to content
Remitur

custom client fields values retrieving

Recommended Posts

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

Edited by Remitur

Share this post


Link to post
Share on other sites

Perhaps something like this...

<?php
use WHMCS\User\Client;

// get client's custom fields
$clientFields = Client::find($userID)->customFieldValues;

// run through customfields
foreach($clientFields AS $field){
  //...do something
}

 

Share this post


Link to post
Share on other sites
On 1/11/2019 at 3:44 PM, Remitur said:

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

Did you find a solution to your problem ? Im facing a similar problem!

Share this post


Link to post
Share on other sites
27 minutes ago, steven99 said:

Have you tried the solution mentioned by @mbit ?  That will give you the values you want.  Check WHMCS/User/Client and CustomFieldValue for more info on those objects. 

 

Ok then we continue here.

I now the fieldname as I created it in custom client fields.

In my custom module I want to get the value from customclient field (from the client)

So Example

Customfield = Fax

I want to have the Faxnumber value passed from that client to use to set up a product

Share this post


Link to post
Share on other sites

I understand your goals, but not your current issue.  Did you try the above solution and complete it to your needs?  If so, what was the actual results? 

Share this post


Link to post
Share on other sites
 
	 $clientFields = Client::find($userID)->customFieldValues;
	 foreach($clientFields AS $field){

	 
	 if (!$fieldname)
        {
        	return array("error"=>"Some Error Message");
        }
        else
			
        { $xml .="
			<no-ext-contact:identity type=\"Number\">
			$fieldname1
			</no-ext-contact:identity>";
        } $xml .="
	 }
	 
	

1) Check if there is a value in fieldname1

2) fieldname1 value also used in the XML

Please advice

Share this post


Link to post
Share on other sites
On 1/11/2019 at 3:44 PM, Remitur said:

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

DId you find any good solution to your problem. ? Please let me know and share your thoughts and what solution you came up with.

Share this post


Link to post
Share on other sites

You need to get the field name by $field->customField->fieldName  where you are trying to get the fieldname from a undefined variable .

For example:

foreach($clientFields AS $field)
{
    if (isset($field->customField->fieldName))
    {
        $xml .= "
			<no-ext-contact:identity type='Number'>" .
            $field->customField->fieldName . "
			</no-ext-contact:identity>";
    }
    else
    {
        return array("error" => "Some Error Message");

    }
}

 

Share this post


Link to post
Share on other sites

Okay, then it is something before the foreach that isn't being terminated with a semi-colon (";") .  Would need to see all the code to know for sure. 

Share this post


Link to post
Share on other sites

We get the value now! - but another problem occured described in new topic. The problem was that there had came som strange charchers in the php file that was not visible in Notepad ++ but in Netbeans, Thanks to @steven99

Share this post


Link to post
Share on other sites

Im not quite sure if we are getting the value at all ... as now my xml error is
Parse error at line [28], column [1824]: Element 'identity': ' ' is not a valid value of the atomic type. As its empty not getting any value.
Last code we have and tried is as follow.

 

foreach($clientFields AS $field)
{
    
if (isset($field->customField->fieldName) 
	
and $field->customField->fieldName == "CustomFieldName") 

{ 
    $xml .= $field->customField->value; // Add ID to XML 
}	
    
    else
    {
        return array("error" => "Some Error Message");

    }
}

 

Share this post


Link to post
Share on other sites
On 1/11/2019 at 3:44 PM, Remitur said:

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

Did you find a solution, and if so please share your solution with the community (and me) 🙂

Share this post


Link to post
Share on other sites

Again, you need to change

$xml .= $field->customField->value; // Add ID to XML 

to

    $xml .= $field->value; // Add ID to XML 

Share this post


Link to post
Share on other sites
9 hours ago, steven99 said:

Again, you need to change


$xml .= $field->customField->value; // Add ID to XML 

to


    $xml .= $field->value; // Add ID to XML 

Yes its correct, but its still not getting any values at all into the xml, but at least the xml is right just not getting any value!

Share this post


Link to post
Share on other sites
$clientFields = Client::find($userID)


Would alone return null as $userID is not defined.

But when looking at there should be an easy way to get value for field when knowing the field name.  As there are allready are methods that obtain all the client custom fields. Property for name of custom field, and property for custom field values.

 

https://docs.whmcs.com/classes/7.6/WHMCS/CustomField.html

 

   
Anyone?!

 

 

Share this post


Link to post
Share on other sites

Does anybody know at all a good way to retrive value from Client Custom Field that actually works? Please advice!

Share this post


Link to post
Share on other sites
2 hours ago, ptomter said:

Does anybody know at all a good way to retrieve value from Client Custom Field that actually works? Please advice!

if the client was logged in, then their client custom field values would be available from the $clientsdetails array - which in a hook you could access via $vars

other than querying the database (which is what I would do), then using the class docs as above should work without any issue... though obviously you'll have to define the value of $userID - which I assume that you are doing somewhere ??

Share this post


Link to post
Share on other sites
30 minutes ago, brian! said:

if the client was logged in, then their client custom field values would be available from the $clientsdetails array - which in a hook you could access via $vars

other than querying the database (which is what I would do), then using the class docs as above should work without any issue... though obviously you'll have to define the value of $userID - which I assume that you are doing somewhere ??

Hi @brian!I think there is a misunderstanding somewhere... as this is a custom module, and I try to get the value when I use the module on the customer in the Admin Section.  So im not logged in as the customer!

As It seems that some misunderstanding has been the issue I also wrote a sql query that works in sql that I had hope to use in the meantime. Yes I know its not Laravel and Capsule yet and full_query may be removed from later version, but for now I need a solution to work so I can get the value from the client customer field. to use in the XML (custom module) from Admin Section on a Client, when setting up a product. As there is no auto setup if customer doesnt pay the invoice right away. Then this must be done manually from admin.

$val = 
full_query("
SELECT b.value
FROM tblcustomfieldsvalues b
JOIN tblcustomfields a 
ON a.id = b.fieldid
AND a.fieldname= 'FieldName'
AND b.relid = '1';  // Here could I have $params[customerid] to get the id
"); 
	   
if (isset($val))
	
{$xml .=  $val;} 	
    
else
{
return array("error" => "Field is Empty");
}
	
 $xml .="


 

Share this post


Link to post
Share on other sites

Please see hooks I used for testing below.  It outputs all custom fields for the client custom fields and for their service custom fields.   I have tested this with WHMCS 7.6 and outputs as expected.  First toss that the hooks in to a file and toss that in to whmcs_root/includes/hooks to confirm they are getting the custom fields.  If not, what version of WHMCS are you using?  

In your XML items, just use what I have given below in the foreach loops where it mentions values to fill in the needed areas.  If you still have problems with this, all I can really say is to hire a developer via the requests community. 

I did both client custom fields and service custom fields just in case you or someone else needs either one.  I really doesn't matter where you are calling from as long as you give it a userid .

<?php
/**
 * @author steven99
 */

use WHMCS\User\Client;

add_hook('AdminAreaClientSummaryPage', 1, function($vars) {
    // get client's custom fields
    $HTML = "Client Custom Fields<br>";
    $Client = Client::find($vars['userid']);
    $clientFields = $Client->customFieldValues;

    // run through customfields
    foreach($clientFields AS $field){
        $HTML .= "Field ".$field->customField->fieldName.": ".$field->value."<br>";
    }

    $HTML .= "Service custom fields";
    foreach($Client->services as $service)
    {
        $ServiceFields = $service->customFieldValues;
        foreach($ServiceFields as $field)
        {
            $HTML .= "Service field ".$field->customField->fieldName . ": ".$field->value."<br>";

        }
    }
    return $HTML;
});

//GET VIA API
add_hook('AdminAreaClientSummaryPage', 1, function($vars) {

    $results = localAPI('GetClientsDetails', array(
        'clientid' => $vars['userid'])
    );
    $HTML = "Client Custom Fields from API<br>";

    if ($results['result'] == "success")
    {
        foreach($results as $fieldkey => $fieldName)
        {
            //WHMCS likes to have custom fields returned by API in both clientdetails array and the main array as a
            // variable called "customfieldsX" where "X" is a number.  We use this number to find our index within the
            // customfields array.
            if (preg_match("/customfields(\d+)/", $fieldkey, $Match))
            {
                $FieldID = $Match[1] - 1; // arrays start with 0, but we get a 1 as starter so minus 1 to get the correct index
                if (isset($results['customfields'][ $FieldID ]))
                {
                    $HTML .= "Field $fieldName: ". $results['customfields'][ $FieldID ]['value']."<br>";
                }
            }
        }
    }


    $HTML .= "Service Custom Fields from API<br>";
    $results = localAPI('GetClientsProducts', $postData = array(
        'clientid' => $vars['userid'],));
    if ($results['result'] == 'success')
    {
        foreach($results['products']['product'] as $service)
        {
            foreach($service['customfields']['customfield'] as $field)
            {
                $HTML .= "Field".$field['name'].": ".$field['value']."<br>";
            }
        }
    }

    return $HTML;

});

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines