Skip to main content

Creating Clients & Cases

A case and a client are more or less the same thing. The case represents the relationship to your client. In terms of creating a new case/client, the case works as a kind of 'container' for the client. Therefore, when creating a single client, you have to create the case itself and then add the client to the case. When adding a person client, you can use the AddPersonClientToCase mutation and for company clients use AddCompanyClientToCase.

GraphQL mutations for existingCase, createCase, addPersonClientToCase and addCompanyClientToCase

existingCase is for checking if a Client Relationship already exists. It uses the cases query. In this example it returns ExternalId_GenericCustomerNumber, but it can return any field from the Company object.

query existingCase($amount: Int, $filters: [CaseFilters]) {
cases(amount: $amount, filters: $filters) {
items {
item {
id
name
clients {
... on Company {
ExternalId_GenericCustomerNumber {
payload
}
}
}
}
}
}
}

createCase is for creating the empty case:

mutation createCase($notificationUserId: ID) {
createCase(notificationUserId: $notificationUserId) {
id
name
timeCreated
timeLastEdited
}
}

The following two mutations are for adding a client to an empty case:

mutation addPersonClientToCase($id: ID!, $info: PersonInfo!) {
addPersonClientToCase(id: $id, info: $info) {
id
name
timeCreated
timeLastEdited
}
}

mutation addCompanyClientToCase($id: ID!, $info: CompanyInfo!) {
addCompanyClientToCase(id: $id, info: $info) {
id
name
timeCreated
timeLastEdited
}
}

Mandatory and non-mandatory fields

It is possible to create a client with almost no information if done manually, however, it is highly recommended that you add a name to the client as it will be hard to add later.

Checking if a Client Relationship already exists

Before you create a case with a client, it is recommended to check if there is already an existing Client Relationship to avoid duplicate cases. The following code checks if there are any cases with a certain externalID which represents the company registration number. externalIDType represents the type of ExternalId.

public async existingCase(externalIDType: string, externalID: string): Promise<FetchResult<ExistingCaseQuery>> {
return this.gqlClient.query({
query: ExistingCase,
variables: {
amount: 10000,
filters: [
{
externalId: {
type: externalIDType,
in: [externalID],
},
},
],
},
})
.then((existingCaseResult) => existingCaseResult.data.cases.items)
.catch((error) => {
console.log(error);
});
}

Creating an empty case with createCase mutation

When creating a new client you have to first create the case and then later add the client to the created case. The following code creates a case.

public async createCase(notificationUserId?: string | undefined): Promise<CreateCaseMutation> {
return this.gqlClient.mutate({
mutation: CreateCase,
variables: {
notificationUserId,
},
})
.then((createCaseResult) => {
console.log('Created case with id: ', [createCaseResult.data.createCase!.id]);
if (notificationUserId) {
console.log('Added notification user: ', [notificationUserId]);
}
return createCaseResult.data;
})
.catch((error) => console.log(error));
}

Adding a company client with addCompanyClientToCaseGenericCustomerNumber mutation

When the case is created, a new client can be added. In this example it is done with just the company identification number, which corresponds to adding a client in the graphical interface.

public async addCompanyClientToCaseGenericCustomerNumber(caseId: string, GenericCustomerNumber: InputMaybe<string>): Promise<FetchResult<AddCompanyClientToCaseMutation> | void> {
return this.gqlClient.mutate({
mutation: AddCompanyClientToCase,
variables: {
id: caseId,
info: { ExternalId_GenericCustomerNumber: [GenericCustomerNumber] },
} as AddCompanyClientToCaseMutationVariables,
});
}

If you want to create a company client with a generic business registration number, you can follow this example.

public async addCompanyClientToCaseGenericBusinessRegistration(caseId: string, genericBusinessRegistration: string, orgCountryCode: string, name: InputMaybe<string>): Promise<FetchResult<AddCompanyClientToCaseMutation> | void> {
const companyInformation : CompanyInfo = {};

companyInformation.Name = [name];

companyInformation.ExternalId_GenericBusinessRegistrationNumber = [{
countryCode: orgCountryCode,
registrationNumber: genericBusinessRegistration,
}];

return this.gqlClient.mutate({
mutation: AddCompanyClientToCase,
variables: {
id: caseId,
info: companyInformation,
} as AddCompanyClientToCaseMutationVariables,
});
}

Adding a person client with addPersonClientToCase mutation

When adding a person client to a case you can use the same logic as for the company client. Here is an example of this:

public async addPersonClientToCaseName(caseId: string, personName: InputMaybe<string>): Promise<FetchResult<AddCompanyClientToCaseMutation> | void> {
return this.gqlClient.mutate({
mutation: AddPersonClientToCase,
variables: {
id: caseId,
info: { Name: [personName] },
} as AddPersonClientToCaseMutationVariables,
});
}

Code Example

import PublicApiClient from './services/PublicApiClient';
import 'dotenv/config';

(async () => {
const publicApiClient = new PublicApiClient();

const GenericCustomerNumber = '35633766';
const externalIdType = 'ExternalId_GenericCustomerNumber';

const existingCase = await publicApiClient.existingCase(externalIdType, GenericCustomerNumber);

if (Array.isArray(existingCase) && existingCase.length === 0) {
const createdCase = await publicApiClient.createCase();

await publicApiClient.addCompanyClientToCaseGenericCustomerNumber(String(createdCase.createCase?.id), GenericCustomerNumber);
}
})();

AddCompanyClientToCase takes an ID and a CompanyInfo object.