salesforce formula field undefined on pageload

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?

Hi Jerry, can you share the snippet? It sounds like the snippet may be running too soon. 

Sure Mark, here is the snippet I’m running

(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.Bandwidth_Submit_For_Approval__c);//this is the one that has no definition yet at runtime
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(‘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);

// Run the snippet initially on page load


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.

Thanks for sharing that. Is it set up as an inline resource, or a snippet? I believe for this you would want it set up as inline. 
I see at the bottom that you’ve got it calling the function when the .nx-page class is loaded. That’s the reliable way to fire off code when a Skuid page has finished loading, so my suspicion is that your $.when is not being triggered when you expect it to.

When the UIOnlyModel is first loaded on pageload, I don’t think updateData() is involved, which would mean the $.when condition isn’t satisfied on pageload, if I understand correctly. You could test this by adding a console.log or debugger just below the .when line to see if it fires off. (You may have already done this).

The snippet is inline, and I have several console logs inside the execution of the the $.when so I can verify that the code in there does fire. Any of the criteria in that portion of the code not involving that formula field is evaluated correctly.

It might be worth noting that the UIOnly model used there does not have any fields on it, I am simply using it to control a pagetitle component that I did not want refreshed every time the pages main model gets updated.

It occurs to me that this is an UI-only model; I’m not sure that the updateData() method would work on it, since such a model is never going to be querying anything. 

That is a good point, but my actual use for it as I said in the last post is only to use it as an owner for a pagetitle where I display warning messages to the user. the Updatedata call on the UIModel removes the existing warning messages when it’s called and recreates the ones that are currently valid

Although I’ve never tested this, I’d like to think that if I had formula fields on that model an UpdateData call would recalculate those as well

I have a few questions for clarification:

1) this formula field is a Skuid UI-only formula field, right? Or is it a Salesforce formula field?
2) What’s the name of the field that’s showing as undefined? Does its model load records on pageload?
3) Which version of Skuid are you using?

I’m also wondering if you could set up a model action that runs the necessary code when your formula field is updated. I don’t think this would fire the script off on pageload, but if you were to temporarily set up a button that queries that model, then you could watch and see if the model action refreshes the formula field, and also if the script can then see the formula’s value. The point of this would be to try and zero in on the point of failure. 

  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

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">
        <model id="acc" limit="1" query="true" createrowifnonefound="false" datasource="salesforce" type="" sobject="Account">
                <field id="Go_Formula__c"/>
        <basicfieldeditor showheader="true" showsavecancel="true" showerrorsinline="true" model="acc" buttonposition="" uniqueid="sk-3HeNF1-109" mode="read">
                <column width="50%">
                        <section title="Section A" collapsible="no">
                                <field uniqueid="sk-3HeOeF-121" id="Go_Formula__c"/>
                <column width="50%">
                        <section title="Section B">
            <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
        <styleitem type="background" bgtype="none"/>

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.

Did you try this on your existing page, or on the XML test page I shared?

the existing page with the formula field that does not evaluate in the snippet

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.