Name

global.SocialUtils

Description

Utility file to manage sn_app_cs_social records from different scopes.

Script

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

  createSocialChannel: function(channelName){
  	this.logger.info("inside SocialUtils:createSocialChannel channelName = "+channelName);

  	var channelGr = this.prepareChannelGlideRecord(channelName);
  	var scid = channelGr.insert();
  	this.logger.info("SocialUtils:createSocialChannel scid = "+scid);
  	return scid;
  },

  getSocialChannel: function(channelName, displayValue){
  	this.logger.info("inside SocialUtils:getSocialChannel channelName = "+channelName);

  	var channelObj = null;
  	if(channelName){
  		var channelGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_CHANNEL_TABLE);
  		if(channelGr.get(channelName)){
  			channelObj = this.prepareChannelObject(channelGr, displayValue);
  			this.logger.debug("inside SocialUtils:getSocialChannel channelObj = "+JSON.stringify(channelObj));
  		}
  	}
  	return channelObj;
  },

  isSocialChannelPresent: function(channelName){
  	this.logger.info("inside SocialUtils:isSocialChannelPresent channelName = "+channelName);

  	if(this.getSocialChannel(channelName))
  		return true;
  	return false;
  },

  prepareChannelObject: function(channelGr,displayValue){
  	var channelObj = CSMBaseAPIUtils.getJSONFromGR(channelGr, displayValue);
  	return channelObj;
  },

  prepareChannelGlideRecord: function(channelName){
  	var channelGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_CHANNEL_TABLE);
  	channelGr.initialize();
  	channelGr.name=channelName;
  	channelGr.sys_name=channelName;
  	return channelGr;
  },

  createSocialProfile: function(params){		
  	var profileGr = this.prepareProfileGlideRecord(params);
  	var result = profileGr.insert();
  	this.logger.info("inside SocialUtils:createSocialProfile result = "+result);
  	return result;
  },

  getSocialProfileById: function(id, displayVaue){
  	this.logger.info("inside SocialUtils:getSocialProfileById socialId = "+id);

  	var profileObj = null;
  	if(id){
  		var profileGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_PROFILE_TABLE);
  		if(profileGr.get(id)){
  			profileObj = this.prepareProfileObject(profileGr, displayVaue);
  			this.logger.debug("inside SocialUtils:getSocialProfile profileObj = "+JSON.stringify(profileObj));
  		}
  	}
  	return profileObj;
  },

  getSocialProfile: function(socialId, channelName){
  	this.logger.info("inside SocialUtils:getSocialProfile socialId = "+socialId);

  	var profileObj = null;
  	if(socialId && channelName){
  		var profileGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_PROFILE_TABLE);
  		profileGr.addQuery("social_id",socialId);
  		profileGr.addQuery("channel.name",channelName);
  		profileGr.query();
  		if(profileGr.next()){
  			profileObj = this.prepareProfileObject(profileGr);
  			this.logger.debug("inside SocialUtils:getSocialProfile profileObj = "+JSON.stringify(profileObj));
  		}
  	}
  	return profileObj;
  },

  updateSocialProfileOwner: function(socialId, channelName, ownerColumn, ownerValue){
  	this.logger.info("inside SocialUtils:getSocialProfileGr socialId = "+socialId);

  	var spid=null;
  	if(socialId && channelName){
  		var profileGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_PROFILE_TABLE);
  		profileGr.addQuery("social_id",socialId);
  		profileGr.addQuery("channel.name",channelName);
  		profileGr.query();
  		if(profileGr.next()){
  			//Check if the social profile is already bound to some contact/consumer/account. If profile already bound, then don't update.
  			if(!profileGr.getValue("account") && !profileGr.getValue("contact") && !profileGr.getValue("consumer")){
  				profileGr.setValue(ownerColumn,ownerValue);
  				spid = profileGr.update();	
  			}else{
  				spid = profileGr.getValue("sys_id");
  			}
  		}
  	}
  	return spid;
  },

  getSocialProfileByAccountId: function(accountId){
  	this.logger.info("inside SocialUtils:getSocialProfileByAccountId socialId = "+socialId);

  	var profileObj = null;
  	if(socialId && channelName){
  		var profileGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_PROFILE_TABLE);
  		profileGr.addQuery("account",socialId);
  		profileGr.addQuery("channel.name",channelName);
  		profileGr.query();
  		if(profileGr.next()){
  			profileObj = this.prepareProfileObject(profileGr);
  			this.logger.debug("inside SocialUtils:getSocialProfile profileObj = "+JSON.stringify(profileObj));
  		}
  	}
  	return profileObj;
  },

  prepareProfileObject: function(profileGr, displayValue){
  	var profileObj = CSMBaseAPIUtils.getJSONFromGR(profileGr, displayValue);
  	if(profileObj.account){
  		this.accountHandle = new Account();
  		profileObj.account = this.accountHandle.getAccountInfoById(profileGr.getValue('account'),displayValue);
  	}
  	if(profileObj.contact){
  		this.contactHandle = new Contact();
  		profileObj.contact = this.contactHandle.getContactInfoById(profileGr.getValue('contact'), displayValue);
  	}
  	if(profileObj.consumer){
  		this.consumerHandle = new Consumer();
  		profileObj.consumer = this.consumerHandle.getConsumerInfoById(profileGr.getValue('consumer'), displayValue);
  	}
  	if(profileObj.channel){
  		profileObj.channel = this.getSocialChannel(profileObj.channel, displayValue);
  	}
  	return profileObj;
  },

  prepareProfileGlideRecord: function(params){
  	var profileGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_PROFILE_TABLE);
  	profileGr.initialize();
  	if(params.social_handle_url)
  		profileGr.profile_url = params.social_handle_url;
  	if(params.social_channel)
  		profileGr.channel = params.social_channel;
  	if(params.social_handle)
  		profileGr.social_id = params.social_handle;
  	if(params.consumer)
  		profileGr.consumer = params.consumer;
  	if(params.account)
  		profileGr.account = params.account;
  	if(params.contact)
  		profileGr.contact = params.contact;
  	return profileGr;
  },

  bindContactToSocialProfile: function(contactSysId, socialHandle, socialHandleUrl, channelName){

  	//Get sys_id of the social profile. If not present, create a social profile.
  	var spid = this.updateSocialProfileOwner(socialHandle, channelName,"contact",contactSysId);
  	if(!spid){

  		//Get sys_id of social channel. If not present, create a social channel.
  		var scid;
  		var channelObj = this.getSocialChannel(channelName);
  		if(channelObj) scid = channelObj.sys_id;
  		if(!scid){
  			scid = this.createSocialChannel(channelName);
  		}

  		//Create social profile by using the social channel sys_id and contact's sys_id
  		var socialProfileObj = {};
  		socialProfileObj.contact = contactSysId;
  		socialProfileObj.social_handle_url = socialHandleUrl;
  		socialProfileObj.social_handle = socialHandle;
  		socialProfileObj.social_channel = scid;
  		spid = this.createSocialProfile(socialProfileObj);
  	}
  	return spid;
  },

  bindAccountToSocialProfile: function(accountSysId, socialHandle, socialHandleUrl, channelName){

  	//Get sys_id of the social profile. If not present, create a social profile.
  	var spid = this.updateSocialProfileOwner(socialHandle, channelName,"account",accountSysId);
  	if(!spid){

  		//Get sys_id of social channel. If not present, create a social channel.
  		var scid;
  		var channelObj = this.getSocialChannel(channelName);
  		if(channelObj) scid = channelObj.sys_id;
  		if(!scid){
  			scid = this.createSocialChannel(channelName);
  		}

  		//Create social profile by using the social channel sys_id and account's sys_id
  		var socialProfileObj = {};
  		socialProfileObj.account = accountSysId;
  		socialProfileObj.social_handle_url = socialHandleUrl;
  		socialProfileObj.social_handle = socialHandle;
  		socialProfileObj.social_channel = scid;
  		spid = this.createSocialProfile(socialProfileObj);
  	}
  	return spid;
  },

  bindConsumerToSocialProfile: function(consumerSysId, socialHandle, socialHandleUrl, channelName){

  	//Get sys_id of the social profile. If not present, create a social profile.
  	var spid = this.updateSocialProfileOwner(socialHandle, channelName,"consumer",consumerSysId);
  	if(!spid){

  		//Get sys_id of social channel. If not present, create a social channel.
  		var scid;
  		var channelObj = this.getSocialChannel(channelName);
  		if(channelObj) scid = channelObj.sys_id;
  		if(!scid){
  			scid = this.createSocialChannel(channelName);
  		}

  		//Create social profile by using the social channel sys_id and consumer's sys_id
  		var socialProfileObj = {};
  		socialProfileObj.consumer = consumerSysId;
  		socialProfileObj.social_handle_url = socialHandleUrl;
  		socialProfileObj.social_handle = socialHandle;
  		socialProfileObj.social_channel = scid;
  		spid = this.createSocialProfile(socialProfileObj);
  	}
  	return spid;
  },

  createSocialLog: function(params){
  	this.logger.info("inside SocialUtils:createSocialLog params = "+JSON.stringify(params));

  	var socialLogGr = this.prepareSocialLogGlideRecord(params);
  	var slid = socialLogGr.insert();
  	this.logger.info("SocialUtils:createSocialLog scid = "+slid);
  	return slid;
  },

  prepareSocialLogGlideRecord: function(params){
  	var socialLogGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_LOG_TABLE);
  	socialLogGr.initialize();
  	if(params.message){
  		socialLogGr.message=params.message;
  	}
  	if(params.social_url){
  		socialLogGr.social_url = params.social_url;
  	}
  	if(params.social_profile){
  		socialLogGr.social_profile = params.social_profile;
  	}
  	if(params.document){
  		socialLogGr.document = params.document;
  	}
  	return socialLogGr;
  },

  getSocialLogById: function(id){
  	this.logger.info("inside SocialUtils:getSocialLogById socialId = "+id);

  	var socialLogObj = null;
  	if(id){
  		var socialLogGr = new GlideRecordSecure(CSMBaseConstants.SOCIAL_LOG_TABLE);
  		if(socialLogGr.get(id)){
  			socialLogObj = this.prepareSocialLogObject(socialLogGr);
  			this.logger.debug("inside SocialUtils:getSocialLogById socialLogObj = "+JSON.stringify(socialLogObj));
  		}
  	}
  	return socialLogObj;
  },

  prepareSocialLogObject: function(socialLogGr){
  	var profileObj = CSMBaseAPIUtils.getJSONFromGR(socialLogGr);
  	if(profileObj.social_profile){
  		profileObj.social_profile = this.getSocialProfileById(profileObj.social_profile);
  	}
  	return profileObj;
  },

  type: 'SocialUtils'
};

Sys ID

23c3b9825322030097a2ddeeff7b1281

Offical Documentation

Official Docs: