-
Notifications
You must be signed in to change notification settings - Fork 2
/
contentscript.js
191 lines (180 loc) · 7.72 KB
/
contentscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var sessionID = document.cookie.match('sid=([^;]*)')[1];
sforce.connection.sessionId = sessionID;
/* Create variable with messages called from the manifest.json script */
var promptMessage = chrome.i18n.getMessage("errorMsg");
var whatCase = chrome.i18n.getMessage("whatCase");
caseQuery();
/* Start search function */
function caseQuery(){
/* Create variable and set to false, used later to throw error if need be */
var blockSearch = false;
/* Check SF session cookie in try catch, change to true if session cookie is matched */
try {
this.__sfdcSessionId = document.cookie.match(/(^|;\s*)sid=(.+?);/)[2];
} catch (e) {
blockSearch = true;
}
var resultTrim = prompt(whatCase.trim());
var result = resultTrim.replace(/\'+/g,"\\'");
if(blockSearch) {
alert("You must log into Salesforce prior to running the case search.");
} else {
if(result == null) {
return false;
} else if(result.indexOf("--") != -1) {
if(result.indexOf(" --") != -1){
var result = result.split(" --");
} else if(result.indexOf("--")){
var result = result.split("--");
}
if(result[1].toUpperCase() == "EDIT"){
if(result.indexOf(":ref") == -1) {
caseNumSearch("EDIT", result[0]);
} else if(result.indexOf(":ref") != -1) {
refSearch("EDIT", result[0]);
} else {
alert("'" + result + "'" + promptMessage);
}
}
if(result[1].toUpperCase() == "CLOSE"){
if(result.indexOf(":ref") == -1) {
caseNumSearch("CLOSE", result[0]);
} else if(result.indexOf(":ref") != -1) {
refSearch("CLOSE", result[0]);
} else {
alert("'" + result + "'" + promptMessage);
}
}
if(result[1].toUpperCase() == "ASSIGN"){
if(result.indexOf(":ref") == -1) {
caseNumSearch("ASSIGN", result[0]);
} else if(result.indexOf(":ref") != -1) {
refSearch("ASSIGN", result[0]);
} else {
alert("'" + result + "'" + promptMessage);
}
}
if(result[1].toUpperCase() == "A/C"){
if(result.indexOf(":ref") == -1) {
caseNumSearch("A/C", result[0]);
} else if(result.indexOf(":ref") != -1) {
refSearch("A/C", result[0]);
} else {
alert("'" + result + "'" + promptMessage);
}
}
} else if(result.indexOf(":ref") == -1) {
caseNumSearch(undefined, result);
} else if(result.indexOf(":ref") != -1) {
refSearch(undefined, result);
} else {
alert("'" + result + "'" + promptMessage);
}
}
}
function caseNumSearch(type, result){
var caseQuery = sforce.connection.query("SELECT Id, Subject, CaseNumber FROM Case WHERE CaseNumber = '" + result + "' ORDER BY LastModifiedDate DESC LIMIT 10");
var caseId = caseQuery.getArray("records");
var id = caseId[0].Id.substring(0,15);
if (caseQuery.size == 1) {
if(type == undefined){
window.location = URL = "https://" + window.location.host + "/" + id;
} else if(type == "EDIT"){
window.location = URL = "https://" + window.location.host + "/" + id + "/e?retURL=%2F" + id;
} else if(type == "CLOSE"){
window.location = URL = "https://" + window.location.host + "/" + id + "/s?retURL=%2F" + id;
} else if(type == "ASSIGN"){
assign(result);
} else if(type == "A/C"){
assignAndClose(result);
}
} else {
alert("'" + result + "'" + promptMessage);
}
}
function refSearch(type, result){
var id;
if(result.indexOf("500") != -1){
var search = result.indexOf("500");
var queryId = result.substring(search);
id = queryId.substring(0,10).split("C").join("C00000");
} else if (!isNaN(result)) {
var caseQuery = sforce.connection.query("SELECT Id, Subject, CaseNumber FROM Case WHERE Subject = '" + result + "' OR CaseNumber = '" + result + "' ORDER BY LastModifiedDate DESC LIMIT 10");
var caseId = caseQuery.getArray("records");
if(caseQuery.size == 1){
id = caseId[0].Id.substring(0,15);
} else if (caseQuery > 1){
var num = "";
for(var i = 0; i < caseQuery.size; i++) {
num += (i+1) + ") " + caseId[i].CaseNumber + " " + caseId[i].Subject.substring(0,20) + "\r\n";
}
var idSelect = prompt("Please enter the number from the list below, corresponding to your case number, if your case has not been found then please try again:\r\n" + num);
var idFinal = idSelect - 1;
id = caseId[idFinal].Id.substring(0,15);
} else {
alert("'" + result + "'" + promptMessage);
}
} else {
alert("'" + result + "'" + promptMessage);
}
if(type == undefined){
window.location = URL = "https://" + window.location.host + "/" + id;
} else if(type == "EDIT"){
window.location = URL = "https://" + window.location.host + "/" + id + "/e?retURL=%2F" + id;
} else if(type == "CLOSE"){
window.location = URL = "https://" + window.location.host + "/" + id + "/s?retURL=%2F" + id;
} else if(type == "ASSIGN"){
assign(id);
} else if(type == "A/C"){
assignAndClose(id);
}
}
function assign(id){
var newOwner = sforce.connection.getUserInfo().userId;
var sfcase = new sforce.SObject("case");
sfcase.id = id;
sfcase.OwnerId = newOwner;
sforce.connection.update([sfcase]);
var result = sforce.connection.update([sfcase]);
if(result == undefined){
alert("Please click okay to continue");
window.location = "https://" + window.location.host + "/" + id;
} else {
alert("Warning an error occurred:\r\n" + result[0].errors.statusCode + "\r\n"
+ "The following error was encountered:\r\n" + result[0].errors.message + "\r\n"
+ "The error was found on the following field:\r\n" + result[0].errors.fields);
}
}
function assignAndClose(id){
var newOwner = sforce.connection.getUserInfo().userId;
var sfcase = new sforce.SObject("case");
sfcase.id = id;
sfcase.Status = "Closed";
sfcase.OwnerId = newOwner;
sforce.connection.update([sfcase]);
var result = sforce.connection.update([sfcase]);
if(result == undefined){
alert("Your case was update.");
window.location = "https://" + window.location.host + "/" + id;
} else {
alert("Warning an error occurred:\r\n" + result[0].errors.statusCode + "\r\n"
+ "The following error was encountered:\r\n" + result[0].errors.message + "\r\n"
+ "The error was found on the following field:\r\n" + result[0].errors.fields);
}
}
function close(id){
var newOwner = sforce.connection.getUserInfo().userId;
var sfcase = new sforce.SObject("case");
sfcase.id = id;
sfcase.Status = "Closed"
sforce.connection.update([sfcase]);
var result = sforce.connection.update([sfcase]);
if(result == undefined){
alert("Your case was update.");
window.location = "https://" + window.location.host + "/" + id;
} else {
alert("Warning an error occurred:\r\n" + result[0].errors.statusCode + "\r\n"
+ "The following error was encountered:\r\n" + result[0].errors.message + "\r\n"
+ "The error was found on the following field:\r\n" + result[0].errors.fields);
}
}