JavaScript - Store Variable in Browser

JavaScript - Store Variable in Browser

Here is a quick tip when using JavaScript in the context of Main forms and opening Quick Create forms. In this scenario, I have an attribute on the Main form that I need to reference in a function that exists on the Quick Create form that I am opening. However, the attribute is not included in the formContext available on the Quick Create form. What we can do is store the value of the attribute from the Main form in the "top" properties. The following function can run onLoad on your Main form.

getChapterType: function(executionContext)
    {
        var formContext = executionContext.getFormContext();
        var chapterType = formContext.getAttribute("pre_chaptertype").getValue();

        window.top.chapterType = chapterType;
    }

This allows us to store chapterType as a variable for use later.

We can validate this by running window.top in the console.

I can now access the chapterType variable from my browser using the function found in my Quick Create form. I have the following script running onLoad :

 setFilter: function (executionContext) {
        var formContext = executionContext.getFormContext();
        var attribute = formContext.getAttribute("pre_position");
        attribute.controls.forEach(control => control.addPreSearch(PRE.ChapterCommitteeMember.filterType));
    },
    filterType: function (executionContext) {
        var control = executionContext.getEventSource();
        var chapterType = window.top.chapterType;

        if (chapterType == 626640000 || chapterType == 626640002 || chapterType == 626640003) {
            var filter = "<filter type='or'><condition attribute='pre_stateorlocal' operator='eq' value='0' /><condition attribute='pre_stateorlocal' operator='null' /></filter>";
            control.addCustomFilter(filter);

        }
        else {
            var filter = "<filter type='or'><condition attribute='pre_stateorlocal' operator='eq' value='1' /><condition attribute='pre_stateorlocal' operator='null' /></filter>";
            control.addCustomFilter(filter);
        }

All in all, this allows me to take an attribute found on the Account entity, save it for use on the Quick Create form that I open, then use the variable to apply a custom filter to a lookup found on the Quick Create form.