Quick Start Guide
2 min
Use Case: When I change the name on the Salesforce Account I also want that to change the company name on the Breadwinner Company record and on the customer record in NetSuite
This can be done by creating a trigger to update the Customer record in NetSuite any time the Account Name changes on the Salesforce Account

Account with NetSuite Companies (Customers / Vendors)
The below code was created using our Apex Generator and then modified slightly to fit our use case.
Create an apex class to fill in the JSON request needed to update the NetSuite Customer record.
// Start Future Method Class called from Trigger 'BW_AccountTrigger'
public class WriteNetSuiteCustomerFuture {
@future (callout=true)
public static void updateNetSuiteCustomer(String accountRecords) {
List<Account> accountList = new List<Account>();
try{
accountList = (List<Account>)JSON.deserialize(accountRecords , List<Account>.class);
Map<String, String> mapOfAccountIdVsNetSuiteCustomerInternalId = new Map<String, String>();
for(breadwinner_ns__BW_Company__c accountVar:[Select Id, breadwinner_ns__InternalId__c, breadwinner_ns__Salesforce_Account__c From breadwinner_ns__BW_Company__c Where breadwinner_ns__Entity_Type__c = 'Customer' AND breadwinner_ns__Salesforce_Account__c IN : accountList ]){
mapOfAccountIdVsNetSuiteCustomerInternalId.put(accountVar.breadwinner_ns__Salesforce_Account__c, accountVar.breadwinner_ns__InternalId__c);
}
List<Object> netSuiteCustomerList = new List<Object>();
// Iterate over all the passed in Account records
for(Account accountVar : accountList){
Map<String, Object> netSuiteCustomerMap = new Map<String, Object>();
// Assign the values of Account to NetSuite Customer
if(mapOfAccountIdVsNetSuiteCustomerInternalId.containskey(AccountVar.Id)) {
netSuiteCustomerMap.put('internalId',mapOfAccountIdVsNetSuiteCustomerInternalId.get(AccountVar.Id));
netSuiteCustomerMap.put('companyName',accountVar.Name);
netSuiteCustomerList.add(netSuiteCustomerMap);
}
}
Map<String, Object> reqJSONMap = new Map<String, Object>();
reqJSONMap.put('customers' , netSuiteCustomerList);
String reqJSON = JSON.serialize(reqJSONMap);
Map<String, Object> finalReqMap = new Map<String, Object>();
finalReqMap.put('version' , '1.0');
finalReqMap.put('action' , 'updateCustomer');
finalReqMap.put('RequestJSON' , reqJSON);
System.debug('Request ::: ' + finalReqMap);
Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
System.debug('Response ::: ' + respMap);
}catch(Exception e) {
System.debug('Exception Error ::: ' + e);
}
}
}
// End Future Method Class called from Trigger "BW_AccountTrigger"Next: create a trigger to run the above method only if the account name has changed.
// Start Trigger
// trigger executed after Account record is Update...
trigger BW_AccountTrigger on Account (after Update) {
if(!System.isFuture() && !System.isBatch() && Trigger.isUpdate && Trigger.isAfter){
List<Account> accountList = new List<Account>();
for(Account accountVar : trigger.new){
// compare the new account name to the old one
// and add that to the account list only if something has changed.
if(accountVar.Name != trigger.oldMap.get(accountVar.Id).Name){
accountList.add(accountVar);
}
}
if(!accountList.isEmpty()){
WriteNetSuiteCustomerFuture.updateNetSuiteCustomer(JSON.serialize(accountList));
}
}
}
// End Trigger
Now when I change Eastern Mills Corp on the Salesforce Account

to Eastern Mills Enterprises and click 'Save', the change happens in all 3 places.
