Send email to task assignee by field event

Hi,

We have a customized task request application in Aras and I want to build a method to send assignee an email when the task has been assigned to the person.

I want to add the method to a field event, onChange. So, when the assignee changes value, an email will be sent. See attached screenshots.

And my code:

Innovator inn = this.getInnovator();
string str_body = "";

// Get assignee
string assignee = this.getProperty("dlv_assignedto","");

//Get Url
string str_actionID = this.getProperty("id","");
string str_actionReq = "DLV_ActionReq";
string link = string.Format(@"<html><p><a href=""{2}/Default.aspx?StartItem={0}:{1}"">Click here to open the ticket</a></p></html>",str_actionReq, str_actionID, CCO.Request.Url.ToString().Substring(0,CCO.Request.Url.ToString().IndexOf("/Server/")));

// Get Mail
Item email = inn.newItem("Email Message", "get");
email.setID("11A395B11B3A4337AE24F539266E815D");
email = email.apply();

email.setProperty("subject", string.Format("The following Action Request {0} has been assigned to you",this.getProperty("item_number","")));
str_body = string.Format(@"<html><B>The following Action Request {0} has been assigned to you. <br>{1} <br>Best regards,<br>Engineering Capability Office",this.getProperty("item_number",""), link);

email.setProperty("body_html", str_body);

// Get Identity for assignee
if ( assignee != "" ) {
string str_reqby = this.getPropertyAttribute("dlv_assignedto", "keyed_name");
Item iden = this.newItem("Identity", "get");
iden.setProperty("name", str_reqby);
iden = iden.apply();
if ( iden.getItemCount() == 1) { Boolean result = this.email(email, iden); }
}

return this;

---------------------------------------------------

Any idea why it does not work?

Have tried with server events, onAfterUpdate and it works. But not working with onAfterUnlock, it will solve my problem as well.

Best Regards,

Jimmi

  • This method seems to be a server method so it wont work on field events (field events are JavaScript method). I tried using your method in onAfterUnlock and it works fine for me. Let me know if you are getting any specific error.

    Thank You

    Gopikrishnan R

  • Hi Gopkrishnan,

    Thank you for your kindly reply.

    I did not get any error message, but did not get any email with onAfterUnlock by some reason. Did you make any modification from my code above?

    BR,

    Jimmi

  • Below method should work. My changes are highlighted below

    Innovator inn = this.getInnovator();
    string str_body = "";

    // Get assignee
    Item thisItem = inn.newItem(this.getType(), "get");
    thisItem.setAttribute("select", "dlv_assignedto");
    thisItem.setID(this.getID());
    thisItem = thisItem.apply();
    string assignee = thisItem.getProperty("dlv_assignedto","");

    //Get Url
    string str_actionID = this.getProperty("id","");
    string str_actionReq = "DLV_ActionReq";
    string link = string.Format(@"<html><p><a href=""{2}/Default.aspx?StartItem={0}:{1}"">Click here to open the ticket</a></p></html>",str_actionReq, str_actionID, CCO.Request.Url.ToString().Substring(0,CCO.Request.Url.ToString().IndexOf("/Server/")));

    // Get Mail
    Item email = inn.newItem("Email Message", "get");
    email.setID("11A395B11B3A4337AE24F539266E815D");
    email = email.apply();

    email.setProperty("subject", string.Format("The following Action Request {0} has been assigned to you",this.getProperty("item_number","")));
    str_body = string.Format(@"<html><B>The following Action Request {0} has been assigned to you. <br>{1} <br>Best regards,<br>Engineering Capability Office",this.getProperty("item_number",""), link);

    email.setProperty("body_html", str_body);

    // Get Identity for assignee
    if ( assignee != "" ) {
    string str_reqby = thisItem.getPropertyAttribute("dlv_assignedto", "keyed_name");
    Item iden = this.newItem("Identity", "get");
    iden.setProperty("name", str_reqby);
    iden = iden.apply();
    if ( iden.getItemCount() == 1) { Boolean result = this.email(email, iden); }
    }

    return this;

    Thank You

    Gopikrishnan R

  • Thank you for your help! 

    The finally code:

    //return inn.newError(string.Format(str_reqby)); //Debugg code

    Innovator inn = this.getInnovator();
    string str_body = "";

    // Get assignee
    Item thisItem = inn.newItem(this.getType(), "get");
    thisItem.setAttribute("select", "dlv_assignedto");
    thisItem.setID(this.getID());
    thisItem = thisItem.apply();
    string assignee = thisItem.getProperty("dlv_assignedto","");
    //return inn.newError(string.Format(assignee));

    //Get Url
    Item thisItem2 = inn.newItem(this.getType(), "get");
    thisItem2.setAttribute("select", "item_number");
    thisItem2.setID(this.getID());
    thisItem2 = thisItem2.apply();
    string str_actionID = thisItem2.getProperty("item_number","");
    string str_actionReq = "DLV_ActionReq";
    string link = string.Format(@"<html><p><a href=""{2}/Default.aspx?StartItem={0}:{1}"">Click here to open the ticket</a></p></html>",str_actionReq, str_actionID, CCO.Request.Url.ToString().Substring(0,CCO.Request.Url.ToString().IndexOf("/Server/")));

    // Get Mail
    Item email = inn.newItem("Email Message", "get");
    email.setID("1241FA3032B3483786BC8DDD0DDF7394"); //DLV_ActionReq_Assignment
    email = email.apply();

    email.setProperty("subject", string.Format("The following Action Request {0} has been assigned to you",str_actionID));
    str_body = string.Format(@"<html><B>The following Action Request {0} has been assigned to you. <br>{1} <br>Best regards,<br>Engineering Capability Office",str_actionID, link);

    email.setProperty("body_html", str_body);

    // Get Identity for assignee
    if ( assignee != "" ) {
    Item userItem = this.newItem("User","get");
    userItem.setID(assignee);
    userItem.setAttribute("select","first_name,last_name");
    userItem = userItem.apply();
    string username = (userItem.getProperty("first_name") + " " + userItem.getProperty("last_name"));
    //return inn.newError(string.Format(username));
    Item iden = this.newItem("Identity", "get");
    iden.setProperty("name", username);
    iden = iden.apply();
    if ( iden.getItemCount() == 1) { Boolean result = this.email(email, iden); }
    }

    return this;

  • Hi Jimmi

    I have few suggestions for you.

    1. Based on your method, it seems you are doing apply action for 5 times but it can be reduced to 3 server calls. lesser the server call, more the application performance. 

    2. It is good practice to use the user ID to get the identity than using the user's first name and last name. If first name and last name is modified this method won't work. 

    3. Since on after unlock event is triggered on every unlock, the mail will trigger two times (assuming DLV is versionable) when you edit an existing item. 

    Example : I have one DLV item with generation 1. If I edit then it will change to generation 2. So, when I click Unlock, this will unlock generation 1 and generation 2. So method will trigger two time (one for old dlv assigned to another for new dlv assigned to).

    This wont be a problem if DLV is not versionable and always have one generation

    Based on above suggestions, Modified code is below

    Innovator inn = this.getInnovator();
    string str_body = "";

    // Get existing assignee
    string existingassignee = this.getProperty("dlv_assignedto","");

    // Get assignee
    Item thisItem = inn.newItem(this.getType(), "get");
    thisItem.setAttribute("select", "dlv_assignedto,item_number");
    thisItem.setID(this.getID());
    thisItem = thisItem.apply();
    string assignee = thisItem.getProperty("dlv_assignedto","");

    if(existingassignee == assignee)
    {
    return this;
    }

    //Get Url
    string str_actionID = thisItem.getProperty("item_number","");
    string str_actionReq = "DLV_ActionReq";
    string link = string.Format(@"<html><p><a href=""{2}/Default.aspx?StartItem={0}:{1}"">Click here to open the ticket</a></p></html>",str_actionReq, str_actionID, CCO.Request.Url.ToString().Substring(0,CCO.Request.Url.ToString().IndexOf("/Server/")));

    // Get Mail
    Item email = inn.newItem("Email Message", "get");
    email.setID("35DF32E58F02491B99B0450DA70AF85E"); //DLV_ActionReq_Assignment
    email = email.apply();

    email.setProperty("subject", string.Format("The following Action Request {0} has been assigned to you",str_actionID));
    str_body = string.Format(@"<html><B>The following Action Request {0} has been assigned to you. <br>{1} <br>Best regards,<br>Engineering Capability Office",str_actionID, link);

    email.setProperty("body_html", str_body);

    // Get Identity for assignee
    if ( assignee != "" )
    {
    Item userItem = inn.newItem("User", "get");
    userItem.setAttribute("select", "id");
    Item aliasItem = inn.newItem("Alias", "get");
    aliasItem.setAttribute("select", "related_id");
    Item identity = inn.newItem("Identity", "get");
    aliasItem.setPropertyItem("related_id", identity);
    userItem.addRelationship(aliasItem);
    userItem.setID(assignee);
    userItem = userItem.apply();
    if (userItem.isError())
    {
    return userItem;
    }
    aliasItem = userItem.getRelationships("Alias");
    Item iden = aliasItem.getItemByIndex(0).getRelatedItem();
    if (iden.getItemCount() == 1)
    {
    Boolean result = this.email(email, iden);
    }
    }
    return this;

    Thank You

    Gopikrishnan R

  • Hi Gopikrishnan,

    The following condition does not work, since existingassignee returns empty. 

    if(existingassignee == assignee)
    {
    return this;
    }

    Any other idea how to check if assignee changes or not? Right now, an email will be sent OnAfterUnlock, even though the assignee is not changed.

    BR,

    Jimmi

  • Hi Yanjim

    I would propose some other solution than the one on After Unlock event. 

    Follow below steps

    1. Create new JavaScript method (name : ARAS_Community_JS)and add below code 

    Update Server Method Name below ARAS_Community_Server

    var dlvAssignedTo = document.thisItem.getProperty('dlv_assignedto');
    if (dlvAssignedTo)
    {
    if (!top.aras.confirm('Do you want to send mail to DLV Assigned to User ?'))
    {
    return;
    }
    var callServerMethod = aras.IomInnovator.newItem('Method', 'ARAS_Community_Server');
    callServerMethod.setProperty('dlv_assignedto', dlvAssignedTo);
    callServerMethod.setProperty('item_number', document.thisItem.getProperty('item_number'));
    var result = callServerMethod.apply();
    if (result.isError())
    {
    aras.AlertError(result.getErrorString());
    }
    else
    {
    aras.AlertSuccess('Email sent successfully.');
    }
    }

    2. Update your existing server method with below code

    Innovator inn = this.getInnovator();
    string str_body = "";

    // Get assignee
    string assignee = this.getProperty("dlv_assignedto","");

    //Get Url
    string str_actionID = this.getProperty("item_number","");
    string str_actionReq = "DLV_ActionReq";
    string link = string.Format(@"<html><p><a href=""{2}/Default.aspx?StartItem={0}:{1}"">Click here to open the ticket</a></p></html>",str_actionReq, str_actionID, CCO.Request.Url.ToString().Substring(0,CCO.Request.Url.ToString().IndexOf("/Server/")));

    // Get Mail
    Item email = inn.newItem("Email Message", "get");
    email.setID("35DF32E58F02491B99B0450DA70AF85E"); //DLV_ActionReq_Assignment
    email = email.apply();

    email.setProperty("subject", string.Format("The following Action Request {0} has been assigned to you",str_actionID));
    str_body = string.Format(@"<html><B>The following Action Request {0} has been assigned to you. <br>{1} <br>Best regards,<br>Engineering Capability Office",str_actionID, link);

    email.setProperty("body_html", str_body);

    // Get Identity for assignee
    if ( assignee != "" )
    {
    Item userItem = inn.newItem("User", "get");
    userItem.setAttribute("select", "id");
    Item aliasItem = inn.newItem("Alias", "get");
    aliasItem.setAttribute("select", "related_id");
    Item identity = inn.newItem("Identity", "get");
    aliasItem.setPropertyItem("related_id", identity);
    userItem.addRelationship(aliasItem);
    userItem.setID(assignee);
    userItem = userItem.apply();
    if (userItem.isError())
    {
    return userItem;
    }
    aliasItem = userItem.getRelationships("Alias");
    Item iden = aliasItem.getItemByIndex(0).getRelatedItem();
    if (iden.getItemCount() == 1)
    {
    Boolean result = this.email(email, iden);
    }
    }
    return this;

    3. Open form where this dlv_assignedto property exist

    4. Add Form field event on dlv_assignedto property (Method Name : ARAS_Community_JS and event : On Change)

    5. Save and Unlock Form

    6. Try testing.

    Expected Result:

    Whenever the dlv_assigned_to field is changed, it will ask for confirmation from user whether to send email to user, if user select Ok, then it will send email else it will not send email

  • Hi Gopikrishnan,

    Thank you for your engagement and time! Appreciate it, really!

    The code works with following small issues:

    1. The confirm dialog appears twice each time dlv_assigned_to field is changed, does it need a delay somehow?

    2. When the dlv_assigned_to changes by search dialog (... button), the confirm dialog will not appear. Bug?

    BR,

    Jimmi

  • Hi Jimmi,

    I'm not sure a field event is the best option for this particular use case. The changes that are made within the form are only suggestions until the user clicks the save button to write those changes to the database. The onChange field event triggers when the actual field is filled in and not when the property is changed at a database level. In a situation where a user accidentally clicks on the wrong user when filling in the field and then corrects their mistake before clicking save, the wrong user will still get a notification that a task was assigned to them even though it isn't.

    For that reason, this use case is likely best handled server-side. If there is a Workflow or Life Cycle attached to this item, you could configure those to send an email without needing to write any extra code. If there's not, this is probably best done in an onBeforeUpdate server event where you can query for the old value of the div_assignedto property to check if it's being changed, and then send the email if it is.

    Chris


    Christopher Gillis

    Aras Labs Software Engineer

  • Hi Chris,

    Agree! But I couldn't get the old value of div_assignedto to work, can you help me with that? See my reply 2020-06-12, 5 pm

    /Jimmi