Jira Code: TM 77
We need to create the credit card transactions in the NetSuite with item details. But we did not have any method in the web service to create the credit card transaction. So we need a function in NetSuite so we can pass the parameters to that function to create the credit card transactions and return the message whether the transaction is created or not. If the transaction is created, we need the internal ID return from NetSuite. Previously your team does the same for us to update the project task in the jobs from.
Suitelet
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
/*******************************************************************************
*
* Author : Jobin and jismi created on:13/06/2018
* Created an API for custom transaction
* request JSON scriptContext.request.body JSON with credit transaction details
* response {isSucess:BOOLEAN,message:"STRING/OBJECT"}
*
* IF ERROR {"isSucess":false,"message":"ERROR_MESSAGE"}
* IF SUCESS {"isSucess":true,"message":{"creditId":RECORDID(integer)}}
*
*
******************************************************************************/
define(['N/record'], function (record) {
function onRequest(scriptContext) {
var response={}
try {
var JSONobj=JSON.parse(scriptContext.request.body);
log.debug({title:"JSON",details:scriptContext.request.body});
try{
var creditPayment = record.create({
type: "customtransaction_tm77_cust_transaction",
isDynamic: true
});
creditPayment.setText({ fieldId: 'trandate', text: JSONobj.date, ignoreFieldChange: true});
//creditPayment.setValue({fieldId: 'trantype', value: JSONobj.trantype, ignoreFieldChange: true});
creditPayment.setValue({fieldId: 'custbody_tm_tranid', value: JSONobj.transid, ignoreFieldChange: true});
creditPayment.setValue({fieldId: 'custbody_tm_vendor', value: JSONobj.vendorid, ignoreFieldChange: true});
//creditPayment.setValue({fieldId: 'total', value: JSONobj.accountid, ignoreFieldChange: true});
creditPayment.setValue({fieldId: 'memo', value: JSONobj.memo, ignoreFieldChange: true});
//creditPayment.setValue({fieldId: 'custbody_tm_amount', value: JSONobj.usertotal, ignoreFieldChange: true});
var totalAmount = 0.0;
var expenseArray = JSONobj.expense;
for(var i=0;i<expenseArray.length;i++)
{
totalAmount = parseFloat(totalAmount) + parseFloat(expenseArray[i].amount);
}
creditPayment.setValue({fieldId: 'custbody_tm_amount', value: totalAmount, ignoreFieldChange: true});
creditPayment.setValue({fieldId: 'total', value: totalAmount, ignoreFieldChange: false});
if(JSONobj.trantype=='CardChrg')
{
creditPayment.setValue({fieldId: 'custbody_tm_charge', value: true, ignoreFieldChange: true});
}
else{
creditPayment.setValue({fieldId: 'custbody_tm_iscredit', value: true, ignoreFieldChange: true});
}
var creditId = creditPayment.save({
enableSourcing: true,
ignoreMandatoryFields: false
});
}catch(e)
{
log.debug({
title : "ERROR@ Create Cust Transaction type",
details : e.message
});
}
try{
var expenseArray = JSONobj.expense
for (var i = 0; i < expenseArray.length; i++) {
// to create new record
var custRecord = record.create({
type : "customrecord_tm77_cust_tran_record",
isDynamic : true
});
// to enter the fields
custRecord.setValue({
fieldId : 'custrecord_tm_record_id',
value : creditId,
ignoreFieldChange : true
});
custRecord.setValue({
fieldId : 'custrecord_tm_amount',
value : expenseArray[i].amount,
ignoreFieldChange : true
});
custRecord.setValue({
fieldId : 'custrecord_tm_account',
value : expenseArray[i].accountid,
ignoreFieldChange : true
});
custRecord.setValue({
fieldId : 'custrecord_tm_customer',
value : expenseArray[i].customerid,
ignoreFieldChange : true
});
var custRe= custRecord.save({
enableSourcing : true,
ignoreMandatoryFields : false
});
try{
var jobRec = record.submitFields({
type:'job',
id: expenseArray[i].customerid,
values: {
custentity_external_cart:totalAmount,
custentity_transaction_id:creditId,
custentity_vendor:JSONobj.vendorid
}
});
}catch(e)
{
log.debug({
title : "ERROR@ Job Setting",
details : e.message
});
}
}
}catch(e)
{
log.debug({
title : "ERROR@ Create Cust Record type",
details : e.message
});
}
try{
// to set the fields in Job
}catch(e)
{
log.debug({
title : "ERROR@ setting Job fields",
details : e.message
});
}
response={
isSucess:true,
message :"",
creditId:creditId
};
log.debug("CREATED",creditId);
scriptContext.response.write(JSON.stringify(response));
} catch (err) {
response={isSucess:false,message:err.message,creditId:0};
log.debug({title:"ERROR@onRequest",details:err});
scriptContext.response.write(JSON.stringify(response));
}
}
return {
onRequest: onRequest
}
});