forked from salesforce-misc/violet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf-cases-employee.js
153 lines (134 loc) · 5.74 KB
/
sf-cases-employee.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
/* Copyright (c) 2017-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
'use strict';
var violet = require('violet').script();
var violetClientTx = require('violet/lib/violetClientTx')(violet);
var violetTime = require('violet/lib/violetTime')(violet);
var violetCasesList = require('violet/lib/violetList')(violet, 'Cases', 'case', 'cases');
var violetSFStore = require('violet/lib/violetStoreSF')(violet);
violetSFStore.store.propOfInterest = {
'Case*': ['Id*', 'CaseNumber*', 'Contact*.Name*', /*'Contact*.Owner*.Name*',*/ 'Subject*', 'Status*', 'Priority*']
}
violet.addInputTypes({
"caseNo": "NUMBER",
"caseStatus": {
"type": "caseStatusType",
"values": ["New", "Working", "Escalated", "Closed"]
},
"casePriority": {
"type": "casePriorityType",
"values": ["Low", "Medium", "High"]
},
});
// implement login - as a function of how we deploy
const ownerAlias = 'VSinh';
// almost identical to the cases-user example with the query being on an owner instead
violet.respondTo({
expecting: ['status of my cases'],
resolve: function *(response) {
var results = yield response.load('Case*', 'Owner*.Alias*', ownerAlias);
if (results.length == 0) {
response.say('Sorry. You have no cases.');
return;
}
// iterate through results and collect states in object 'status'
var status = {};
results.forEach((c)=>{
if (!status[c.Status]) status[c.Status] = 0;
status[c.Status]++;
});
var out = 'You have ' + results.length + ' cases. Of these'
var states = Object.keys(status);
states.forEach((s,ndx)=>{
if (status[s]==1)
out += ' ' + status[s] + ' is ' + s;
else
out += ' ' + status[s] + ' are ' + s;
if (ndx == states.length-2)
out += ' and ';
else if (ndx < states.length-2)
out += ','
});
response.say(out);
}});
violetCasesList.getItemText = (ndx, results) => {
var caseObj = results[ndx];
return 'Result ' + (ndx+1) + ' is ' + caseObj.Subject + ', and has status ' + caseObj.Status + '. ';
}
violet.defineGoal({
goal: violetCasesList.interactionGoal(),
prompt: ['Would you like to hear or set the priority, change status, or add a comment along with the case number'],
respondTo: [{
expecting: ['{hear|} priority for case [[caseNo]]'],
resolve: (response) => {
var caseObj = violetCasesList.getItemFromResults(response, response.get('caseNo'));
response.say('Case ' + caseObj.Subject + ' has priority ' + caseObj.Priority);
}}, {
expecting: ['Set priority for case [[caseNo]] to [[casePriority]]'],
resolve: function *(response) {
var caseObj = violetCasesList.getItemFromResults(response, response.get('caseNo'));
yield response.update('Case*', 'CaseNumber*', caseObj.CaseNumber, {
'Priority*': response.get('casePriority')
});
response.say('Case ' + caseObj.Subject + ' has priority updated to [[casePriority]]');
}}, {
expecting: ['Change status for case [[caseNo]] to [[caseStatus]]'],
resolve: function *(response) {
var caseObj = violetCasesList.getItemFromResults(response, response.get('caseNo'));
yield response.update('Case*', 'CaseNumber*', caseObj.CaseNumber, {
Status: response.get('caseStatus')
});
response.say('Case ' + caseObj.Subject + ' has status updated to [[caseStatus]]');
}}, {
expecting: ['Add comment to case [[caseNo]] saying {{commentText]]'],
resolve: function *(response) {
var caseObj = violetCasesList.getItemFromResults(response, response.get('caseNo'));
yield response.store('CaseComment*', {
'CommentBody*': 'Text String',
'ParentId*': caseObj.Id
});
response.say('Case ' + caseObj.Subject + ' has comment added');
}}]
});
violet.respondTo({
expecting: ['what are my {open|} cases'],
resolve: function *(response) {
var results = yield response.load('Case*', 'Owner*.Alias*', ownerAlias, "Status <> 'Closed'");
response.set('CaseResults', results);
violetCasesList.respondWithItems(response, results);
}});
violet.respondTo({
expecting: ['what are my [[caseStatus]] cases', 'what cases of mine have status {set to|} [[caseStatus]]'],
resolve: function *(response) {
var results = yield response.load('Case*', 'Owner*.Alias*', ownerAlias, "Status = '" + response.get('caseStatus') + "'");
response.set('CaseResults', results);
if (results.length == 0) {
response.say('Sorry. You have no cases.');
return;
}
violetCasesList.respondWithItems(response, results);
}});
violet.respondTo({
expecting: ['what are my [[casePriority]] priority cases', 'what cases of mine have priority {set to|} [[casePriority]]'],
resolve: function *(response) {
var results = yield response.load('Case*', 'Owner*.Alias*', ownerAlias, "Priority = '" + response.get('casePriority') + "'");
response.set('CaseResults', results);
if (results.length == 0) {
response.say('Sorry. You have no cases.');
return;
}
violetCasesList.respondWithItems(response, results);
}});
violet.respondTo({
expecting: ['what cases of mine have status {set to|} [[caseStatus]] and priority {set to|} [[casePriority]]',
'what cases of mine have priority {set to|} [[casePriority]] and status {set to|} [[caseStatus]]'],
resolve: function *(response) {
var results = yield response.load('Case*', 'Owner*.Alias*', ownerAlias, "(Status = '" + response.get('caseStatus') + "' AND Priority = '" + resposne.get('casePriority') + "')");
response.set('CaseResults', results);
if (results.length == 0) {
response.say('Sorry. You have no cases.');
return;
}
violetCasesList.respondWithItems(response, results);
}});
module.exports = violet;