Thursday, January 02, 2014

Update email properties

I had a client requirement to update few of the attributes of incoming email.

The incoming email was tracked on customers based on the from email address. Customer did not want the auto-resolution of emails to customers as many of the customers had the same email ( family members tend to have the same email for all of them). So they want to remove the customer name from the auto resolution.

The response from me was @£$€£€@$€{]}[$£

yeah it's possible. but HOW? We remove the activity party from the sender list... sounds simple :)

Go through the code to find yourself

For example they wanted to have a list view of email with the body of the email (a quick preview of the email).

This was not possible using the standard description field of the email as it contained HTML codes. The alternative is to create a new column to hold the plain text email. So I went ahead and created a new multiline text field with 2000 char limit.

I added a plugin on post activity create to update the description of the email. optionally the same plugin is to be added on email update for the description field.

Here you go with the code. target is the email entity

Entity target = context.InputParameters["Target"] as Entity;

Code:
Entity _email = service.Retrieve("email", target.Id, new ColumnSet("from","to", "description", "directioncode"));

                    if (!((bool)_email["directioncode"])) //Incoming
                    {
                        EntityCollection from = (EntityCollection)_email["from"];

                        if (from.Entities.Count > 0)
                        {
                            foreach (Entity actParty in from.Entities)
                            {
                                if (actParty.Contains("partyid"))
                                {
                                    actParty["partyid"] = null;
                                }
                                else
                                {
                                    actParty.Attributes.Add("partyid", null);
                                }
                            }
                        }
                        _email["from"] = from;
                    }
                    else //outgoing
                    {
                        EntityCollection to = (EntityCollection)_email["to"];

                        if (to.Entities.Count > 0)
                        {
                            foreach (Entity actParty in to.Entities)
                            {
                                if (actParty.Contains("partyid"))
                                {
                                    actParty["partyid"] = null;
                                }
                                else
                                {
                                    actParty.Attributes.Add("partyid", null);
                                }
                            }
                        }
                        _email["to"] = to;
                    }
                    if (_email.Contains("description"))
                    {
                        string desc = _email["description"].ToString();
                        string descStrip = PluginHelper.StripHTML(desc);
                        descStrip = descStrip.Trim();
                        string desc4 = "";
                        string prevLine = "";
                        foreach (string currLine in descStrip.Split('\r'))
                        {
                            if (currLine != prevLine)
                            {
                                prevLine = currLine.Trim();
                                desc4 += currLine.Trim();
                                if (currLine != "")
                                {
                                    desc4 += "\n";
                                }
                            }
                        }

                        if (desc4.Length > 2000)
                            desc4 = desc4.Substring(1, 2000);

                        if (_email.Contains("ssb_mail_text"))
                        {
                            _email["ssb_mail_text"] = desc4;
                        }
                        else
                        {
                            _email.Attributes.Add("ssb_mail_text", desc4);
                        }
                        _email.Attributes.Remove("description");
                    }
                    service.Update(_email);

No comments: