EncompDEV
LoanDebugger2

Get Employee ID Plugin

Click here to download this plugin with source code     See Demo Video

This plugin returns Employee ID in [CX.EMP.EMPID] when [CX.EMP.USERID] is set to the user ID. It returns "ERROR" in case of errors, and an Error debug trace is made, which can be observed in either the Debug Log or via Loan Debugger.

Code used for this plugin:
using System;
using System.Collections.Generic;
using System.Text;

using EllieMae.Encompass.ComponentModel;
using EllieMae.Encompass.Automation;
using EllieMae.Encompass.BusinessObjects.Loans;
using EllieMae.Encompass.BusinessObjects.Users;

namespace GetEmployeeIDPlugin
{
    [Plugin]
    public class EncompassPlugin
    {
        // Constructor - wait for Login event
        public EncompassPlugin()
        {
            EncompassApplication.Login += new EventHandler(EncompassApplication_Login);
        }

        // Login - handle Loan Open and Loan Closing events
        private void EncompassApplication_Login(object sender, EventArgs e)
        {
            EncompassApplication.LoanOpened += new EventHandler(LoanOpenedEvent);
            EncompassApplication.LoanClosing += new EventHandler(LoanClosingEvent);
        }

        // Loan Opened event - start monitoring field changes
        private void LoanOpenedEvent(object sender, EventArgs e)
        {
            EncompassApplication.CurrentLoan.FieldChange += new FieldChangeEventHandler(FieldChangeEvent);
        }

        // Loan Closing event - stop monitoring field changes
        private void LoanClosingEvent(object sender, EventArgs e)
        {
            EncompassApplication.CurrentLoan.FieldChange -= new FieldChangeEventHandler(FieldChangeEvent);
        }

        // Field Change event - if FieldID = [CX.EMP.USERID] - set [CX.EMP.EMPID]
        // return "ERROR" in [CX.EMP.EMPID] in case of any errors or if user does not exist
        // return "" in [CX.EMP.EMPID] when [CX.EMP.USERID] is ""
        private void FieldChangeEvent(object source, FieldChangeEventArgs e)
        {
            if (e.FieldID != null &&
                e.FieldID.Equals("CX.EMP.USERID", StringComparison.OrdinalIgnoreCase))
            {
                Loan loan = EncompassApplication.CurrentLoan;
                try
                {
                    string sUserID = e.NewValue.Trim();
                    User user = loan.Session.Users.GetUser(sUserID);
                    if (user == null)
                    {
                        throw new Exception("No such user: [" + sUserID + "]");
                    }
                    else
                    {
                        loan.Fields["CX.EMP.EMPID"].Value = user.EmployeeID;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    // 1) log error if we are debugging
                    System.Diagnostics.Debug.WriteLine("GetEmployeeIDPlugin Exception: " + ex.Message);
                    // 2) try to set error in the returned field
                    try { loan.Fields["CX.EMP.EMPID"].Value = "ERROR"; }
                    catch { }
                }
            }
        }

    }
}