We're writing an Extension for WeClapp (https://www.weclapp.com/api2/). Custom Attributes in WeClapp could be added to almost all Datatypes. These Attributes are accessible over a nested JSON object Array.
Interface example:
export interface ICustomAttribute {
attributeDefinitionId: string;
booleanValue?: boolean;
dateValue?: number;
entityId?: string;
numberValue?: number;
selectedValueId?: string;
stringValue?: string;
}
export interface IContact {
id: string;
...
customAttributes: ICustomAttribute[];
email: string;
...
}
Response Example:
{
"id": "4317",
...
"customAttributes": [
{
"attributeDefinitionId": "4576",
"booleanValue": null,
"dateValue": null,
"entityId": null,
"numberValue": null,
"selectedValueId": null,
"selectedValues": null,
"stringValue": "Test"
},
{
"attributeDefinitionId": "69324",
"booleanValue": true,
"dateValue": null,
"entityId": null,
"numberValue": null,
"selectedValueId": null,
"selectedValues": null,
"stringValue": null
}
],
...
"email": "[email protected]",
...
}
There is no Problem accessing the IContact (contact in the following example) properties expect the [customAttributes] property. How can I access and manipulate Data?
In the following example contact = IContact:
console.log(contact);
Outputs:
[
{
id: '102570',
...
customAttributes: [ [Object], [Object], [Object], [Object] ],
...
}
]
console.log(contact.id); //Outputs: 102570
console.log(contact.customAttributes); //Outputs: undefined
JavaScript Array Extensions (length, ForEach, ...) are not available on [contact.customAttributes], because it's undefined:
let attributes = new Array(0);
attributes = attributes.concat(record.customAttributes);
console.log(attributes); // Outputs: [undefined]
I also tried to Change the interface and tried to reparse:
export interface IContact {
id: string;
...
customAttributes: string;
email: string;
...
}
...
let attributes Array<ICustomAttribute> = JSON.Parse(record.customAttributes);
I have no idea why I can't Access the Array. The strangest thing is that setting attributes does not throw any errors:
let attribute: ICustomAttribute = { attributeDefinitionId: "4576", stringValue: "Test" };
let contact = { customAttributes: [attribute] };
This new entry could be posted and Returns as shown above.
Output of JSON.stringify(contact):
[{"id":"102871",...,"customAttributes":[{"attributeDefinitionId":"4229"},{"attributeDefinitionId":"46381"},{"attributeDefinitionId":"69316"},{"attributeDefinitionId":"98781","stringValue":"77b5d0f1-b1a4-4957-8ea2-ea95969e3c03"}],...,"email":"[email protected]"}]
Output of console.log(contact["customAttributes"]) is undefined.