salesforce formula field undefined on pageload

  • 1
  • Question
  • Updated 1 year ago
  • Doesn't Need an Answer
I'm working with a formula field on one of my organizations custom objects that is used as rendering criteria and referenced in the pages javascript in other areas. The problem I am having is that even though it evaluates correctly as part of rendering criteria for page components in my on pageload javascript snippet is shows up as undefined.

Has anyone encountered this before? How can I access that fields value in the pageload snippet without recreating that formula?
Photo of Jerry

Jerry

  • 1,150 Points 1k badge 2x thumb
  • like a potato

Posted 1 year ago

  • 1
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
Hi Jerry, can you share the snippet? It sounds like the snippet may be running too soon. 
Photo of Jerry

Jerry

  • 1,150 Points 1k badge 2x thumb
Sure Mark, here is the snippet I'm running
<code>
(function (skuid) {
    $ = skuid.$;
    skuid.snippet.registerSnippet('CheckApprovalRequired', function() {
        var BANDWIDTHFIELDS = ['Bandwidth_Ceiling__c', 'Bandwidth_Floor__c'];
        var MARGINFIELDS = ['Pricing_Margin__c'];
        var tenor = skuid.$M('Tenor');
        var term = tenor.getFirstRow();
        var UIOnlyModel = skuid.$M('UIOnlyModel');
        var hasITAccounts = skuid.$M('ParentOpportunity').getFirstRow().Contains_IT_Accounts__c;
        var commodity = skuid.$M('Commodity').getFirstRow();
        var bandwidthAppliesTo = commodity.Bandwidth_Applies_To__c;
        var bandwidthDefault = (hasITAccounts ? commodity.Default_Bandwidth_IT__c : commodity.Default_Bandwidth__c);
        var pageTitle = 'MyPageTitle';
        var displayMessage = skuid.snippet.getSnippet('displayMessage');
        if(term.Pending_Approval__c !== true) {
$.when(UIOnlyModel.updateData()).then((result) => {
if((bandwidthAppliesTo === 'Underuse' && term.Bandwidth_Floor__c !== bandwidthDefault) ||
bandwidthAppliesTo !== 'Underuse' && term.Bandwidth_Ceiling__c !== bandwidthDefault) {
displayMessage('This opportunity\'s bandwidth is not set to the default of ' + bandwidthDefault + '%.', 'WARNING', pageTitle);
}
if(term.ApprovalNeeded) {
       console.log('pending approval is false');
       console.log(term.Needs_Margin_Approval__c);
       console.log(term.Bandwidth_Submit_For_Approval__c);//this is the one that has no definition yet at runtime
       console.log(term.Approved_Bandwidth__c);
    if(term.Needs_Margin_Approval__c === true && term.Bandwidth_Submit_For_Approval__c === true && term.Approved_Bandwidth__c === false) {
    console.log('needs margin approval and bandwidth submit for approval are true, approved bandwidth is false');
    displayMessage('Pricing Margin and Bandwidth need to be submitted for approval.', 'WARNING', pageTitle);
    }
    else if(term.Needs_Margin_Approval__c === true) {
           console.log(term.Bandwidth_Submit_For_Approval__c);
    console.log('needs margin approval is true');
    displayMessage('Pricing Margin needs to be submitted for approval.', 'WARNING', pageTitle);
    }  
    else if(term.Bandwidth_Submit_For_Approvall__c === true && term.Approved__c === false) {
    console.log('submit for approval is true, approved is false');
    displayMessage('Bandwidth needs to be submitted for approval.', 'WARNING', pageTitle);
    }
}
});
console.log(term.Bandwidth_Submit_For_Approval__c);
        }
    });
    
    // Run the snippet initially on page load
    $('.nx-page').one('pageload',function(){
        skuid.snippet.getSnippet('CheckApprovalRequired')();
    });
})(skuid);
</code>

I'm pretty sure you're correct, that was my assumption as well. I just don't know the best way to fix the issue.
Photo of Jerry

Jerry

  • 1,150 Points 1k badge 2x thumb
1) Nope, it's a salesforce formula field on the pages main model

2) The field is called Bandwidth_Submit_For_Approval__c, and it does load a single record on page load based on a url Id parameter.

3) skuid version is 9.5.2
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
Is the Bandwidth_Submit_For_Approval__c part of your model? And does it have the right security settings for the environment it's in?

I've successfully used the XML (with simplified script) below to pull in a formula field, and have it logged to the console. Do you mind trying this in a test page? 


<skuidpage unsavedchangeswarning="yes" personalizationmode="server" showsidebar="true" useviewportmeta="true" showheader="true">
    <models>
        <model id="acc" limit="1" query="true" createrowifnonefound="false" datasource="salesforce" type="" sobject="Account">
            <fields>
                <field id="Go_Formula__c"/>
            </fields>
            <conditions/>
            <actions/>
        </model>
    </models>
    <components>
        <basicfieldeditor showheader="true" showsavecancel="true" showerrorsinline="true" model="acc" buttonposition="" uniqueid="sk-3HeNF1-109" mode="read">
            <columns>
                <column width="50%">
                    <sections>
                        <section title="Section A" collapsible="no">
                            <fields>
                                <field uniqueid="sk-3HeOeF-121" id="Go_Formula__c"/>
                            </fields>
                        </section>
                    </sections>
                </column>
                <column width="50%">
                    <sections>
                        <section title="Section B">
                            <fields/>
                        </section>
                    </sections>
                </column>
            </columns>
        </basicfieldeditor>
    </components>
    <resources>
        <labels/>
        <javascript>
            <jsitem location="inline" name="newInlineJS" cachelocation="false" url="">(function (skuid) {
    $ = skuid.$;
    skuid.snippet.registerSnippet('CheckApprovalRequired', function() {
       
        var tenor = skuid.$M('Tenor');
        var term = tenor.getFirstRow();
        
       console.log(term.Bandwidth_Submit_For_Approval__c);//this is the one that has no definition yet at runtime
       });
    // Run the snippet initially on page load
    $('.nx-page').one('pageload',function(){
        skuid.snippet.getSnippet('CheckApprovalRequired')();
    });
})(skuid);</jsitem>
        </javascript>
        <css/>
    </resources>
    <styles>
        <styleitem type="background" bgtype="none"/>
    </styles>
</skuidpage>
Photo of Jerry

Jerry

  • 1,150 Points 1k badge 2x thumb
I tried putting the formula field on the page to be displayed. It shows up with an appropriate value, but never gets it's value in the page load snippet.
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
Did you try this on your existing page, or on the XML test page I shared?
Photo of Jerry

Jerry

  • 1,150 Points 1k badge 2x thumb
the existing page with the formula field that does not evaluate in the snippet
Photo of Jerry

Jerry

  • 1,150 Points 1k badge 2x thumb
Thank you for all of your help with this Mark, in the end it just became easier for me to manually evaluate the formula fields criteria in Javascript.