Create a new case
In order to create a new case, you will need to:
- Get the name of the
CaseType
you want to create a case for (see above) - Fetch the
CaseForm
for creating a case of that type - Create an array of values for each form field
- Submit the values
Fetching the create form
Use the CaseManager
to retrieve the form for creating a new case of the CaseType
you want.
<?php
/** @var \Jadu\Quantum\ServiceApiClient\Manager\CaseManager $caseManager */
$caseManager = $container->get('quantum_service_api_client.manager.case');
$createForm = $caseManager->getCreateForm('case-type-name');
Using the information in the CaseForm
that is returned, you can now build up an array of values to use to create your case. Each CaseFormField
has a unique name, you should use these as the key to each entry in the values array.
More information about CaseForms
<?php
$createForm = $caseManager->getCreateForm('case-type-name');
$values = [];
foreach ($createForm->getFields() as $formField) {
$values[$formField->getName()] = 'VALUE';
}
For example, given the following set of CaseFormFields
:
Name | Type |
---|---|
address | CaseAddressRelationshipField |
website | CaseFieldField.UrlField |
comments | CaseFieldField.TextareaField |
The following array might be created:
<?php
$values = [
'address' => 'AKMKWyo',
'website' => 'http://www.jadu.net/',
'comments' => 'Everything / Possible',
];
Once you have your values array, you can create a new case:
<?php
$case = $caseManager->save($caseForm, $values, null, 'case-type-name');
Checking whether a case can be created
Before trying to create a case, you may want to check whether it can be created, taking into account the case limits configured in the case type.
For example, given a case type with name some-case-type
which requires an address and a person to create a case,
and a limit configured so that there cannot be more than one case of this type associated to any single address.
In this scenario, before creating a new case for a given address, you will want to check whether that address would exceed the case limit. You can do the following:
<?php
/** @var \Jadu\Quantum\ServiceApiClient\Manager\CaseManager $caseManager */
$caseManager = $container->get('quantum_service_api_client.manager.case');
$caseTypeName = 'some-case-type';
$references = [
'primary-address' => 'some-address-reference-or-uprn',
];
try {
$caseManager->canCreateCaseOfType($caseTypeName, $references);
} catch (CaseLimitExceededException $exception) {
/** @var \Jadu\Quantum\ServiceApiClient\Exception\CaseLimitExceededException $exception */
foreach ($exception->getCaseLimitsExceeded() as $caseLimitExceeded) {
echo sprintf('Exceeded case limit of type %s. Maximum number of cases is %d', [
$caseLimitExceeded['limit_type'],
$caseLimitExceeded['limit_number'],
]);
}
}