Question:
WHY “wix-crm-backend” createContact(ContactInfo,options)… is not working and ended with FORBIDDEN ERROR…??
Product:
Wix Editor, velo coding on frontend and backend
What are you trying to achieve:
I have a site with a page that includes a wix form. A site member with an specific role has access to this page. The main idea is to complete the form with someone data (a future customer) name, surname, email (main), and personal data. The member sends the form, and the on submit click, the form is sent, and the form data is used to create a new contact in Wix CRM.
What have you already tried:
this is the code in the web page:
$w.onReady(function () {
// Escribe tu código de Javascript aquí usando la API de marco de Velo
// Escribe hola mundo:
// console.log("¡Hola mundo!");
// Llama las funciones en los elementos de la página, por ejemplo:
// $w("#button1").label = "¡Haz clic aquí!";
// Haz clic en "Ejecutar", o ve a la vista previa de tu sitio, para ejecutar el código
});
/**
* Adds an event handler that runs when the element is clicked.
[Read more](https://www.wix.com/corvid/reference/$w.ClickableMixin.html#onClick)
* @param {$w.MouseEvent} event
*/
import { myCreateContactFunction } from "backend/createContact.web.js";
// Función que se ejecuta cuando el usuario envía el formulario
export function button1_click(event) {
// Prevenir comportamiento predeterminado
// Obtener los datos del formulario
Preformatted textconst email = $w("#input3").value;
//const password = $w("#passwordInput").value;
const firstName = $w("#input1").value;
const lastName = $w("#input2").value;
console.log('Contacto a crear :', firstName + " " + lastName +" " + email);
// Crear una cuenta de miembro
myCreateContactFunction(firstName, lastName, email)
.then((contact) => {
console.log('Contacto creado ------Alta Contacto Arraigo Formacion: ', contact );
// Manejar la respuesta, como mostrar un mensaje de éxito
})
.catch((error) => {
console.log('Contacto Error ------Alta Contacto Arraigo Formacion: ', error );
console.error('Error:', error);
});
}
and this is the code in backend createContact.Web.js
import { Permissions, webMethod } from "wix-web-module";
import { contacts } from "wix-crm-backend";
export const myCreateContactFunction = webMethod(Permissions.Anyone, (FirstName, LastName,Email) => {
const contactInfo = {
name: {
first: FirstName,
last: LastName,
},
emails: [
{
email: Email,
primary: true,
tag: "MAIN",
},
],
};
const options = {
allowDuplicates: false,
suppressAuth: false,
};
console.log('--------------pase por createContact.web.js webMethod', contactInfo );
console.log('--------------pase por createContact.web.js webMethod', FirstName +" "+LastName+" "+Email );
return contacts
.createContact(contactInfo, options)
.then((contact) => {
console.log('Contacto creado en ------createContact.web.js', contact);
return contact;
})
.catch((error) => {
console.log('Contacto error:', error);
console.error(error);
});
});
Additional information:
when I have executed, the form is sent, but the createContact() has an FORBIDDEN error and the contact is not created. I have put the Permissions.Anyone.
the logged member in the site is a member with a a role adhoc “AsociadoPDE” but he has not any other privileges