Jira Code: ME-51
Sync Customers to Netsuite when an Opportunity is created.
Oracle REST APIs in Oracle Sales Cloud to fetch the Opportunities from Oracle Engagement Cloud. From the fetched Opportunities we perform the Customer updation for the Opportunities. We use Opportunity TargetPartyId to fetch the corresponding Customer.
The PartyId of customer available in the CRM is Map to NetSuite Customer PartyId. Customer PartyId is placed in a custom Field CRM Partyid in NetSuite. We are using the Netsuite Customer CRM Partyid and CRM Customer PartyId to match the Customer in CRM to Customer in NetSuite. If Customer is present in NetSuite for the corresponding Customer PartyId in CRM, it loads the Customer and updates customer details if any. In the else part, if a customer is not present, creates a customer with the fetched data and PartyId is set to external id and CRM Partyid Field in NetSuite.
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
/**
* Script Description
* Script to Integrate Customers to NS when opportunities are created in CRM
**/
/*******************************************************************************
* Mangalam
* **************************************************************************
*
* Date: 28-02-2018
*
* Author: Jobin & Jismi IT Services LLP
*
*
* REVISION HISTORY
*
* Revision 1 $ 28-02-2018 rijoy : Created
*
* ****************************************************************************
*/
define(['N/file', 'N/http', 'N/https', 'N/record', 'N/redirect', 'N/runtime', 'N/search'],
/**
* @param {file} file
* @param {http} http
* @param {https} https
* @param {record} record
* @param {redirect} redirect
* @param {runtime} runtime
* @param {search} search
*/
function(file, http, https, record, redirect, runtime, search) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object} context
* @param {ServerRequest} context.request - Encapsulation of the incoming request
* @param {ServerResponse} context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
function onRequest(context) {
try{
var OppChild_response=[];
var Customer_response=[];
var Contact_response=[];
var customers={};
var Opp_response=https.get({
url:'https://eheg-test.fa.em2.oraclecloud.com/crmRestApi/resources/11.13.18.05/opportunities',
headers:{
authorization:'Basic aW1wX3VzZXI6ZmlsaXhAcHJvZDEyMw==',
accept:'application/json'
}
});
var Opp_content=JSON.parse(Opp_response.body);
log.debug("Opp_content",Opp_content);
var Opp_count = Object.keys(Opp_content.items).length;
log.debug("Opp_count",Opp_count);
for(var i=0;i<Opp_count;i++)
{
Customer_response.push(JSON.parse(https.get({
url:'https://eheg-test.fa.em2.oraclecloud.com/crmRestApi/resources/11.13.18.05/accounts?q=PartyId='+Opp_content.items[i].TargetPartyId+'',
headers:{
authorization:'Basic aW1wX3VzZXI6ZmlsaXhAcHJvZDEyMw==',
accept:'application/json'
}
}).body));
}
var Customer_content=Customer_response;
log.debug("Customer_content",Customer_content);
context.response.write(JSON.stringify(Customer_content));
for(var i=0;i<Opp_count;i++){
for(var j=0;j<Customer_content[i].items.length;j++){
if(Customer_content[i].items[j].PartyId=="300000003286497")
{
if(!customers[Customer_content[i].items[j].PartyId]){
var custSearch = search.create({
type:search.Type.CUSTOMER,
columns:[{
name: 'internalid',
}],
filters: [{
name: 'custentity_jj_crm_party_id',
operator: 'is',
values:[Customer_content[i].items[j].PartyId]
}]
});
custSearch.run().each(function(result){
customers[Customer_content[i].items[j].PartyId]=result.getValue({
name:'internalid',
});
return false;
});
}
if(!customers[Customer_content[i].items[j].PartyId])
{
var newCustomer = record.create({
type: record.Type.CUSTOMER,
isDynamic: true,
});
log.debug("Customer_content[i].items[j].PartyId",Customer_content[i].items[j].PartyId);
newCustomer.setValue({
fieldId: 'email',
value:Customer_content[i].items[j].OwnerEmailAddress
});
newCustomer.setValue({
fieldId: 'externalid',
value:''+Customer_content[i].items[j].PartyId
});
newCustomer.setValue({
fieldId: 'custentity_jj_crm_party_id',
value:''+Customer_content[i].items[j].PartyId
});
newCustomer.setValue({
fieldId: 'companyname',
value:Customer_content[i].items[j].OrganizationName
});
newCustomer.setText({
fieldId: 'category',
text:'Customer_content[i].items[j].OrganizationDEO_AccountType_c'
});
newCustomer.setValue({
fieldId: 'url',
value:Customer_content[i].items[j].OrganizationDEO_ACCWebsite_c
});
newCustomer.selectNewLine({
sublistId: 'addressbook'
});
var myaddressSubrecord = newCustomer.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
myaddressSubrecord.setValue({
fieldId: 'country',
value:Customer_content[i].items[j].Country
});
myaddressSubrecord.setValue({
fieldId: 'addr1',
value:Customer_content[i].items[j].AddressLine1
});
myaddressSubrecord.setValue({
fieldId: 'addr2',
value:Customer_content[i].items[j].AddressLine2
});
myaddressSubrecord.setValue({
fieldId: 'city',
value:Customer_content[i].items[j].City
});
myaddressSubrecord.setText({
fieldId: 'state',
text:Customer_content[i].items[j].State
});
myaddressSubrecord.setValue({
fieldId: 'zip',
value:Customer_content[i].items[j].PostalCode
});
newCustomer.commitLine({
sublistId: 'addressbook'
});
var recId = newCustomer.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
customers[Customer_content[i].items[j].PartyId]= recId;
log.debug({
title:'recId',
details: recId
});
}
else{
var CustobjRecord = record.load({
type: record.Type.CUSTOMER,
id:customers[Customer_content[i].items[j].PartyId],
isDynamic: true,
});
recursive(Customer_content[i].items[j].PartyId,10000);
log.debug("Customer_content[i].items[j].PartyId",Customer_content[i].items[j].PartyId);
CustobjRecord.setValue({
fieldId: 'email',
value:Customer_content[i].items[j].OwnerEmailAddress
});
CustobjRecord.setText({
fieldId: 'custentity_jj_crm_party_id',
text:''+Customer_content[i].items[j].PartyId
});
CustobjRecord.setValue({
fieldId: 'companyname',
value:Customer_content[i].items[j].OrganizationName
});
CustobjRecord.setText({
fieldId: 'category',
text:Customer_content[i].items[j].OrganizationDEO_AccountType_c
});
CustobjRecord.setValue({
fieldId: 'url',
value:Customer_content[i].items[j].OrganizationDEO_ACCWebsite_c
});
CustobjRecord.selectNewLine({
sublistId:'addressbook'
});
var myaddressSubrecord = CustobjRecord.getCurrentSublistSubrecord({
sublistId:'addressbook',
fieldId: 'addressbookaddress'
});
myaddressSubrecord.setValue({
fieldId: 'country',
value:Customer_content[i].items[j].Country
});
myaddressSubrecord.setValue({
fieldId: 'addr1',
value:Customer_content[i].items[j].AddressLine1
});
myaddressSubrecord.setValue({
fieldId: 'addr2',
value:Customer_content[i].items[j].AddressLine2
});
myaddressSubrecord.setValue({
fieldId: 'city',
value:Customer_content[i].items[j].City
});
myaddressSubrecord.setValue({
fieldId: 'state',
value:Customer_content[i].items[j].State
});
myaddressSubrecord.setValue({
fieldId: 'zip',
value:Customer_content[i].items[j].PostalCode
});
CustobjRecord.commitLine({
sublistId: 'addressbook'
});
var recId1 = CustobjRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
customers[Customer_content[i].items[j].PartyId]= recId1;
log.debug({
title:'recId1',
details: recId1
});
}
}
}
}
}
catch(e){
log.debug({
title:'err@Get',
details:e
});
}
}
return {
onRequest: onRequest
};
});