How can I get the old value of a field in fieldCha...
# ask-stanley-ai
j
How can I get the old value of a field in fieldChange function in Client Script?
c
AI generated answer (using internal Slack threads):
In a client script, you can't directly get the old value of a field in the fieldChange function. However, you can use a workaround by using a global variable to store the initial value of the field in the pageInit function, and then compare or update this value in the fieldChanged function. Here is a sample code snippet:
Copy code
javascript
var OLDVALS = {
    entity: '',
    trandate: '',
    status: '',
    location: ''
};

/**
* @param {ClientScriptContext.pageInit} context
*/
function pageInit(context) {
    var rec = context.currentRecord;

    Object.keys(OLDVALS).forEach(function (fieldId) {
        OLDVALS[fieldId] = rec.getValue(fieldId);
    });
}

/**
* @param {ClientScriptContext.fieldChanged} context
*/
function fieldChanged(context) {
    var fieldId = context.fieldId;
    var rec = context.currentRecord;

    if (Object.keys(OLDVALS).indexOf(fieldId) > -1 && rec.getValue(fieldId) !== OLDVALS[fieldId]) {
        // Do stuff
    }
}
In this code,
OLDVALS
is a global object that holds the initial values of the fields. In the
pageInit
function, it sets the initial values of the fields. In the
fieldChanged
function, it checks if the current value of the field is different from the initial value. If it is, you can perform your desired operations. Remember to update the
OLDVALS
object in the
fieldChanged
function if the user changes the field more than once. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.865595341
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.858214855
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.856849968
Please provide your feedback on this answer.