Hallo Wixers I just Wrote code thet auto register users as contacts in website X (mother website) from website Y (daughter website)
Mother website - Recive contacts with Http POST , and save them with WIX BACKEND CRM
Creat new Backend file name - http-functions.js
Add the next code:
import { created, serverError } from 'wix-http-functions';
import wixCrmBackend from 'wix-crm-backend';
export function post_addContact(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return request.body.json()
.then((body) => {
return wixCrmBackend.createContact(body.ans)
.then((contactID) => {
return contactID;
}).catch((errr)=>{console.log(errr);});
})
.then((results) => {
options.body = {
"inserted": results
};
return created(options);
})
.catch((error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
Daughter website - When Contect Created - > Add a tag, and send it to the Mother website.
Creat new Backend file name - events.js
Add the next code:
**you just need to change 2 CONST:
1 - the ID of the tag
2- your Domain
import wixCrmBackend from 'wix-crm-backend';
import wixFetch from 'wix-fetch';
const TAGID = "SomeTagId" //Need to change this ID of the TAG
const DOMAIN = "https://www.yourdomain.com" //Need to change Domain
export function wixCrm_onContactCreated(event) {
const contactId = event.metadata.entityId;
return wixCrmBackend.contacts.labelContact(contactId, [TAGID])
.then((contact) => {
return wixCrmBackend.getContactById(contactId).then((ans)=>{
console.log("contact", ans);
return wixFetch.fetch(DOMAIN + "/_functions/addContact/", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
ans
})
}).then((httpResponse) => {
if (httpResponse.ok) {
console.log("Post ok");
return httpResponse.json();
} else {
console.log(httpResponse);
return Promise.reject("Fetch did not succeed");
}
});
})
})
}
ENJOY