Adding Contacts from HTTP-Functions

Hello,

I am trying to allow an external webhook to post new contacts to my site by calling a post function in HTTP-Functions. This post runs multiple steps so I am confident that I am receiving the data correctly, I am even getting it into a custom collection correctly but can not seem to get it into Wix Contacts. Below is the relevant code and subsequent error.

  return request.body.json()
        .then((body) => {
            console.log(body)
 var customerid = body.id
 var firstName = body.first_name;
 var lastName = body.last_name;
 var email = body.email;
 var birthday = body.birth_date;
 var address = body.address;
 var loyaltynumber = body.loyalty_number;
 var loyaltyrefid = body.loyalty_ref_id;
 var okToEmail = body.ok_to_email;
 var zipcode = body.zipcode;
 var totalpurchases = body.total_purchases;
 var totalvisits = body.total_visits;
 var vehicles = body.vehicles;
 var phone = body.phone_number
            wixCrm.updateContact( {
 "firstName": firstName,
 "lastName": lastName,
 "emails": [email],
 "phones": [phone],
 "CustomerID": customerid,
 "Vehicle": vehicles,
 "Birthday": birthday
                } )
            .then( (contactId) => {
            console.log(contactId) // contact created
            } );

Which give me this error:

"["Unhandled rejection Error: Bad Request: please check the user inputs.\n    at /dynamic-modules/edm_root/93c8bfb0-904d-11ea-b5a7-6b45394bc288/node_modules/@wix/wix-crm-backend/src/server-api.ts:32:11\n    at Generator.throw (<anonymous>)\n    at rejected (/dynamic-modules/edm_root/93c8bfb0-904d-11ea-b5a7-6b45394bc288/node_modules/@wix/wix-crm-backend/dist/src/server-api.js:6:65)\n    at bound (domain.js:420:14)\n    at runBound (domain.js:433:12)\n    at tryCatcher (/elementory/node_modules/bluebird/js/release/util.js:16:23)\n    at Promise._settlePromiseFromHandler (/elementory/node_modules/bluebird/js/release/promise.js:547:31)\n    at Promise._settlePromise (/elementory/node_modules/bluebird/js/release/promise.js:604:18)\n    at Promise._settlePromise0 (/elementory/node_modules/bluebird/js/release/promise.js:649:10)\n    at Promise._settlePromises (/elementory/node_modules/bluebird/js/release/promise.js:725:18)\n    at _drainQueueStep (/elementory/node_modules/bluebird/js/release/async.js:93:12)\n    at _drainQueue (/elementory/node_modules/bluebird/js/release/async.js:86:9)\n    at Async._drainQueues (/elementory/node_modules/bluebird/js/release/async.js:102:5)\n    at Immediate.Async.drainQueues [as _onImmediate] (/elementory/node_modules/bluebird/js/release/async.js:15:14)\n    at processImmediate (internal/timers.js:439:21)\n    at process.topLevelDomainCallback (domain.js:131:23)"]"

I have also tried doing this on an afterInsert hook since I am also depositing this info into a different collection but I get the same “Bad Request: please check the user inputs”. I confirmed that my custom fields are names properly.

Any ideas?

You’re missing a " return " before the wixCrm.updateContact().
I’m not saying that’s the only issue, but it’s an issue.

Thanks J.D. I’ll correct but don’t think that is the cause as the code that I am using in the afterInsert hook has the return but gets the same error Albeit a More abbreviated version that just says Bad Request: please check user input

@vincebalsamo It seems that you’re missing contactId (before the contactInfo object).
See:
https://www.wix.com/corvid/reference/wix-crm-backend.html#updateContact

@jonatandor35 Fair point but again I don’t think think that is the issue, I changed the code to updateContact during troubleshooting but get the same error with createContact which would not require contactId

For clarity, here is the code on my afterInsert hook which receives the same error.


export function Customers_afterInsert(item, context) {

 return wixCrm.createContact({
 "firstName": item.firstName,
 "lastName": item.lastName,
 "emails": [item.email],
 "phones": [item.phone]
    })

    .then((contactId) => {
      console.log(contactId) // contact created
    });
}

I don’t see a problem with the hook (except for the lack of return item at the end of the promise chain. But that’s not the issue).
so the error is probably somewhere else.

@jonatandor35 Apparently the issue is with the phone field. I was able to get the rest of the items to create a contact but when phones throws that same error and nothing gets created.

This was a numbers field in the dataset but I changed it to text since that was the only difference between the others but still the same error.