Hi everyone,
I’m using afterInsert hook to send a confirmation email upon submission to my website.
Basically, I’m calling afterInsert to create a new contact, grabbing its contactId and updating the same item to contain the contactId (see code below).
The problem is, once in a while the update fails because the item is not yet in the collection.
Is it even possible? I assumed that whatever happens in the afterInsert hook happens after the item was inserted…
Any ideas on how to make sure the item is already in my DB?
Attached is my code:
export function Credit_afterInsert(item, context) {
console.log("Sending Confirmation email to: " + item.email);
createContact(item);
return item;
}
export function createContact(item) {
wixCrmBackend.createContact({
"firstName": item['email'],
"emails": [item['email']]
})
.then((result) => {
const contactId = result;
item["contactId"] = contactId;
wixData.update("Credit", item)
.then( (results) => {
console.log("Item updated successfuly. Email: " + item.email);
} )
.catch( (err) => {
console.log("Couldn't update item: " + err + ". Email: " + item.email);
} );
}).catch((err) => console.log("Error while creating contact: " + err + ". Email: " + item.email));
}
This is the error I’m getting:
Error: WDE0073: Item [item] does not exist in collection [collectionName]