crm-backend

Hi i’m trying to create a contact from a form that i have created I’m using crm-backend, but I’m geeting the below error: " Error: server responded with 403 - {“message”:“permission_denied, details: {"RequiredRole":"Anonymous User","GivenIds":"member → d5e81888-e7f1-4348-932d-dfdf44897d48"}”,“details”:{“givenids”:“member → d5e81888-e7f1-4348-932d-dfdf44897d48”,“requiredrole”:“Anonymous User”,“error”:“permission_denied”,“category”:“api_error”}}"

This is the code at the backend:

import wixCrm from ‘wix-crm-backend’;

export function myBackendFunction(firstName, lastName, emails, phones) {
return wixCrm.createContact( {
“firstName”: firstName,
“lastName”: lastName,
“emails”: [emails],
“phones”: [phones]
} )

}

This is the code at the frontend

export function buttonSubmit_click(event) {
//Add your code for this event here:
let firstName = “Luis”
let lastName = “Perez”
let emails = “ricardo@data.com
let phones = “254124256254”

myBackendFunction(firstName, lastName, emails, phones) 
.then( (contactId) => { 
    console.log(contactId) 
} ) 
. **catch** (err => console.log(err)) 

}

First, make sure that your code is correct for your backend file and you haven’t added or left out any parts of the code:
https://www.wix.com/code/reference/wix-crm-backend.html

Second, in your page code you are going to need onReady and calls to import wix-crm backend and to import your backend function.

There are a few register options here which already have all the backend codes and page codes set up for them, maybe try using these as examples and change your code to suit your needs.
https://www.wix.com/code/reference/wix-users-backend.html#approveByToken

Was this solved?

This was obviously not solved because I am getting the same exact error with the same exact code.
Hoping someone like @yisrael-wix can help us out over here

This is really holding me back from going live with my new site

No idea how to help since you have not provided any code or other information that would indicate what you’re trying to do, and what is happening in fact.

@yisrael-wix I have the exact code as the first comment above:
Front-end code:

import { sendEmail } from "backend/emails.jsw"
$w("#sendEmail").onClick(() => {
    sendEmail("first name","last name","myemail@gmail.com","1111111111")
    .then((answer) => {
        console.log(answer)
        console.log("success")
    })
    .catch((err) => {
        console.log(err)
        console.log("error")
    });
});

Backend Code (emails.jsw):

import wixCrmBackend from 'wix-crm-backend';
export function sendEmail(firstName, lastName, email, phone) {
 return wixCrmBackend.createContact( {
 "firstName": firstName,
 "lastName": lastName,
 "emails": [email],
 "phones": [phone]
  } );
}

When running this on my live site, I get an error, when running from the preview, I get the following error:

Error: server responded with - {"message":"permission_denied, details: {\"RequiredRole\":\"Anonymous User or corvidBackend\",\"GivenIds\":\"member -> 2719d7ed-5eff-48b2-92ad-676777586560\"}","details":{"givenids":"member -> 2719d7ed-5eff-48b2-92ad-676777586560","requiredrole":"Anonymous User or corvidBackend","error":"permission_denied","category":"api_error"}} (403) 

@mennyg19 This will not work correctly in Preview (same as wix-user). What error do you get in Live? It works OK for me, so I asked QA and it works for them as well.

@yisrael-wix I did not get any error message, This is my console in Chrome in Live. The green lines are for when I am logged out. The red line is when I am logged in. This also works for me on my old site ( https://www.chessedvemes.com/send-emails-receipts ). This is only not working on my new site ( https://mennyg4.wixsite.com/website/blank-4 )


Here is the catch code again for reference:

.then((answer) => {
        console.log(answer)
        console.log("success")
    })
    .catch((err) => {
        console.log(err)
    });
I also tried it this way with the same result:
.then((answer) => {
        console.log(answer)
        console.log("success")
    })
    .catch( err => console.log(err));

@mennyg19 What is the error? What’s not working? Is the contact created?

@yisrael-wix The contact is not being created at all if I am logged in. If I am logged out, the contact is created

@mennyg19 You are logged in? Then what contact is being created? If you’re logged in, then are you trying to create a contact for yourself?

@yisrael-wix

Yes does make perfect sense, when logged in you are already a site member so you can’t create a new contact for yourself as you already have one, all you can do is update your contact.

@yisrael-wix Okay, so you gave me a few pointers. If logged in, I cannot create a contact.
I tried just emailing and it works (once the contact is created), thank you.
However, I want to create an admin page (that does not require a login to Wix Dashboard, but requires a login to the site) that can create new contacts and send emails to them. Is that impossible? What about a dashboard page? I tried doing this on a dashboard page and that does not work either.