在Netsuite系统上线过程中,有许多的开发工作。尤其是与第三方系统的集成。第三方系统往往是一些成熟的Saas产品或者Paas产品,以及一些自开发服务系统,例如CRM系统中的salesforce以及其他系统Servify,shopify,shipstation ,EBS等
关于Netsuite系统和第三方系统集成总的来讲有2中方式,一是SuiteTalk,实际上就是Webservice;2是SuiteScript ,是NetSuite系统自己提供的脚本语言。由于写了很多集成接口,因此萌生了写成通用的方式,能够重用的接口。希望能够打成Bundle(NetSuite很强大的一个功能)
SuiteTalk 这种方式,个人认为,比较适合自开发系统,以及提供调Webservice功能的第三方系统。往往需要第三方系统的IT人员进行开发,同时需要懂NetSuite系统。
SuiteScript 这种方式比较普适,往往是NetSuite本系统的开发人员进行开发。(本文j即是采用这种方式)
**需求描述:**
1 第三方系统 的 salesorder 需要同步到 NetSuite系统
2 NetSuite 开放接口,第三方系统调用接口,进行创建和更新的操作
1、通过读取配置文件,循环设定Body Field Value 包括一些需要Search 的值
2、通过读取配置文件,循环Set sublist field value 包括通过setText 和 setValue的方式
3、一些特定的逻辑,请通过UserEvent 的方式进行设定
通过以上思路,可以做到该接口所有场景都适用。开发人员可以通过这种方式开发其他record 的同步接口。
/**
* create a dynamic salesorder record
*/
var soRecord = record.create({
type: 'salesorder',
isDynamic: true
})
for (var key in params.body) {
// No. 1 custom form
if ('customform' == key) {
/**
* if other sys wants set form should set this key
* internal id not provide
*/
if (configObj[key].name) {
} else {
soRecord.setValue({
fieldId: key,
value: params.body[key]
});
}
} else
// No.2 customer field
if ('entity' == key) {
/**
* if ns customer internal id not provide
*/
if (configObj[key].name) {
// search ns customer id by other system customer id
var customerSearchObj = search.create({
type: "customer",
filters:
[
[configObj[key].extraInfo, "is", params.body[key]],
"AND",
["isinactive", "is", "F"]
],
columns:
[
search.createColumn({ name: "internalid", label: "Internal ID" })
]
});
var searchResultCount = customerSearchObj.runPaged().count;
log.debug("customerSearchObj result count", searchResultCount);
if (searchResultCount) {
customerSearchObj.run().each(function (result) {
soRecord.setValue({
fieldId: 'entity',
value: result.id
});
return true;
});
} else {
throw error.create({
name: 'INVALID_VALUE_OF_ENTITY',
message: 'Invalid value entity: ' + params.entity,
notifyOff: false
})
}
/**
* if ns customer internal id is provide
*/
} else {
soRecord.setValue({
fieldId: 'entity',
value: params.body.entity
});
}
} else
// No.3 customer field
if ('subsidiary' == key) {
/**
* if ns subsidiary internal id not provide
*/
if (configObj[key].name) {
soRecord.setText({
fieldId: 'subsidiary',
text: params.body.subsidiary
});
} else {
soRecord.setValue({
fieldId: 'subsidiary',
value: params.body.subsidiary
});
}
} else
// No.4 salesRep
if ('salesRep' == key) {
/**
* set salesRep not internal id so name is true
*/
if (configObj[key].name) {
var employeeSearchObj = search.create({
type: "employee",
filters:
[
[configObj[key].extraInfo, "is", params.body[key]]
],
columns:
[
search.createColumn({
name: "entityid",
sort: search.Sort.ASC,
label: "ID"
})
]
});
var searchResultCount_e = employeeSearchObj.runPaged().count;
log.debug("employeeSearchObj result count", searchResultCount_e);
if (!searchResultCount_e) {
var errorObj = error.create({
name: 'INVALID_KEY_OR_REF',
message: 'Invalid salesrep reference key ' + params.body[key],
notifyOff: false
});
log.debug("Error Code: " + errorObj.name);
throw errorObj;
}
employeeSearchObj.run().each(function (result) {
// .run().each has a limit of 4,000 results
log.debug('result id', result.id)
soRecord.setValue({
fieldId: 'salesrep',
value: result.id
});
return true;
});
// if the ns internal id is provide
} else {
soRecord.setValue({
fieldId: 'salesrep',
value: params.body.salesRep
});
}
} else
/**
* ---------------loop for body field beging-----------------
*/ {
switch (configObj[key].label) {
case 1:
soRecord.setValue({
fieldId: key,
value: params.body[key]
})
break;
case 2:
soRecord.setValue({
fieldId: key,
value: new Date(params.body[key])
})
break;
case 3:
if (configObj[key].name) {
soRecord.setText({
fieldId: key,
text: params.body[key]
});
} else {
soRecord.setValue({
fieldId: key,
value: params.body[key]
})
}
break;
case 4:
if (configObj[key].name) {
var customrecord_ps_custSearch = search.create({
type: params.body[key].recordType,
filters:
[
[configObj[key].extraInfo, "is", params.body[key]]
],
columns:
[
configObj[key].extraInfo
]
});
var searchResultCount = customrecord_ps_custSearch.runPaged().count;
log.debug("customrecord_ps_custSearch result count", searchResultCount);
if (searchResultCount) {
customrecord_ps_custSearch.run().each(function (result) {
// .run().each has a limit of 4,000 results
soRecord.setValue({
fieldId: key,
value: result.id
});
return true;
});
} else {
throw error.create({
name: 'INVALID_VALUE_OF_ENTITY',
message: 'Invalid value ' + key + ': ' + params.body[key],
notifyOff: false
})
}
} else {
soRecord.setValue({
fieldId: key,
value: params.body[key]
})
}
break;
default:
break;
}
}
/** **
* ---------------loop for body field end-----------------
*/
}
/**
* salesorder shippingaddress
*/
var so_shippingaddress = soRecord.getSubrecord({ fieldId: 'shippingaddress' });
log.debug('so_shippingaddress', so_shippingaddress)
// loop the shippingaddress property
for (var shipProperty in params.shippingaddress) {
so_shippingaddress.setValue({ fieldId: shipProperty, value: params.shippingaddress[shipProperty] });
}
for (var key in params.line) {
soRecord.selectNewLine({
sublistId: 'item'
});
// each line
for (var property in params.line[key]) {
// whether pass the ns internal id "Not"
if (configObj[property].name) {
soRecord.setCurrentSublistText({
sublistId: 'item',
fieldId: property,
text: params.line[key][property]
});
} else {
soRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: property,
value: params.line[key][property]
});
}
}
soRecord.commitLine({
sublistId: 'item'
});
}
var soRecord_id = soRecord.save();
log.debug('create record id', soRecord_id)
if (soRecord_id) {
return soRecord_id;
} else {
return 0;
}
}
`
本文是和大家交流学习,有更好的意见欢迎大家留言,我们一起讨论