Portal Tip - Adding an email link for the affected user to the incident form

john_doylejohn_doyle Cireson Support Ninja IT Monkey ✭✭✭✭
Here's some code that you can add to the custom.js file in the CustomSpace folder. It will add a link to the form if the affected user has an email address which will allow an analyst to send the affected user an email using their desktop email client (e.g. Outlook). Don't forget to add the email address of the mailbox monitored by the SCSM Server to the script.

app.custom.formTasks.add('Incident', null, function (formObj, viewModel) { 
formObj.boundReady(function () {
$("label[for='RequestedWorkItem']").parent().append("<span id='affectedUserEmail' class='help-block'></span>");
var fnLookUpUser = function(lookupUserId){
$.get("/api/V3/User/GetUserRelatedInfoByUserId", { userId: lookupUserId },
function (data) {
if (data.length > 0)
{
var obj = jQuery.parseJSON( data);
var strEmailLink = "";
if (typeof obj.Email != "undefined" && obj.Email.length > 0)
{
var emailTo = obj.Email;
var irTitle = (pageForm.viewModel.Title) ? pageForm.viewModel.Title : "";
var emailSubject = "[" + pageForm.viewModel.Id + "] " + irTitle;
var emailCC = "[email protected]";
strEmailLink = "<a href='mailto:" + emailTo + "?cc=" + emailCC + "&subject=" + emailSubject + "'>" + emailTo + "</a>";
$("#affectedUserEmail").html(strEmailLink);
}
else
$("#affectedUserEmail").html("");
}
else
$("#affectedUserEmail").html("");
},"json");
}
if (pageForm.viewModel.RequestedWorkItem.BaseId)
fnLookUpUser(pageForm.viewModel.RequestedWorkItem.BaseId);
boundObj["RequestedWorkItem"].bind( "change",
function( objEvent, objData ){
var affectedUserId = (pageForm.viewModel.RequestedWorkItem.BaseId) ? pageForm.viewModel.RequestedWorkItem.BaseId : "";
fnLookUpUser(affectedUserId);
});
});
});


Tagged:

Comments

  • Xiaoqian_LiXiaoqian_Li Customer IT Monkey ✭
    Nice code. Is there a way to populate Description value into message body? I tried "var irDesc = (pageForm.viewModel.Description) ? pageForm.viewModel.Description : "";" but received "undefined" in the message body.
  • john_doylejohn_doyle Cireson Support Ninja IT Monkey ✭✭✭✭
    I added the description to the body, and added some code to encode special characters. I decided also to only add the title and description when the link is clicked as these fields could have been edited.

    app.custom.formTasks.add('Incident', null, function (formObj, viewModel) { 
    formObj.boundReady(function () {
    $("label[for='RequestedWorkItem']").parent().append("<span id='affectedUserEmail' class='help-block'></span>");
    var fnLookUpUser = function(){
    var lookupUserId = (pageForm.viewModel.RequestedWorkItem.BaseId) ? pageForm.viewModel.RequestedWorkItem.BaseId : "";
    $.get("/api/V3/User/GetUserRelatedInfoByUserId", { userId: lookupUserId },
    function (data) {
    if (data.length > 0)
    {
    var obj = jQuery.parseJSON( data);
    var strEmailLink = "";
    if (typeof obj.Email != "undefined" && obj.Email.length > 0)
    {
    var emailTo = obj.Email;
    var emailCC = "[email protected]";
    strEmailLink = "<a href='mailto:" + emailTo + "?cc=" + emailCC + "'>" + emailTo + "</a>";
    $("#affectedUserEmail").html(strEmailLink);
    }
    else
    $("#affectedUserEmail").html("");
    }
    else
    $("#affectedUserEmail").html("");
    },"json");
    }
    if (pageForm.viewModel.RequestedWorkItem.BaseId)
    fnLookUpUser(pageForm.viewModel.RequestedWorkItem.BaseId);
    boundObj["RequestedWorkItem"].bind("change",
    function(){
    fnLookUpUser();
    });
    $("label[for='RequestedWorkItem']").parent().on("click", "a", function(e)
    {
    var mailLink = e.target.href;
    var irTitle = (pageForm.viewModel.Title) ? encodeURIComponent(pageForm.viewModel.Title) : "";
    var emailSubject = "[" + pageForm.viewModel.Id + "] " + irTitle;
    var irDesc = (pageForm.viewModel.Description) ? encodeURIComponent(pageForm.viewModel.Description) : "";
    e.currentTarget.href = mailLink + "&subject=" + emailSubject + "&body=" + irDesc;
    console.log(e.currentTarget.href);
    fnLookUpUser();
    }
    )
    });
    });



  • Xiaoqian_LiXiaoqian_Li Customer IT Monkey ✭

    Thank you so much! This is exactly what I was looking for.

  • Brad_McKennaBrad_McKenna Customer Advanced IT Monkey ✭✭✭

    Old post, however wanted to say Thank You @John_doyle for providing this to the committee.

    This is great way to help give a personal feel/touch to email compared to the Send Email task.

  • Eugene_RackEugene_Rack Customer IT Monkey ✭
    Good morning
    Thanx for the code, very useful. I wonder if you could help with the following regarding the publishing of the email address. We are busy migrating from Lotus notes email to Outlook. As we have not fully migrated all users / analysts when a user clicks the link it will try to load the local mail client and in this case it would be outlook. If they are still using Lotus notes it will try to go through the process of installing outlook. I was hoping there is a way to hide the email address from users and only analysts will have the option to see the email address. I know it is a long shot but thought I would ask

    Kind regards
    Eugene
  • Adam_DzyackyAdam_Dzyacky Customer Contributor Monkey ✭✭✭✭✭
    I updated this to a Task available to analysts on IR, SR, CR, and PR. Currently lacking some elegance, but it gets the job done. In the spirit of trying to standardize customizations and CustomSpace's folder structure (like @martin_blomgren), this contains the single line to add to your custom.js file and its own directory to drop within CustomSpace.

    Just replace "SCSMEMAIL" with your service manager email account.
  • Adrian_MataiszAdrian_Mataisz Customer Advanced IT Monkey ✭✭✭
    Can this be added to the Uploads section?

    How can I add a link to the ticket in the body of the email?
  • Joe_BurrowsJoe_Burrows Cireson Devops Super IT Monkey ✭✭✭✭✭
    Can this be added to the Uploads section?
    Have moved this now.
  • Adam_DzyackyAdam_Dzyacky Customer Contributor Monkey ✭✭✭✭✭
    edited March 2017
    Currently the email is generated the classic MAILTO: method, so the current code looks like -
    window.location.href = "mailto:" + emailTo + "?cc=" + emailCC + "&subject=" + emailSubject;

    And simply would have to be updated to -
    window.location.href = "mailto:" + emailTo + "?cc=" + emailCC + "&subject=" + emailSubject + "&body=" + emailBody;

    Where "emailBody" is a variable declared before this in each Work Item section to the effect of -
    emailBody = "https://portalName.domain.tld/Incident/edit/" + pageForm.viewModel.Id

    However if I'm not mistaken the mailto: method in HTML enforces plaintext preventing a short link from being generated. As a result, if you take the steps above you'll just end up with the entire URL in the body of the email. But if that isn't a showstopper, than awesome!
  • Eugene_RackEugene_Rack Customer IT Monkey ✭
    Thanx for the new code. I did it another way by adding an initial user.session check if analyst, and if so to call a .js file. Worked fine for me. Also did the url option as above however as you stated the mailto: is plain text and not html so the url is in the long format. Not a big issue though. One thing I did find is if you to the mailto: and add a standard text for the Body, the analysts normal signature is not loaded. If you leave the body empty then the on using mailto the signature is added. Not sure if anyone else experienced this. 
  • Adam_DzyackyAdam_Dzyacky Customer Contributor Monkey ✭✭✭✭✭
    edited March 2017
    Second there Eugene. Using the "body" portion of the mailto: syntax replaces anything in the body of the Outlook email (i.e. the user's signature).
  • john_doylejohn_doyle Cireson Support Ninja IT Monkey ✭✭✭✭
    The one reservation I have about the code you wrote, Adam, is that if the form is new or the affected user has been changed but not yet saved, the email address sent to Outlook will be incorrect. That is why I made the call to the API in the original code as the only piece of info that you can rely on in the viewModel is the BaseId.

    Given that the affected user rarely changes and the form will probably be saved before they choose to use the task, it is probably not an issue anyway.
  • Nicola_SchubertNicola_Schubert Customer IT Monkey ✭

    Hi Guys.

    Thanks for this great code John. We have a few email addresses with an apostrophe in the surname. The mailto breaks in these scenarios. Any tips to fix this?

    Thanks

    Nicola

  • john_doylejohn_doyle Cireson Support Ninja IT Monkey ✭✭✭✭
    Hi Nicola,
    Try changing the line
    var emailTo = obj.Email;
    to:
    var emailTo = encodeURIComponent(obj.Email);
    That should escape any special characters in the email addresses.
  • Nicola_SchubertNicola_Schubert Customer IT Monkey ✭
    Hi John, I still get the same result with this change. :(
  • john_doylejohn_doyle Cireson Support Ninja IT Monkey ✭✭✭✭
    Hi Nicola,
    Well, it seems that encodeURIComponent does not escape single quotes.
    Try this instead:
    var emailTo = obj.Email.replace(/'/g, "%27");


  • Nicola_SchubertNicola_Schubert Customer IT Monkey ✭
    Thanks for your help John :)
  • Brad_McKennaBrad_McKenna Customer Advanced IT Monkey ✭✭✭
    Attached is a small update, thought I share to show a small change we made that resolved an internal issue in targeting of the 'onclick' event. Changing the target to the specific span id, resolved our issue where we had two hyperlinks tied to the RequestedWorkItem label, and this general target was being invoked when we executed a different hyperlink.

    The actual change can be seen here:
    Removed/CommentedOut:
    //$("label[for='RequestedWorkItem']").parent().on("click", "a", function (e) {
    Added:
    $("#affectedUserEmail").on("click", "a", function (e) {
Sign In or Register to comment.