{{announcement.body}}
{{announcement.title}}

Implementing NetSuite Add Records Operation With MuleSoft - Part II

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

In 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.

JSON
 




x


 
1
%dw 2.0
2
output application/java
3
---
4
payload map ( payload01 , indexOfPayload01 ) -> {
5
    externalId: payload01.Id default '',
6
    firstName: payload01.FirstName default '',
7
    middleName: payload01.MiddleName default '',
8
    lastName: payload01.lastName default '',
9
    isInactive: payload01.isInactive default false,
10
    mobilePhone: payload01.Mobile default '',
11
    title: payload01.Designation default '',
12
}


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.

adding employee in record type

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 Email 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.

JSON
 




x


1
%dw 2.0
2
output application/java
3
---
4
payload map ( payload01 , indexOfPayload01 ) -> {
5
    customFieldList: {
6
        customField: [{
7
            scriptId: "custentity_multiemail",
8
            value: payload.Email default '',
9
        } as Object {
10
            class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
11
        },
12
        {
13
            scriptId: "custentity_empsalary",
14
            value: payload.Salary default '',
15
        } as Object {
16
            class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
17
        }
18
        ]
19
    } as Object {
20
        class: "org.mule.module.netsuite.extension.api.CustomFieldList"
21
    }
22
} as Object {
23
    class: "org.mule.module.netsuite.extension.api.CustomRecord"
24
}


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.

Plain Text
 




x


 
1
netsuite.currency.internalId.USD=1
2
netsuite.currency.internalId.EUR=2
3
netsuite.currency.internalId.INR=3
4
 


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.

JSON
 




x
34
9





1
%dw 2.0
2
output application/java
3
---
4
payload map ( payload01 , indexOfPayload01 ) -> {
5
    currency: {
6
        internalId: Mule::p('netsuite.currency.internalId' ++ payload01.Currency default "USD")
7
        }
8
}


Final Dataweave for All the Three Use Cases

JSON
 




x





1
%dw 2.0
2
output application/java
3
---
4
payload map ( payload01 , indexOfPayload01 ) -> {
5
    externalId: payload01.Id default '',
6
    firstName: payload01.FirstName default '',
7
    middleName: payload01.MiddleName default '',
8
    lastName: payload01.lastName default '',
9
    isInactive: payload01.isInactive default false,
10
    mobilePhone: payload01.Mobile default '',
11
    title: payload01.Designation default '',
12
    currency: {
13
        internalId: Mule::p('netsuite.currency.internalId' ++ payload01.Currency default "USD")
14
        }
15
    customFieldList: {
16
        customField: [{
17
            scriptId: "custentity_multiemail",
18
            value: payload.Email default '',
19
        } as Object {
20
            class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
21
        },
22
        {
23
            scriptId: "custentity_empsalary",
24
            value: payload.Salary default '',
25
        } as Object {
26
            class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"
27
        }
28
        ]
29
    } as Object {
30
        class: "org.mule.module.netsuite.extension.api.CustomFieldList"
31
    }
32
} as Object {
33
    class: "org.mule.module.netsuite.extension.api.CustomRecord"
34
}


Once you Add records in NetSuite, it will respond with internalId and that can be mapped with external system record.

JSON
 




x


 
1
[
2
  {
3
    "baseRef": {
4
      "externalId": "123weew12323223",
5
      "type": "EMPLOYEE",
6
      "internalId": "3941",
7
      "name": null
8
    },
9
    "status": {
10
      "statusDetail": [
11
        {
12
          "code": null,
13
          "message": null,
14
          "type": "ERROR",
15
          "afterSubmitFailed": false
16
        }
17
      ],
18
      "isSuccess": true
19
    }
20
  }
21
]


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.

Topics:
mulesoft, mulesoft 4, netsuite

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}