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

Parents
  • 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

  • I think the issue was the type of server method being used previously. onAfterUnlock events aren't typically used, and I think by the time that event would be called, the value at a database level will have already been changed.

    An onBeforeUpdate event should contain the old value which you can get from the database and the new value (if one exists) within the context item of this. By comparing these two values, you should be able to determine if an email is necessary.

    This code I copied from above should work fine in a different event type:

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

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

    if((newAssignee != "") && (newAssignee == existingAssignee))
    {
    return this;
    }

    I've switched the variable names around for better clarity. Since this will contain the proposed changes before they are sent to the database, we set that to newAssignee, and then we query for the item as it exists in the database before the change to find the existing value. I've also added in an additional check in bold to see if there is even a change being made to the div_assignedto property at all.

    Lastly, I'd recommend seeing if a Workflow or Life Cycle could be added to your use case instead of custom code like this. I could see a simple life cycle like the one below being added to this ItemType:

    Proposed->In Work->Completed

    A task request comes in and starts in the Proposed state. It could be looked at and reviewed in this state, and then assigned to a specific person before being voted into the In Work state. At that point, the person assigned to the task via the div_assignedto property is emailed. This kind of notification can be configured directly on the Life Cycle Map, so you won't need to write any custom code at all. 

    Chris

Reply
  • I think the issue was the type of server method being used previously. onAfterUnlock events aren't typically used, and I think by the time that event would be called, the value at a database level will have already been changed.

    An onBeforeUpdate event should contain the old value which you can get from the database and the new value (if one exists) within the context item of this. By comparing these two values, you should be able to determine if an email is necessary.

    This code I copied from above should work fine in a different event type:

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

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

    if((newAssignee != "") && (newAssignee == existingAssignee))
    {
    return this;
    }

    I've switched the variable names around for better clarity. Since this will contain the proposed changes before they are sent to the database, we set that to newAssignee, and then we query for the item as it exists in the database before the change to find the existing value. I've also added in an additional check in bold to see if there is even a change being made to the div_assignedto property at all.

    Lastly, I'd recommend seeing if a Workflow or Life Cycle could be added to your use case instead of custom code like this. I could see a simple life cycle like the one below being added to this ItemType:

    Proposed->In Work->Completed

    A task request comes in and starts in the Proposed state. It could be looked at and reviewed in this state, and then assigned to a specific person before being voted into the In Work state. At that point, the person assigned to the task via the div_assignedto property is emailed. This kind of notification can be configured directly on the Life Cycle Map, so you won't need to write any custom code at all. 

    Chris

Children
No Data