it the fieldchanged event of a client script, is i...
# ask-stanley-ai
b
it the fieldchanged event of a client script, is it possible to retrieve the value of the field before it was changed
c
AI generated answer (using internal Slack threads):
In the fieldChanged event of a client script, it is not directly possible to retrieve the value of the field before it was changed. The client script only has access to the currentRecord, not the oldRecord. However, there are workarounds to achieve this. One common method is to use a global variable to store the initial value of the field during the pageInit event, and then compare or update this value during the fieldChanged event. Here is a code sample that demonstrates this approach:
Copy code
javascript
var OLDVALS = {
    entity: '',
    trandate: '',
    status: '',
    location: ''
};

function pageInit(context) {
    var rec = context.currentRecord;

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

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, the OLDVALS object is used to store the initial values of the fields. During the pageInit event, these values are set to the current values of the fields. Then, during the fieldChanged event, the new value of the field is compared with the stored value. If they are different, the code inside the if statement is executed. Remember to update the value in the global variable in the fieldChanged() as well so it works when being changed multiple times. ****** 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.847728729
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.846943498
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.835982382
Please provide your feedback on this answer.