Best answer

Need to be able to suppress null items.

  • 15 August 2020
  • 2 replies
  • 55 views

Userlevel 2
Badge

I am working on a script for where I want to pass order information. I am supporting up to three order items. The issue is if there is only one order item, blank values are still being passed for the two other items, creating blank rows. Here is what I have so far:



const options = {
url: 'https://edapi.campaigner.com/v1/Orders',
method: 'POST',

headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-KEY': bundle.authData.ApiKey
},
params: {
'ApiKey': bundle.authData.ApiKey
},
body: {
'EmailAddress': bundle.inputData.EmailAddress,
'OrderNumber': bundle.inputData.OrderNumber,
'PurchaseDate': bundle.inputData.PurchaseDate,
'TotalAmount': bundle.inputData.TotalAmount,
'Items':[
{"ProductName": bundle.inputData.ProductName,
"SKU": bundle.inputData.SKU2,
"Quantity": bundle.inputData.Quantity,
"UnitPrice": bundle.inputData.UnitPrice,
"Status": bundle.inputData.Status},

{"ProductName": bundle.inputData.ProductName2,
"SKU": bundle.inputData.SKU2,
"Quantity": bundle.inputData.Quantity2,
"UnitPrice": bundle.inputData.UnitPrice2,
"Status": bundle.inputData.Status2},

{"ProductName": bundle.inputData.ProductName3,
"SKU": bundle.inputData.SKU3,
"Quantity": bundle.inputData.Quantity3,
"UnitPrice": bundle.inputData.UnitPrice3,
"Status": bundle.inputData.Status2},
]

}

};


return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;
// You can do any parsing you need for results here before returning them
return results;
});



 

I am thinking that I need an if statement of maybe or something?

icon

Best answer by hank3rd 19 August 2020, 00:11

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 2
Badge

I was able to get some help on this. 

 

const options = {
url: 'https://edapi.campaigner.com/v1/Orders',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-KEY': bundle.authData.ApiKey
},
params: {
'ApiKey': bundle.authData.ApiKey
},
body: {
'EmailAddress': bundle.inputData.EmailAddress,
'OrderNumber': bundle.inputData.OrderNumber,
'PurchaseDate': bundle.inputData.PurchaseDate,
'TotalAmount': bundle.inputData.TotalAmount,
'Items':[],
}
};
// Add Items to array if they exist
if (bundle.inputData.ProductName) {
options.body.Items.push({
"ProductName": bundle.inputData.ProductName,
"SKU": bundle.inputData.SKU,
"Quantity": bundle.inputData.Quantity,
"UnitPrice": bundle.inputData.UnitPrice,
"Status": bundle.inputData.Status});
}
if (bundle.inputData.ProductName2) {
options.body.Items.push({
"ProductName": bundle.inputData.ProductName2,
"SKU": bundle.inputData.SKU2,
"Quantity": bundle.inputData.Quantity2,
"UnitPrice": bundle.inputData.UnitPrice2,
"Status": bundle.inputData.Status2});
}
if (bundle.inputData.ProductName3) {
options.body.Items.push({
"ProductName": bundle.inputData.ProductName3,
"SKU": bundle.inputData.SKU3,
"Quantity": bundle.inputData.Quantity3,
"UnitPrice": bundle.inputData.UnitPrice3,
"Status": bundle.inputData.Status3});
}
return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;
// You can do any parsing you need for results here before returning them
return results;
});

Userlevel 7
Badge +8

@hank3rd glad you were able to crack this one- thank you for sharing!