Skip to content

Instantly share code, notes, and snippets.

@skyrpex
Forked from tstachl/ApplyMacro
Created June 1, 2017 11:03
Show Gist options
  • Save skyrpex/f86412d092e1777f429316f84b63b8e5 to your computer and use it in GitHub Desktop.
Save skyrpex/f86412d092e1777f429316f84b63b8e5 to your computer and use it in GitHub Desktop.
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" recordSetVar="macros" docType="html-5.0"
standardController="Macro__c" extensions="ApplyMacroControllerExtension" title="Select Macro" >
<apex:includeScript value=""/>
<apex:includeScript value="/support/console/30.0/integration.js"/>
<apex:includeScript value="/soap/ajax/30.0/connection.js"/>
<apex:includeScript value="/support/api/30.0/interaction.js"/>
<apex:includeScript value="/canvas/sdk/js/30.0/publisher.js"/>
<apex:stylesheet value="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<apex:form >
<apex:input value="{!searchTerm}" type="search" styleClass="form-control" html-placeholder="Search ...">
<apex:actionSupport action="{!doSearch}" event="onkeyup" immediate="false" reRender="macroList" />
</apex:input>
</apex:form>
<apex:dataList value="{!macros}" var="macro" styleClass="nav nav-pills nav-stacked" id="macroList">
<a href="/{!macro.Id}" class="macro_link">{!macro.Name}</a>
</apex:dataList>
<script type="text/javascript">
(function(document, EventTarget) {
function matches(selector, currentNode) {
var vendors = ['webkit', 'ms', 'moz'],
count = vendors.length, vendor, i;
for(i = 0; i < count; i++) {
vendor = vendors[i];
if((vendor + 'MatchesSelector') in currentNode) {
return currentNode[vendor + 'MatchesSelector'](selector);
}
}
}
function passedThrough(event, selector, stopAt) {
var currentNode = event.target;
while(true) {
if(matches(selector, currentNode)) {
return currentNode;
}
else if(currentNode != stopAt && currentNode != document.body) {
currentNode = currentNode.parentNode;
}
else {
return false;
}
}
}
EventTarget.prototype.delegateEventListener = function(eName, toFind, fn) {
this.addEventListener(eName, function(event) {
var found = passedThrough(event, toFind, event.currentTarget);
if(found) {
fn.call(found, event);
}
});
};
}(window.document, window.EventTarget || window.Element));
sforce.connection.sessionId = '{!$Api.Session_ID}';
if (!String.format) {
String.format = function(str) {
var args = Array.prototype.slice.call(arguments, 1);
return str.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
sforce.console.isInConsole() && (function() {
var MACRO_QUERY = "Select Id, Name, Description__c, QuickCode__c From Macro__c Where Id = '{0}'"
, ACTION_QUERY = "Select Id, Name, Type__c, Value__c From MacroAction__c Where Macro__c = '{0}'"
, applyMacro = function(macroId) {
return function(result) {
if (result.success && result.id.indexOf('500') === 0) {
var macro = sforce.connection.query(String.format(MACRO_QUERY, macroId)).getArray('records')[0]
, actions = sforce.connection.query(String.format(ACTION_QUERY, macroId)).getArray('records')
, ticket = new sforce.SObject('Case')
, field_updates = actions.filter(function(a) { return a.Type__c == 'Field Update'; })
, quick_reply = actions.filter(function(a) { return a.Type__c == 'Quick Reply'; })[0]
ticket.Id = result.id;
field_updates.forEach(function(action) { ticket[action.Name] = action.Value__c });
sforce.connection.update([ticket]);
sforce.console.getFocusedPrimaryTabId(function(result) {
if (result.success && result.id) {
sforce.console.refreshPrimaryTabById(result.id, true, function(result) {
if (quick_reply) {
setTimeout(function() {
Sfdc.canvas.publisher.publish({
name:"publisher.selectAction",
payload: {
actionName: "Case.Email"
}
});
Sfdc.canvas.publisher.publish({
name: "publisher.setActionInputValues",
payload: {
actionName: "Case.Email",
emailFields: {
body: {
value: quick_reply.Value__c.replace(/\n/g, "<br />")
}
}
}
});
}, 10000);
}
});
}
});
}
};
};
document.body.delegateEventListener('click', 'a.macro_link', function(e) {
e.preventDefault();
sforce.console.getFocusedPrimaryTabObjectId(applyMacro(this.href.split('/').pop()));
});
}())
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment