BUY

CommuniGate Pro Business Application Integrations

Integration of CommuniGate Pro Contact Center with the "SalesForce".

// ================================================== //
//   Contact Center integration with SalesForce CRM   //
//                   Version 2.0.1b                   //
// ================================================== //

function  consultCRM(finalAttempt)          forward;
function  loginCRM()                        forward;
function  getUserCRM(session,qParam)        forward;

var monitorSettings;

function ccSalesForceCRM() {
	return consultCRM(false);
}

///////////////

function consultCRM(finalAttempt) {
	var qParam = EmailUserPart(SIPURItoEmail(RemoteURI())) || "";
	var session = GetAccountPreferences("~" + MyEmail() + "/CRMSession");
	var result;
	if(!IsDictionary(session)) {
		session = loginCRM();
		if(IsDictionary(session) && IsString(session.token_type) && IsString(session.access_token)) {
			ExecuteCLI("UpdateAccountPrefs " + MyEmail() + " {CRMSession=" + session.objectToString() + ";}");
		}
	}
	
	if(!IsDictionary(session)) {return null;}
	
	// we have a dictionary 'session' value
	result = getUserCRM(session,qParam);
	if(IsArray(result)) {
		return result;
	} else {
		if(finalAttempt) {return null;}
		else {
			if(ExecuteCLI("UpdateAccountPrefs " + MyEmail() + " {CRMSession=#NULL#;}") != null) {return null;}
			return consultCRM(true);
		}
	}
}

///////////////

function loginCRM() {
  var username = "user@domain.com";
  var password = "pass";
  var security_token = "user_security_token";
  var consumer_key = "XXXXXXXXXXXXXXXXXXXXX";
  var consumer_secret = "XXXXXXXXXXXXXXXXXX";
	var url = "https://login.salesforce.com/services/oauth2/token";
	var params                    = NewDictionary();
	params.method                 = "POST";
	params.timeout                = 2;
	params.body                   = NewDictionary();
	params.body.grant_type        = "password";
	params.body.client_id         = consumer_key;
	params.body.client_secret     = consumer_secret;
	params.body.username          = username;
	params.body.password          = password + security_token;
	var result = HTTPCall(url,params);
	if(IsDictionary(result) && result.responseCode < 400 && IsData(result.body)) {
                var resultJSON;
                if(IsString(result.charset)) {
                        resultJSON = Convert(result.body,result.charset);
                } else {
                        resultJSON = result.body;
                }
		var data = JSONToObject(resultJSON);
		if(IsDictionary(data) && IsString(data.access_token)) {
			return data;
		}
	}
	return null;
}

///////////////

function getUserCRM(session,qParam) {
        if(!IsDictionary(session) || !IsString(session.token_type) || !IsString(session.access_token)) {return null;}
	var max_results = 3;
        var apiVersion = "v20.0";
	var url = session.instance_url;
	if(!IsString(url)) {return null;}
        url += "/services/data/" + apiVersion + "/search/";
	var params                    = NewDictionary();
	params.method                 = "GET";
        params.urlParams              = NewDictionary();
        params.urlParams.q            = "FIND {" + qParam + "} IN PHONE FIELDS RETURNING Contact " +
                                        "(Id, Name, Email) LIMIT " + String(max_results);
	params.timeout                = 2;
        params.supplFields            = NewDictionary();
        params.supplFields.Authorization = session.token_type + " " + session.access_token;
	var result = HTTPCall(url,params);
	if(IsDictionary(result) && result.responseCode < 400 && IsData(result.body)) {
                var resultJSON;
                if(IsString(result.charset)) {
                        resultJSON = Convert(result.body,result.charset);
                } else {
                        resultJSON = result.body;
                }
		var data = JSONToObject(resultJSON);
		if(IsArray(data)) {
			var crm = NewArray();
			for(var i = 0; i < Length(data); i += 1) {
				var obj = NewDictionary();
                                obj.id = session.instance_url + "/" + data[i].Id;
                                obj.name = data[i].Name;
                                obj.email = data[i].Email;
				AddElement(crm, obj);
			}
			return crm;
		}
	}
	return null;
}

To get more info about CommuniGate Contact Center write to support@communigate.com