How can I insert a record with a subform using the PHP SDK
This is an update of a question from 3 years ago. There is still no documentation of how to add a record with a subform.
$record = new Record();
$record->addFieldValue(Contacts::FirstName(), $firstName);
$record->addFieldValue(Contacts::LastName(), $lastName);
$record->addFieldValue(Contacts::Email(), $email);
$record->addFieldValue(Contacts::MailingCountry(), $country);
//My subform:
$record->addFieldValue(new Field("QuoteRequest"), [
"dateOfInquiry" => (new DateTime("now"))->format(DateTimeInterface::ATOM),
"subscriptionRequest" => $numSubscriptions
]);
//Get instance of BodyWrapper Class that will contain the request body
$bodyWrapper = new BodyWrapper();
//Set the list to Records in BodyWrapper instance
$bodyWrapper->setData([$record]);
$trigger = array("approval", "workflow", "blueprint");
$bodyWrapper->setTrigger($trigger);
//Get instance of RecordOperations Class that takes moduleAPIName as parameter
$recordOperations = new RecordOperations();
$headerInstance = new HeaderMap();
$rawResponse = $recordOperations->createRecords($moduleApiName->value, $bodyWrapper, $headerInstance);
return $this->getResponses($rawResponse);
When I do this I get an error in the json serialization code:
- {
- "error": "TypeError",
- "message": "get_class(): Argument #1 ($object) must be of type object, string given",
- "line": 95,
- "file": "/code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/Converter.php",
- "code": 0,
- "trace": "
- #0 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/Converter.php(95): get_class(\u00272023-04-24T14:5...\u0027)
- #1 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/JSONConverter.php(460): com\zoho\crm\api\util\Converter-\u003EvalueChecker(\u0027com\\zoho\\crm\\ap...\u0027, \u0027devis\u0027, Array, Array, Array, 0)
- #2 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/JSONConverter.php(60): com\zoho\crm\api\util\JSONConverter-\u003EisRecordRequest(Object(com\zoho\crm\api\record\Record), Array, 0, Array)
- #3 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/JSONConverter.php(596): com\zoho\crm\api\util\JSONConverter-\u003EformRequest(Object(com\zoho\crm\api\record\Record), \u0027com\\zoho\\crm\\ap...\u0027, 0, Array)
- #4 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/JSONConverter.php(489): com\zoho\crm\api\util\JSONConverter-\u003EsetJSONArray(Array, Array)
- #5 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/JSONConverter.php(185): com\zoho\crm\api\util\JSONConverter-\u003EsetData(Array, Array)
- #6 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/JSONConverter.php(68): com\zoho\crm\api\util\JSONConverter-\u003EisNotRecordRequest(Object(com\zoho\crm\api\record\BodyWrapper), Array, NULL, NULL)
- #7 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/util/CommonAPIHandler.php(280): com\zoho\crm\api\util\JSONConverter-\u003EformRequest(Object(com\zoho\crm\api\record\BodyWrapper), \u0027com\\zoho\\crm\\ap...\u0027, NULL, NULL)
- #8 /code/vendor/zohocrm/php-sdk-2.0/src/com/zoho/crm/api/record/RecordOperations.php(143): com\zoho\crm\api\util\CommonAPIHandler-\u003EapiCall(\u0027com\\zoho\\crm\\ap...\u0027, \u0027application/jso...\u0027)
- #9 /code/src/Service/Zoho/ZohoCRM.php(206): com\zoho\crm\api\record\RecordOperations-\u003EcreateRecords(\u0027Contacts\u0027, Object(com\zoho\crm\api\record\BodyWrapper), Object(com\zoho\crm\api\HeaderMap))
- #10 /code/src/Service/Zoho/ZohoContacts.php(41): App\Service\Zoho\ZohoCRM-\u003EcreateRecords(Object(App\Service\Zoho\ModuleApiName), Object(com\zoho\crm\api\record\Record))
- #11 /code/src/Controller/Rest/Admin/ZohoContactController.php(26): App\Service\Zoho\ZohoContacts-\u003EcreateContact()
- #12 /code/vendor/symfony/http-kernel/HttpKernel.php(163): App\Controller\Rest\Admin\ZohoContactController-\u003EAddItem(Object(Symfony\Component\HttpFoundation\Request), Object(App\Service\Zoho\ZohoContacts))
- #13 /code/vendor/symfony/http-kernel/HttpKernel.php(74): Symfony\Component\HttpKernel\HttpKernel-\u003EhandleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
- #14 /code/vendor/symfony/http-kernel/Kernel.php(184): Symfony\Component\HttpKernel\HttpKernel-\u003Ehandle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
- #15 /code/web/index.php(44): Symfony\Component\HttpKernel\Kernel-\u003Ehandle(Object(Symfony\Component\HttpFoundation\Request))\n#16 {main}"
- }