Skip to main content
Nintex Community Menu Bar
Question

Can't get snippet to run on page load

  • July 9, 2024
  • 7 replies
  • 66 views

Forum|alt.badge.img+5

Mostly because I don’t know how 🙂

I have a script which works fine when pasted into the console, but I would like for it to run on Page Load.

var params = arguments[0],

    step = params.step,

$ = skuid.$;

var userModel = skuid.model.getModel(‘User’),
    user = userModel.getFirstRow(),
   weekUpdateModel = skuid.model.getModel(‘WeeklyUpdate’),
   weekUpdate = weekUpdateModel.getFirstRow();
var userAlias = user.Alias + ’ Update - ’ + user.Today__c;
weekUpdateModel.updateRow(weekUpdate,‘Note’,userAlias);
$.each(weekUpdateModel.registeredItems,function(){
  this.refreshFields();
});

Thanks,

Jacob

7 replies

Forum|alt.badge.img+17
  • Nintex Employee
  • July 9, 2024

You actually want this snippet to run AFTER page load,  because you need the model data to be available.  This is why it runs successfully from the console.  Models have loaded etc.  You need to wrap your snippet in a function that waits for the page to load before executing.  Somthing like this: 

(function(skuid){ var $ = skuid.$; $(function(){ Your Function goes here...... }); })(skuid);


Actually if you create a new Javascript resource of type Inline you will get this code prepopulated for you.  


Forum|alt.badge.img+5
  • Author
  • July 9, 2024

Thanks Rob! Learning JS by sheer force is challenging sometimes.


Forum|alt.badge.img+7

Page loading snippet


Forum|alt.badge.img+2

Hi Rob, This works fine for a page. But when i use this page as a popup in another, its not working. Can u please suggest a solution for it? Thanks!


Forum|alt.badge.img+17
  • Nintex Employee
  • July 9, 2024

Forum|alt.badge.img+11
(function(skuid) {&nbsp; &nbsp; <br>&nbsp; &nbsp; var $ = skuid.$;<br>&nbsp; &nbsp; // Register a snippet to run<br>&nbsp; &nbsp; skuid.snippet.registerSnippet('HelloWorld', function() {<br>&nbsp; &nbsp; &nbsp; &nbsp; console.log("Go Skuid!");<br>&nbsp; &nbsp; });<br>&nbsp; &nbsp; // Called on page load<br>&nbsp; &nbsp; function init() {<br>&nbsp; &nbsp; &nbsp; &nbsp; skuid.snippet.getSnippet('HelloWorld')();<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; // Run the snippet initially on page load<br>&nbsp; &nbsp; $('.nx-page').one('pageload', function() {<br>&nbsp; &nbsp; &nbsp; &nbsp; init();<br>&nbsp; &nbsp; });<br>})(skuid);

Forum|alt.badge.img+2

Thanks! Found the solution 🙂