Skip to content

Instantly share code, notes, and snippets.

@ailabs-software
Created December 8, 2019 23:49
Show Gist options
  • Save ailabs-software/52b49459caefb0ce3bbccd04045a7624 to your computer and use it in GitHub Desktop.
Save ailabs-software/52b49459caefb0ce3bbccd04045a7624 to your computer and use it in GitHub Desktop.
How to create a person and set fields & email on the new person
var https = require("https");
function callBandWindowAPI( requestOptions, data ) {
return new Promise((resolve, reject) => {
var postReq = https.request(
requestOptions,
function(response) { // console.log('\n\nin fun...', response);
const { statusCode } = response;
if (statusCode >= 300) {
reject(
new Error( response.statusMessage )
);
}
const chunks = [];
response.on('data', (chunk) => {
chunks.push(chunk);
});
response.on('end', () => {
const result = Buffer.concat(chunks).toString();
resolve( JSON.parse(result) );
});
}
);
if (data != null) {
postReq.write( JSON.stringify(data) );
}
postReq.end();
})
}
const api_host = "YOUR_ACCOUNT.shopwindow.io";
const auth_un = "YOUR_API_USERNAME";
const auth_pw = "YOUR_API_PASSWORD";
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + new Buffer( auth_un + ':' + auth_pw ).toString('base64')
};
async function createPersonWithNameAndEmail(firstName, lastName, emailAddress)
{
// First create person, returning id.
var newPersonId = await callBandWindowAPI(
{host: api_host,
path: '/dg3_rpchost/Zealand/crm/DataAPI/PersonDAL/create_restapi',
method: 'POST',
headers: headers},
{}
);
console.log("New person created: " + newPersonId);
// Update fields on new person to add a first and last name. Will only update fields we specify in JSON object below!
await callBandWindowAPI(
{host: api_host,
path: '/dg3_rpchost/Zealand/crm/DataAPI/Fields/FieldsDAL/update',
method: 'POST',
headers: headers},
// data sent: Can be used to set any field, provided format is correct.
// To get the field name to use for any, field:
// 1. Go into a person card
// 2. Click "Organise"
// 3. Double-click on the field of interest.
// 4. To the right of "Personalisation" is the name of the field to use.
{person_id: newPersonId,
data: {firstname: firstName,
lastname: lastName}
}
);
// Add an e-mail address for this person.
await callBandWindowAPI(
{host: api_host,
path: '/dg3_rpchost/Zealand/crm/DataAPI/ContactDAL/create',
method: 'POST',
headers: headers},
// data:
{person_id: newPersonId,
contact: {type: "email", subtype: null, valid: true, active: true, value: emailAddress}
});
}
createPersonWithNameAndEmail("Douglas", "Engelbart", "[email protected]");
@ailabs-software
Copy link
Author

James,
Here's a tip:
Always send a JSON body in your POST, even if you have nothing to send. You can just send "{}" in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment