Implementing NetSuite Add Records Operation With MuleSoft - Part II
In part one, we went through various use cases for performing search operations using the MuleSoft NetSuite connector. In part 2, let's see how to add records.
Join the DZone community and get the full member experience.
Join For FreeIn my previous article, we have gone through various use cases for performing search operations using the MuleSoft NetSuite connector. Here, we will see how we can add records into NetSuite which consists of predefined fields, custom fields, and fields of type RecordRef.
Before we deep dive into various use cases, it is necessary to understand internalId and externalId fields in netsuite record type. Generally, each record type has internalId and externalId.
- internalId: Unique Id in netsuite for each record.
- externalId: Unique Id in the external system for each record. Generally, this is useful when we want to create or update or sync any records from the external system to netsuite.
Use Case 1: - Create Employee Record in NetSuite having predefined fields
We need to create records in NetSuite having predefined fields and no custom fields. We will be using a record type as Employee. Below is data mapping between the External System and Netsuite.
Sr No | External System Fields | Data Type (External System) | Netsuite Fields ( NetSuite Employee) |
Data Type (NetSuite Employee) |
1 | FirstName | string | firstName | string |
2 | MiddleName | string | middleName | string |
3 | LastName | string | lastName | string |
4 | isInactive | boolean | isInactive | boolean |
5 | Mobile | string | mobilePhone | string |
6 | Designation | string | title | string |
7 | Id | string | externalId | string |
Dataweave transformation for the above data mapping will look as shown below and it is very straight forward.
x
%dw 2.0
output application/java
---
payload map ( payload01 , indexOfPayload01 ) -> {
externalId: payload01.Id default '',
firstName: payload01.FirstName default '',
middleName: payload01.MiddleName default '',
lastName: payload01.lastName default '',
isInactive: payload01.isInactive default false,
mobilePhone: payload01.Mobile default '',
title: payload01.Designation default '',
}
To create records in NetSuite, we will be using the Add record operation available in the MuleSoft NetSuite connector, and under connector configuration, we will be using Token Authentication explained in my last article. Add above Dataweave in the Transform message component and placed in front of NetSuite Add Records connector.
Use Case 2: Extend above Dataweave to Add Custom Fields Data Mapping
We will be adding mapping for two custom fields custentity_empsalary, custentity_multiemail in NetSuite.
Sr No | External System Fields | Data Type (External System) | Netsuite Fields ( NetSuite Employee) |
Data Type (NetSuite Employee) |
1 | string | custentity_multiemail | Custom Field (string) | |
2 | Salary | string | custentity_empsalary | Custom Field (string) |
Dataweave for the above data mapping will look like as shown below and it is not straight forward for custom fields as you need to pass namespace.
x
%dw 2.0
output application/java
---
payload map ( payload01 , indexOfPayload01 ) -> {
customFieldList: {
customField: [{
scriptId: "custentity_multiemail",
value: payload.Email default '',
} as Object {
class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
},
{
scriptId: "custentity_empsalary",
value: payload.Salary default '',
} as Object {
class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
}
]
} as Object {
class: "org.mule.module.netsuite.extension.api.CustomFieldList"
}
} as Object {
class: "org.mule.module.netsuite.extension.api.CustomRecord"
}
Use Case 3: Extend Above Dataweave Transformation to Add Field of Type RecordRef
We have field currency of type RecordRef in Employee record. It means the currency field is referring to the Currency record in NetSuite. So we need to map internalId to currency when creating Employee records.
We need to maintain the table or lookup which we will have a mapping between currency and internalId. Currently, we have placed the mapping between currency and internalId in the properties file.
x
netsuite.currency.internalId.USD=1
netsuite.currency.internalId.EUR=2
netsuite.currency.internalId.INR=3
Sr No | External System Fields | Data Type (External System) | Netsuite Fields ( NetSuite Employee) |
Data Type (NetSuite Employee) |
1 | Currency | string | currency | RecordRef |
Dataweave for the above data mapping will look like as shown below and it will lookup internalId for currency in the properties file.
x
%dw 2.0
output application/java
---
payload map ( payload01 , indexOfPayload01 ) -> {
currency: {
internalId: Mule::p('netsuite.currency.internalId' ++ payload01.Currency default "USD")
}
}
Final Dataweave for All the Three Use Cases
x
%dw 2.0
output application/java
---
payload map ( payload01 , indexOfPayload01 ) -> {
externalId: payload01.Id default '',
firstName: payload01.FirstName default '',
middleName: payload01.MiddleName default '',
lastName: payload01.lastName default '',
isInactive: payload01.isInactive default false,
mobilePhone: payload01.Mobile default '',
title: payload01.Designation default '',
currency: {
internalId: Mule::p('netsuite.currency.internalId' ++ payload01.Currency default "USD")
}
customFieldList: {
customField: [{
scriptId: "custentity_multiemail",
value: payload.Email default '',
} as Object {
class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
},
{
scriptId: "custentity_empsalary",
value: payload.Salary default '',
} as Object {
class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
}
]
} as Object {
class: "org.mule.module.netsuite.extension.api.CustomFieldList"
}
} as Object {
class: "org.mule.module.netsuite.extension.api.CustomRecord"
}
Once you Add records in NetSuite, it will respond with internalId and that can be mapped with external system record.
x
[
{
"baseRef": {
"externalId": "123weew12323223",
"type": "EMPLOYEE",
"internalId": "3941",
"name": null
},
"status": {
"statusDetail": [
{
"code": null,
"message": null,
"type": "ERROR",
"afterSubmitFailed": false
}
],
"isSuccess": true
}
}
]
We can do a similar mapping for the update, upsert, and other operations in the NetSuite connector.
Now, you know how to use Add Record operation and data transformation with the MuleSoft NetSuite connector.
Opinions expressed by DZone contributors are their own.
Comments