The restlet is used to create a contact for the corresponding customer. The customer id will be sent from the Magento side and in backend side, NetSuite creates a contact for the corresponding customer. Create a contact for an existing customer through restlet.
/**************************************************************************************
Developer: Suravi Lahiri,Anukaran
Development Head: Mr. Sumeet Kulkarni
Company Name: CEINO
Purpose: Create a contact for an existing customer through restlet.
Update: 25.09.17 added lead address
© Copyright All Right
****************************************************************************************/
function POST_customer_data(datain)
{
nlapiLogExecution('DEBUG', 'RESTLET POST', 'IN POST');
var customerObj = datain.data;
var responseObj = new Array();
responseObj['message'] = '';
responseObj['responseCode'] = '';
responseObj['lead_rec_id'] = '';
responseObj['contact_rec_id'] = '';
//Start:LMS Dropbox
var customer_id = datain.customer_id; //sending customer id
nlapiLogExecution('DEBUG', 'RESTLET POST', 'customer_id==' + customer_id);
var lead_name = datain.lead_name; // sending contact name
nlapiLogExecution('DEBUG', 'RESTLET POST', 'lead_name==' + lead_name);
var contact_first_name = datain.contact_first_name; // sending contact first name
nlapiLogExecution('DEBUG', 'RESTLET POST', 'contact_first_name==' + contact_first_name);
var contact_last_name = datain.contact_last_name; // sending contact last name
nlapiLogExecution('DEBUG', 'RESTLET POST', 'contact_last_name==' + contact_last_name);
var lead_first_name = datain.lead_first_name; // sending lead first name
nlapiLogExecution('DEBUG', 'RESTLET POST', 'lead_first_name==' + lead_first_name);
var lead_last_name = datain.lead_last_name; // sending lead last name
nlapiLogExecution('DEBUG', 'RESTLET POST', 'lead_last_name==' + lead_last_name);
var email_address = datain.email_address; // sending email address
nlapiLogExecution('DEBUG', 'RESTLET POST', 'email_address==' + email_address);
if(email_address==null||email_address==""){
return "Please add Email Address";
}
var contact_no = datain.contact_no; // sending contact no
nlapiLogExecution('DEBUG', 'RESTLET POST', 'contact_no==' + contact_no);
var pin_code = datain.pin_code; // sending contact no
nlapiLogExecution('DEBUG', 'RESTLET POST', 'pin_code==' + pin_code);
var country = datain.country; // sending contact no
nlapiLogExecution('DEBUG', 'RESTLET POST', 'country==' + country);
var city = datain.city; // sending contact no
nlapiLogExecution('DEBUG', 'RESTLET POST', 'city==' + city);
var street1 = datain.street1; // sending contact no
nlapiLogExecution('DEBUG', 'RESTLET POST', 'street1==' + street1);
var street2 = datain.street2; // sending contact no
nlapiLogExecution('DEBUG', 'RESTLET POST', 'street2==' + street2);
var salutation=datain.salutation;
var companyname=datain.companyname;
var leadSource =datain.leadsource;
var campaign =datain.campaignevent;
nlapiLogExecution('debug','leadsource',leadSource);
nlapiLogExecution('debug','campaign',campaign);
//if company id is null then lead will be created
if (customer_id == "" || customer_id == null)
{
//salutation, companyname
var new_lead_rec = nlapiCreateRecord('lead'); //creating lead record
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Record Created=' + new_lead_rec);
var lead_sal = new_lead_rec.setFieldValue('salutation', salutation);
var lead_comp = new_lead_rec.setFieldValue('companyname', companyname);
var lead_fname = new_lead_rec.setFieldValue('firstname', lead_first_name);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead First Name=' + lead_fname);
var lead_lname = new_lead_rec.setFieldValue('lastname', lead_last_name);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Last Name =' + lead_lname);
var lead_email = new_lead_rec.setFieldValue('email', email_address);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead email address=' + lead_email);
var lead_phone = new_lead_rec.setFieldValue('phone', contact_no);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Contact No=' + lead_phone);
var lead_source =new_lead_rec.setFieldText('leadsource', leadSource);
var campaign_event =new_lead_rec.setFieldText('campaignevent', campaign);
//updation weblead
var weblead = new_lead_rec.setFieldValue('weblead', 'T');
var tab = new_lead_rec.selectNewLineItem('addressbook');
nlapiLogExecution('DEBUG','Anukaran-Log','tabselected=' +tab);
var pin_code = new_lead_rec.setLineItemValue('addressbook','zip','1',pin_code);
nlapiLogExecution('DEBUG','Anukaran-Log','Pincode=' +pin_code);
var country = new_lead_rec.setLineItemValue('addressbook','country','1',country);
nlapiLogExecution('DEBUG','Anukaran-Log','Country='+country);
var city = new_lead_rec.setLineItemValue('addressbook','city','1',city);
nlapiLogExecution('DEBUG','Anukaran-Log','City=' +city);
var address1 = new_lead_rec.setLineItemValue('addressbook','addr1','1',street1);
nlapiLogExecution('DEBUG','Anukaran-Log','Address1 =' +address1);
var address2 = new_lead_rec.setLineItemValue('addressbook','addr2','1',street2);
nlapiLogExecution('DEBUG','Anukaran-Log','Address2 =' +address2);
var submit_lead_rec = nlapiSubmitRecord(new_lead_rec, false, true);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Record =' + submit_lead_rec);
if (submit_lead_rec != null || submit_lead_rec != "")
{
responseObj['message'] = 'Lead Record added';
responseObj['responseCode'] = '1';
responseObj['lead_rec_id'] = nlapiLookupField('lead', submit_lead_rec, 'entityid');
}
}
// if company id is not null
else
{
var filter = new Array();
filter[0] = new nlobjSearchFilter('entityid', null, 'is', customer_id);
var customer_search_result = nlapiSearchRecord('customer', null, filter, null); // creating save search for customer
nlapiLogExecution('DEBUG', 'PBTI-log', 'Search result = ' + customer_search_result);
//if customer search comes null then again lead will be created
if (customer_search_result == "" || customer_search_result == null)
{
var new_lead_rec = nlapiCreateRecord('lead'); //creating lead record
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Record Created=' + new_lead_rec);
var lead_fname = new_lead_rec.setFieldValue('firstname', lead_first_name);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead First Name=' + lead_fname);
var lead_lname = new_lead_rec.setFieldValue('lastname', lead_last_name);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Last Name =' + lead_lname);
var lead_email = new_lead_rec.setFieldValue('email', email_address);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead email address=' + lead_email);
var lead_phone = new_lead_rec.setFieldValue('phone', contact_no);
//updation weblead
var weblead = new_lead_rec.setFieldValue('weblead', 'T');
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Contact No=' + lead_phone);
var tab = new_lead_rec.selectNewLineItem('addressbook');
nlapiLogExecution('DEBUG','Anukaran-Log','tabselected=' +tab);
var pin_code = new_lead_rec.setLineItemValue('addressbook','zip','1',pin_code);
nlapiLogExecution('DEBUG','Anukaran-Log','Pincode=' +pin_code);
var country = new_lead_rec.setLineItemValue('addressbook','country','1',country);
nlapiLogExecution('DEBUG','Anukaran-Log','Country='+country);
var city = new_lead_rec.setLineItemValue('addressbook','city','1',city);
nlapiLogExecution('DEBUG','Anukaran-Log','City=' +city);
var address1 = new_lead_rec.setLineItemValue('addressbook','addr1','1',street1);
nlapiLogExecution('DEBUG','Anukaran-Log','Address1 =' +address1);
var address2 = new_lead_rec.setLineItemValue('addressbook','addr1','1',street2);
nlapiLogExecution('DEBUG','Anukaran-Log','Address2 =' +address2);
var lead_source =new_lead_rec.setFieldText('leadsource', leadSource);
var campaign_event =new_lead_rec.setFieldText('leadsourcecampaigncategory', campaign);
var submit_lead_rec = nlapiSubmitRecord(new_lead_rec, false, true);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Lead Record =' + submit_lead_rec);
if (submit_lead_rec != null || submit_lead_rec != "")
{
responseObj['message'] = 'Lead Record added';
responseObj['responseCode'] = '1';
responseObj['lead_rec_id'] = nlapiLookupField('lead', submit_lead_rec, 'entityid');
}
}
//if customer search is not null then it will search whether there is any contact created for that existing customer or not.
else if (customer_search_result != "" || customer_search_result != null)
{
var cust_int_id = customer_search_result[0].getId(); // getting existing customer internal id
nlapiLogExecution('DEBUG', 'PBTI-log', 'Customer ID = ' + cust_int_id);
var filter_contact = new Array();
filter_contact[0] = new nlobjSearchFilter('company', null, 'is', customer_id);
filter_contact[1] = new nlobjSearchFilter('email', null, 'is', email_address);
var column = new Array();
column[0] = new nlobjSearchColumn('entityid');
var search_contact_result = nlapiSearchRecord('contact', null, filter_contact, column); // creating save search for customer contact
nlapiLogExecution('DEBUG', 'PBTI-log', 'Contact result = ' + search_contact_result);
// if no contact is present for that customer then it will create a new contact for that customer
if (search_contact_result == "" || search_contact_result == null)
{
var cust_type = nlapiLookupField('customer',cust_int_id,'isperson');
nlapiLogExecution('DEBUG','Suravi-log','type = '+cust_type);
if(cust_type =='F') // checking whether customer type is company or individual
{
nlapiLogExecution('DEBUG','Suravi-log','Creating Contact')
// if customer type is company then a contact record will be created for that customer
var new_contact_record = nlapiCreateRecord('contact');
new_contact_record.setFieldValue('company', cust_int_id);
new_contact_record.setFieldValue('firstname', contact_first_name);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'First Name =' + contact_first_name);
new_contact_record.setFieldValue('lastname', contact_last_name);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Last Name =' + contact_last_name);
new_contact_record.setFieldValue('email', email_address);
nlapiLogExecution('DEBUG', 'Satish-log', 'Email = ' + email_address);
new_contact_record.setFieldValue('phone', contact_no);
nlapiLogExecution('DEBUG', 'Satish-log', 'contact_phone = ' + contact_no);
nlapiLogExecution('debug','contactsource',datain.leadsource);
nlapiLogExecution('debug','contactsourcecampaigncategory',datain.campaignevent);
// new_lead_rec.setFieldText('contactsource', datain.leadsource);
//new_lead_rec.setFieldText('contactsourcecampaigncategory', datain.campaignevent);
var submit_customer_record = nlapiSubmitRecord(new_contact_record, false, true);
nlapiLogExecution('DEBUG', 'Anukaran-log', 'Customer Record =' +submit_customer_record);
if (submit_customer_record != null || submit_customer_record != "")
{
responseObj['message'] = 'Record added';
responseObj['responseCode'] = '1';
responseObj['contact_rec_id'] = submit_customer_record;
//responseObj['contact_rec_id'] = submit_lead_rec;
}
}
// if customer type is individual then contact record will not be created
else
{
responseObj['message'] = 'Record not added';
responseObj['responseCode'] = '1';
responseObj['contact_rec_id'] = 'Customer type is individual';
}
}
// if customer contact is already present then no contact record will be created
else
{
var contact_search_len = search_contact_result.length;
nlapiLogExecution('DEBUG', 'Satish-log', ' Contact Search result length = ' + contact_search_len);
var contact_rec_id = search_contact_result[0].getId();
nlapiLogExecution('DEBUG', 'Satish-log', 'Contact record Id = ' + contact_rec_id);
responseObj['message'] = 'Contact already exists';
responseObj['responseCode'] = '1';
responseObj['contact_rec_id'] = contact_rec_id;
}
}
}
var JSONObj = { "message": responseObj['message'],"responseCode": responseObj['responseCode'], "data": [{"submit_customer_record": responseObj['submit_customer_record']}, {"lead_rec_id": responseObj['lead_rec_id']},{"contact_rec_id": responseObj['contact_rec_id'] }]}
return JSONObj;
}