Backend CRM Events

Hi Wix,

I’m testing out existing crm-backend events work (eg onCardCreated, onCardMoved). The current docs have no info on how to connect the events.js. I’m specifically looking for info on the red highlights…:

/*event.js*/
import wixCrm from 'wix-crm-backend'

export function wixCrm_onCardMoved(event) {
// do something
}

------------------------------------------

/*backendModules.jsw*/
import { wixCrm_onCardMoved } from 'backend/event'

export function cardFunction() {
// Whether passing an event parameter is necessary 
wixCrm_onCardMoved(event?)
}

------------------------------------------

/*frontendModules*/
import { cardFunction } from 'backend/backendModules'

$w.onReady(function () {
// Whether a trigger is necessary for running the module on the live site
cardFunction()
}

Appreciate any thoughts :slight_smile:

PS: Copied from this comment

There is already a lot of info about it all in the Wix API Reference.
https://www.wix.com/corvid/reference/wix-crm-backend.Events.html#onCardCreated
https://www.wix.com/corvid/reference/wix-crm-backend.Events.html#onCardMoved

These events apply to workflows as shown here.
https://support.wix.com/en/article/about-workflows

You an find out more about them in their own API Reference here.
https://www.wix.com/corvid/reference/wix-crm-backend.workflows.html

Thanks, I actually went through them thoroughly (thus my code sample). But it lacks info on how to connect the modules. Where you able to make it work?

@asralsuvilgaa
If you read the API info, it looks like it does it all in the events.js backend file, so in theory you just need to get the data returned from that function.

If you have used the code in your backend events.js file…

onCardMoved( )
An event that fires when a workflow card is moved.
Description
The onCardMoved() event handler runs when a workflow card is moved programmatically using the moveCard() function or manually in your site’s Dashboard. The received CardMovedEvent object contains information about the card that was moved.

Examples

An event when a card is moved

// Place this code in the events.js file
// of your site's Backend section.

export function wixCrm_onCardMoved(event) {
let workflowId = event.workflowId;
let cardName = event.card.name;
}

/*  Full event object:
*  {
*    "workflowId": "f2cb00b5-9286-4d35-b41b-367d6fec43ce",
*    "workflowName": "n89f2df2-2919-43jk-3uio-v90se98s5ipf",
*    "newPhaseId": "In Progress",
*    "newPhaseName": "sd98iop3-87v3-ni4u-9438-vjc9hu8ec80w",
*    "previousPhaseId": "b00831ee-cc5d-4447-81d7-2ca1828f07b2",
*    "previousPhaseName": "To-do",
*    "card": {
*      "name": "This is a card.",
*      "id": "gd83brj9-34n8-ce8h-fh08-4e90cni3df89",
*      "contactId": "ej8994ed-h3e9-uh2w-fhu0-vj013in3cej8",
*      "createdAt": 2019-03-13T00:00:00.000Z,
*      "updatedAt": 2019-06-12T00:00:00.000Z
*    }
*  }
*/

As it states that it should either trigger the onCardMoved() event handler when you either move the card manually through your dashboard or if you have added any code that moves the card for you.

Move a card

import wixCrm from 'wix-crm-backend';

export async function moveCard(cardId, phaseId){
return await wixCrm.workflows.moveCard(
cardId,
{
"newPhaseId": phaseId,
"newPosition": 0
}
);
}

// Returns a promise that resolves to void.

Have you just tried doing the basic code examples as they are given and adding a basic way of receiving the backend module data, have a look here for a quick example.

I haven’t used it myself personally, so it is only try/test it for now and see what happens, hopefully Shan or a Wix Mod comes back with a much better reply in the other forum post about it.

Also, make sure that the testing is all done on a fully published website and not just through the preview mode otherwise your backend events will not work.