Monday, October 8, 2018

Make a new team the primary team with confirmation alert

Where I work we use Tasks to pass around work from one department to the next.  So a Lead might come in and a Task on that Lead is assigned to the Sales team.  Once the Sales team finishes whatever they need to do they assign the Task to the Legal team.  The Legal team then passes it to the Order Management team and so on.

One of the issues with this was that we couldnt preadd the teams as tasks take their own path sometimes and it muddied reports when we did that.  So each team needed to add the next team to the Task and mark it as Primary. 

Well, as with most manual workflows, it didn't always get marked as primary.  So I came up with the code below to ask the user, after they add a team, if they want to make it the primary team and if they say yes, then mark it as primary.  It took forever to figure out how to tell if a team had been added, and I dont really like this solution but it worked the best so I went with it.

So first the code, it just goes in the record.js of whatever module you want to add this to.

({
extendsFrom: 'RecordView',
initialize: function (options) {
this._super('initialize', [options]);
},
_render: function () {
this._super('_render');
this.model.on('change:team_name', this.onChangeTeamName, this);
},
onChangeTeamName: function () {
var teamField = this.getField('team_name');
var teams = teamField.value;
var teamIDs = [];
var teamLock = sessionStorage.getItem('s2s_teamLock');
if (teamLock == 1) {
return null;
}
//Only run this on an edit view
if (this.action != 'edit') {
sessionStorage.removeItem('s2s_teamLock');
return null;
}
for (i = 0; i < teams.length; i++) {
if (!_.isEmpty(teams[i]['id']) && !_.isUndefined(teams[i]['id'])) {
teamIDs[i] = teams[i]['id'];
} else {
//If we get here then the user is adding a team
sessionStorage.setItem('s2s_teamArray', JSON.stringify(teamIDs));
return null;
}
}
//if there are no teams then just return
if (teamIDs.length == 0) {
return null;
}
var prevTeams = sessionStorage.getItem('s2s_teamArray');
if (_.isEmpty(prevTeams) || _.isUndefined(prevTeams)) {
sessionStorage.setItem('s2s_teamArray', JSON.stringify(teamIDs));
prevTeams = teamIDs;
} else {
prevTeams = JSON.parse(prevTeams);
}
var arrayDiff = this.arrayDiff(teamIDs, prevTeams);
if (arrayDiff.length > 0) {
var teamIndex = teamIDs.indexOf(arrayDiff[0]);
if (!_.isUndefined(teams[teamIndex])) {
var teamDisplayName = teams[teamIndex]['name'];
var message = app.lang.get('MSG_PRIMARY_TEAM001', this.module, {
teamName: teamDisplayName
});
app.alert.show('message-id', {
level: 'confirmation',
messages: message,
autoClose: false,
confirm: {
label: 'Yes'
},
cancel: {
label: 'No'
},
onConfirm: function () {
sessionStorage.setItem('s2s_teamLock', '1');
teamField.setPrimary(teamIndex);
sessionStorage.removeItem('s2s_teamLock');
sessionStorage.removeItem('s2s_teamArray');
}
,
onCancel: function () {
sessionStorage.removeItem('s2s_teamLock');
sessionStorage.removeItem('s2s_teamArray');
}
});
}
}
return true;
},
arrayDiff: function (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
}
});
view raw record.js hosted with ❤ by GitHub


Then add a line to the language file custom/include/language/en_us.lang.php that reads.  Notice how I am filling in the 'TeamName' variable with the app.lang.get() code in the record.js file.

$app_strings['MSG_PRIMARY_TEAM001'] = "Set \"{{teamName}}\" to be the primary team?";
view raw en_us.lang.php hosted with ❤ by GitHub


With this code in place (after a QR&R of course) when a new team is added, the system will ask the use if they want that team marked as primary.  If they say yes then it will be.

No comments:

Post a Comment