APIs & Integrations

IElsanosi
Participant

Not change property value if the property set to null or empty

SOLVE

Hello,

 

Sometime, when the HubSpot property updates, I don't want to earse the existing property value if it is already set.

 

For eample, I have this code:

 

 

 

$contactInput = new SimplePublicObjectInput();
$contactInput->setProperties([
'email' => $user->getEmail(),
'firstname' => $user->getFirstName(),
'lastname' => $user->getLastName(),
'job' => $user->getJob(),
]);

/**  SimplePublicObject $contact */
$contact = $this->hubspot->crm()->contacts()->basicApi()->create($contactInput)

 

 

 

In line "'job' => $user->getJob()", if the  $user->getJob() returns null, I do not want to set job property. How can I achive this?

 

0 Upvotes
1 Accepted solution
Henrik_B_Stein
Solution
Participant | Platinum Partner
Participant | Platinum Partner

Not change property value if the property set to null or empty

SOLVE

Hey IElsanosi,

You can use a conditional statement to check if the value of $user->getJob() is not null before setting the property in the $contactInput array. Here's an example:

$contactInput = new SimplePublicObjectInput();
$contactInputArray = [ 'email' => $user->getEmail(), 'firstname' => $user->getFirstName(), 'lastname' => $user->getLastName(),];

if ($user->getJob() !== null) {
$contactInputArray['job'] = $user->getJob();
}

$contactInput->setProperties($contactInputArray);

$contact = $this->hubspot->crm()->contacts()->basicApi()->create($contactInput);

In this example, we first create an array with the properties we want to set, excluding the 'job' property. Then, we use a conditional statement to check if $user->getJob() is not null. If it's not null, we add the 'job' property to the array with the value of $user->getJob(). Finally, we set the properties in the $contactInput object using the updated array.

This way, if $user->getJob() returns null, the 'job' property will not be set in the $contactInput object, and the existing value of the property in HubSpot will not be overwritten.

I hope this helps, and please let me know if you have further questions.

Best regards,
Henrik Bartenstein

View solution in original post

2 Replies 2
Henrik_B_Stein
Solution
Participant | Platinum Partner
Participant | Platinum Partner

Not change property value if the property set to null or empty

SOLVE

Hey IElsanosi,

You can use a conditional statement to check if the value of $user->getJob() is not null before setting the property in the $contactInput array. Here's an example:

$contactInput = new SimplePublicObjectInput();
$contactInputArray = [ 'email' => $user->getEmail(), 'firstname' => $user->getFirstName(), 'lastname' => $user->getLastName(),];

if ($user->getJob() !== null) {
$contactInputArray['job'] = $user->getJob();
}

$contactInput->setProperties($contactInputArray);

$contact = $this->hubspot->crm()->contacts()->basicApi()->create($contactInput);

In this example, we first create an array with the properties we want to set, excluding the 'job' property. Then, we use a conditional statement to check if $user->getJob() is not null. If it's not null, we add the 'job' property to the array with the value of $user->getJob(). Finally, we set the properties in the $contactInput object using the updated array.

This way, if $user->getJob() returns null, the 'job' property will not be set in the $contactInput object, and the existing value of the property in HubSpot will not be overwritten.

I hope this helps, and please let me know if you have further questions.

Best regards,
Henrik Bartenstein

Jaycee_Lewis
Community Manager
Community Manager

Not change property value if the property set to null or empty

SOLVE

Brilliant 💡 Thank you, @Henrik_B_Stein. If this helps you @IElsanosi, please mark Henrik's answer as a solution.

 

Best,

Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes