Name

sn_customerservice.CaseDao

Description

Dao for use cases related to Case object.

Script

var CaseDao = Class.create();
CaseDao.prototype = {
  initialize: function() {
  	this.logger = global.CSMBaseLogger.getLogger("CaseDao");
  },

  createCase : function(params){
  	this.logger.debug("inside CaseDao:createCase params = " + JSON.stringify(params));
  	var caseGr = global.CSMBaseAPIUtils.prepareGlideRecord(params, global.CSMBaseConstants.CASE_TABLE);
  	if(!caseGr)
  		return null;
  	var tid = caseGr.insert();
  	this.logger.info("CaseDao:createCase tid = "+tid);

  	var socialProfileId;
  	//Create social profile or attach existing social profile to the case.
  	if(tid && params.social_handle && params.social_channel && params.social_handle_url && params.social_handle_type){

  		socialProfileId = this.bindEntityToSocialProfile(params.social_handle_type, params.social_handle, params.social_handle_url, params.social_channel, caseGr);
  		this.bindSocialProfileToCase(socialProfileId, caseGr.getValue("sys_id"));

  		var socialLogInput = this.prepareSocialLogInput(params.social_post_url, socialProfileId, caseGr);
  		var slid = this.socialUtils.createSocialLog(socialLogInput);
  	}

  	return {"sys_id": tid, "number": caseGr.getValue("number")};
  },

  updateCase: function(sysId, params, getCaseDetails, displayValue, selectedFields){
  	var output = null;
  	var caseId = this.getCaseById(sysId);
  	if(caseId){
  		var caseGr = global.CSMBaseAPIUtils.prepareUpdateGlideRecord(sysId, params, global.CSMBaseConstants.CASE_TABLE);
  		var tid = caseGr.update();
  		this.logger.info("CaseDao:updateCase tid = "+tid);

  		if(tid && params.social_handle && params.social_channel && params.social_handle_url && params.social_handle_type){
  			var socialProfileId = this.bindEntityToSocialProfile(params.social_handle_type, params.social_handle, params.social_handle_url, params.social_channel, caseGr);
  			this.bindSocialProfileToCase(socialProfileId, caseGr.getValue("sys_id"));

  			var socialLogInput = this.prepareSocialLogInput(params.social_post_url, socialProfileId, caseGr);
  			var slid = this.socialUtils.createSocialLog(socialLogInput);
  		}
  		if(getCaseDetails.equals('true'))
  			output = this.getCaseById(sysId, displayValue, selectedFields);
  		else 
  			output = {"sys_id": tid, "number": caseGr.getValue("number")};
  	}
  	return output;
  },

  getCaseById : function(id, displayValue, selectedFields){
  	this.logger.info("inside CaseDao:getCaseById id = "+id);
  	var caseObj = null;
  	if(id){
  		var caseGr = new GlideRecordSecure("sn_customerservice_case");
  		if(caseGr.get(id)){
  			caseObj = this.prepareCaseObject(caseGr, displayValue, null, selectedFields);
  			this.logger.debug("inside CaseDao:getCaseById caseObj = "+JSON.stringify(caseObj));
  		}
  	}
  	return caseObj;
  },

  getCaseBySearchQueryString : function(searchQueryStr, offset, limit, displayValue, referenceFields, selectedFields){
  	this.logger.debug("inside CaseDao:getCaseBySearchQueryString searchQueryStr = "+searchQueryStr);

  	var resultJson = {};
  	var caseObjArr = [];
  	var caseGr = new GlideRecordSecure(global.CSMBaseConstants.CASE_TABLE);
  	if(global.CSMBaseAPIUtils.isVarNotNil(searchQueryStr))
  		caseGr.addEncodedQuery(searchQueryStr);
  	caseGr.orderByDesc("number");
  	var firstRow = parseInt(offset);
  	var lastRow = firstRow+parseInt(limit);
              var query = new sn_queryrules.QueryRuleGenerator().appendEncodedQuery(global.CSMBaseConstants.CASE_TABLE, caseGr.getEncodedQuery());
              caseGr.addEncodedQuery(query);
  	caseGr.chooseWindow(firstRow, lastRow);
  	caseGr.query();
  	resultJson.totalRecords = global.CSMBaseAPIUtils.getRowsCount(global.CSMBaseConstants.CASE_TABLE,searchQueryStr);
  	while(caseGr.next()){
  		var caseObj = this.prepareCaseObject(caseGr, displayValue, referenceFields, selectedFields);
  		this.logger.debug("inside CaseDao:getCaseBySearchQueryString caseObj = " + JSON.stringify(caseObj));
  		caseObjArr.push(caseObj);
  	}
  	resultJson.cases = caseObjArr;
  	return resultJson;
  },
  
  prepareCaseObject : function(caseGr, displayValue, referenceFields, selectedFields){
  	var caseObj;
  	caseObj= global.CSMBaseAPIUtils.getCustomJSONFromGR(caseGr,displayValue,selectedFields);
  	if(referenceFields && referenceFields ==  'false')
  		return caseObj;
  	if(caseObj.consumer){
  		this.consumerHandle = new global.Consumer();
  		var consumerObj = this.consumerHandle.getConsumerInfoById(caseGr.getValue("consumer"),displayValue);
  		caseObj.consumer = consumerObj;			
  	}
  	if(caseObj.account){
  		this.accountHandle = new global.Account();
  		var accountObj = this.accountHandle.getAccountInfoById(caseGr.getValue("account"),displayValue);
  		caseObj.account = accountObj;
  	}
  	if(caseObj.contact){
  		this.contactHandle = new global.Contact();
  		var contactObj = this.contactHandle.getContactInfoById(caseGr.getValue("contact"),displayValue);
  		caseObj.contact = contactObj;			
  	}
  	if(caseObj.sn_app_cs_social_social_profile){
  		this.socialUtils = new global.SocialUtils();
  		var profileObj = this.socialUtils.getSocialProfileById(caseGr.getValue("sn_app_cs_social_social_profile"), displayValue);
  		caseObj.sn_app_cs_social_social_profile = profileObj;
  	}
  	return caseObj;
  },

  prepareSocialLogInput: function(socialPostUrl, socialProfileId, caseGr){
  	var socialLogInput = {};
  	if(socialPostUrl){
  		socialLogInput.social_url = socialPostUrl;
  	}
  	if(socialProfileId){
  		socialLogInput.social_profile = socialProfileId;
  	}
  	socialLogInput.document = caseGr.getValue("sys_id");
  	return socialLogInput;
  },

  bindSocialProfileToCase: function(socialProfileId, caseId){
  	var caseGr = new GlideRecord(global.CSMBaseConstants.CASE_TABLE);
  	caseGr.get(caseId);
  	caseGr.setValue("sn_app_cs_social_social_profile",socialProfileId);
  	var result = caseGr.update();
  },

  bindEntityToSocialProfile: function(socialHandleType, socialHandle, socialHandleUrl, socialChannel, caseGr){
  	this.socialUtils = new global.SocialUtils();
  	if(socialHandleType.toUpperCase()==="CONTACT")
  		return this.socialUtils.bindContactToSocialProfile(caseGr.getValue("contact"), socialHandle, socialHandleUrl, socialChannel);
  	if(socialHandleType.toUpperCase()==="ACCOUNT")
  		return this.socialUtils.bindAccountToSocialProfile(caseGr.getValue("account"), socialHandle, socialHandleUrl, socialChannel);
  	if(socialHandleType.toUpperCase()==="CONSUMER")
  		return this.socialUtils.bindConsumerToSocialProfile(caseGr.getValue("consumer"), socialHandle, socialHandleUrl, socialChannel);	
  },

  getReferenceListAndChoices : function(column, start, end, query, dependent_value, ref_qual_input, id, reference_field_columns){
  	return global.CSMBaseAPIUtils.getReferenceListAndChoices(global.CSMBaseConstants.CASE_TABLE,column,start,end,query, dependent_value, ref_qual_input, id, reference_field_columns);
  },
  
  getCaseActivityStream : function(id, start, end, activityType){
  	return global.CSMBaseAPIUtils.getCaseActivityStream(id, start, end, activityType);
  },
  type: 'CaseDao'
};

Sys ID

1e2981ed5312030097a2ddeeff7b1244

Offical Documentation

Official Docs: