EncompDEV
LoanDebugger2

[CX.NOW] At Loan Open And Save Plugin

Click here to download this plugin with source    
This plugin will allow you to set [CX.NOW] field to DateTime.Now when loan is opened and right before loan is saved.

This allows to write field triggers that utilize Virtual Fields and other objects which do not always trigger rules themselves. Since custom field changes always execute field triggers, accessing Virtual Fields inside such triggers will read correct values.

Following screenshots demonstrate how [CX.NOW] behaves compared to [CX.NOW1] and [CX.NOW2] which are assigned to DateTime.Now and Now via standard Encompass field calculations. All three fields are included on the Test form provided in EMPKG.

All test screenshots include Loan Monitor output tracing/displaying exact field changes.


1. Initial Loan Open - all fields are set.

2. First Loan Save - [CX.NOW] updated. Other fields - not.

3. Second Loan Save. Same behaviour.

4. Loan Amount Changed. None of the fields are changed.

5. Loan.Recalculate() called. [CX.NOW1] and [CX.NOW2] are updated.

6. Loan Save. [CX.NOW] is updated. [CX.NOW1] and [CX.NOW2] are not.

Code used in this plugin (C#):

using System;
using System.Collections.Generic;
using System.Text;

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

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

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

        // Loan Opened event - start monitoring BeforeCommit
        private void LoanOpenedEvent(object sender, EventArgs e)
        {
            EncompassApplication.CurrentLoan.BeforeCommit += new CancelableEventHandler(BeforeCommitEvent);
            SetCxNowField();
        }

        // Loan Closing event - stop monitoring BeforeCommit
        private void LoanClosingEvent(object sender, EventArgs e)
        {
            EncompassApplication.CurrentLoan.BeforeCommit -= new CancelableEventHandler(BeforeCommitEvent);
        }

        // Before Commit - set CX.NOW = Now so that stuff can trigger off it
        private void BeforeCommitEvent(object source, CancelableEventArgs e)
        {
            SetCxNowField();
        }

        // Set [CX.NOW] field - must be STRING 20-character
        private void SetCxNowField()
        {
            try
            {
                Loan loan = EncompassApplication.CurrentLoan;
                if (loan != null)
                {
                    loan.Fields["CX.NOW"].Value = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("SetCxNowField Error: " + ex.Message);
            }
        }

    }
}