Get Sales Rep details of a customer from NS to web site

Jira Code: BTN-380

This script takes the customer id from website as a parameter and find the sales rep details of the customer. These details are then return back to the website.

define(['N/error', 'N/record', 'N/search'],
		/**
		 * @param {error} error
		 * @param {record} record
		 * @param {search} search
		 */
		function(error, record, 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     */

	return {
		onRequest: function getSalesRep(context){
			try {
				logme("context.request.method",context.request.method);
				var callBackFuncStr = context.request.parameters.callback;
				logme("callBackFuncStr",callBackFuncStr);
				//If there is callback	
				if(callBackFuncStr != ""){
					try {
						var customerID = context.request.parameters.custid;//get the customer id
						customerID=parseInt(customerID);
						logme("customerID",customerID);
						var customerSearchObj = search.create({//search the customer 
							   type: "customer",
							   filters:
							   [
								  ["internalid","is",customerID]
							   ],
							   columns:
							   [
							      search.createColumn({
							         name: "entityid",
							         sort: search.Sort.ASC,
							         label: "ID"
							      }),			     
							      search.createColumn({name: "salesrep", label: "Sales Rep"}) 
							   ]
							});
						var searchResult = customerSearchObj.run().getRange({
							start : 0,
							end : 1
						});
						//logme('searchResult',searchResult);
						var salesrepName = searchResult[0].getText({ //get the sales rep name
							name : 'salesrep'
						});	
						var salesrepid = searchResult[0].getValue({ //get the sales rep id
							name : 'salesrep'
						});	
						//logme('salesrepName',salesrepName);
						var salesrepSearch = search.create({   //create a search for the sales rep to access the email and phone
							   type: "customrecord_btn_380_jj_sales_rep_detail",							  
							   columns:
							   [{
								   name :'custrecord_sales_rep_phone'
							   },{
								   name : 'custrecord_sales_rep_email'
							   },{
								   name : 'custrecord_sales_rep_id'
							   }
							     
							   ]
							});
						
						var salesrepsearchResult = salesrepSearch.run().getRange({
							start : 0,
							end : 10
						});
						//logme('salesrepsearchResult',salesrepsearchResult);
						var salesrepemail,salesrepphone;
						if(salesrepsearchResult){
					    for(var i=0; i<salesrepsearchResult.length ; i++){
					    	var id = salesrepsearchResult[i].getValue({ //get the sales rep email
								name : 'custrecord_sales_rep_id'
							});
					    	if(id == salesrepid){
						salesrepemail = salesrepsearchResult[i].getValue({ //get the sales rep email
							name : 'custrecord_sales_rep_email'
						});
						salesrepphone = salesrepsearchResult[i].getValue({ //get the sales rep phone
							name : 'custrecord_sales_rep_phone'
						});
					    	}
						}
						}
						var salesrepDetails={     // add the sales rep name,email,phone to array
								'Name': salesrepName,
								'phone': salesrepphone,
								'email':salesrepemail
						};
						//logme('salesrepDetails',salesrepDetails);
						result = salesrepDetails;
						var strJson = callBackFuncStr + '(\'' + JSON.stringify(result)
						+ '\')';
						context.response.write(strJson);
					} catch (e) {
						logme('err@callback',e);
						result = "failure";
						var strJson = callBackFuncStr + '(\'' + JSON.stringify(result)
						+ '\')';
						context.response.write(strJson);
					}
				}
				//If there is no callback
				else{
					logme('failure','No callback function detected');
					result = "failure";
					var strJson = callBackFuncStr + '(\'' + JSON.stringify(result)
					+ '\')';
					context.response.write(strJson);
				}
			} catch (e) {
				logme('err@getSalesRep',e);
				result = "failure";
				var strJson = callBackFuncStr + '(\'' + JSON.stringify(result)
				+ '\')';
				context.response.write(strJson);
			}

		}
	};

});
/*******************************************************************************
 * return error
 * 
 * @param e
 * @returns
 * 
 */
function getError(e) {
	var stErrMsg = '';
	if (e.getDetails != undefined) {
		stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
		+ e.getStackTrace();
	} else {
		stErrMsg = '_' + e.toString();
	}
	return stErrMsg;
}

/*******************************************************************************
 * Log these data
 * 
 * @param title
 * @param details
 * @returns 
 */
function logme(title, details) {
	log.debug({
		title : title,
		details : details
	});
}

Leave a comment

Your email address will not be published. Required fields are marked *