Javascript Snippet works, but when previous step is clicked it doubles table entries

I’ve been using this post and a couple of others to get my wizard to work the way I want it, but now I’m trying to fix a bug with it and can’t figure out how. I’ve created a wizard that converts an Opportunity to a custom object called “Sales Orders”. The wizard takes all the relevant fields from the Opportunity object and populates the wizard using URL parameters and a JavaScript Snippet on the “Next Step” button. All the fields work properly, but if I click “previous step” and then “next step” again it doubles all the rows on my table. Before I click Previous step Like so After I hit previous step I’m assuming this is because I have a loop that runs on the JavaSnippet, so I added an If statement before the loop to check if a variable “PreviousStepClicked” === 0 before starting the loop (and made “PreviousStepClicked = 1 after the loop”, but it doesn’t seem to work. I’m very novice in Javascript and know it’s probably something small and stupid that I’m doing wrong and would greatly appreciate any help anyone has to offer to solve this problem. Here is the JavaSnippet var params = arguments[0]; var step = params.step; var $ = skuid.$; var models = skuid.model.map(); var OppLineItems = models.OppLineItems; var NewSalesOrderItems = models.NewSalesOrderItems; var i = 1; var PreviousStepClicked = 0; if (PreviousStepClicked === 0) { $.each(OppLineItems.data,function(){ var row = NewSalesOrderItems.createRow({ additionalConditions: [ { field: ‘QTY_Req__c’, value: this.QTY_Req__c}, { field: ‘Price_Per_Part__c’, value: this.Price_Per_Part__c}, { field: ‘Part__c’, value: this.Part__c}, { field: ‘Total_Sale_Price__c’, value: this.Total_Sale_Price__c}, { field: ‘Packaging_Note__c’, value: this.Packagig_Note__c}, { field: ‘Part__r.Name’, value: this.Part__r.Name}, { field: ‘Line_Description__c’, value: this.Line_Description__c}, { field: ‘Line_Item_Num__c’, value: i}, i = i + 1, PreviousStepClicked = 1 ], doAppend: true }); }); } // We’re good to go - navigate our wizard to Step 2 step.navigate(‘step2’); Thank you

Hi Sam

I suggest that you create a UI-only field (i.e. ‘newItemsCreated’, preferably a boolean) in one of your models (preferably a model where you have only one record which won’t change during runtime) and set that after your loop to true. And Before you start the loop, check if that field exists/is set to true.

Your snippet would then look something like this:

var params = arguments[0], step = params&#46;step, $ = skuid&#46;$, models = skuid&#46;model&#46;map(), OppLineItems = models&#46;OppLineItems, NewSalesOrderItems = models&#46;NewSalesOrderItems, modelWhitUiOnlyField = model&#46;modelname; &#47;&#47;change that to your model if(!modelWhitUiOnlyField&#46;getFirstRow()&#46;itemsCreated) { $&#46;each(OppLineItems&#46;data, function(index, lineItem){ var row = NewSalesOrderItems&#46;createRow({ additionalConditions: [ {field: 'QTY_Req__c', value: lineItem&#46;QTY_Req__c}, {field: 'Price_Per_Part__c', value: lineItem&#46;Price_Per_Part__c}, {field: 'Part__c', value: lineItem&#46;Part__c}, {field: 'Total_Sale_Price__c', value: lineItem&#46;Total_Sale_Price__c}, {field: 'Packaging_Note__c', value: lineItem&#46;Packagig_Note__c}, {field: 'Part__r&#46;Name', value: lineItem&#46;Part__r&#46;Name}, {field: 'Line_Description__c', value: lineItem&#46;Line_Description__c}, {field: 'Line_Item_Num__c', value: index + 1} ], doAppend: true }); modelWhitUiOnlyField&#46;updateRow(modelWhitUiOnlyField&#46;getFirstRow(), {'itemsCreated': true}); &#47;&#47;itemsCreated would be your UI-Only field }); } step&#46;navigate('step2'); <br />

If you want to keep the original script, you need to increment i and set PreviousStepClicked to 1 AFTER you create the new row:

var params = arguments[0], step = params&#46;step, $ = skuid&#46;$, models = skuid&#46;model&#46;map(), OppLineItems = models&#46;OppLineItems, NewSalesOrderItems = models&#46;NewSalesOrderItems, i = 1, PreviousStepClicked = 0, row; if (PreviousStepClicked === 0) { $&#46;each(OppLineItems&#46;data,function(){ row = NewSalesOrderItems&#46;createRow({ additionalConditions: [ { field: 'QTY_Req__c', value: this&#46;QTY_Req__c}, { field: 'Price_Per_Part__c', value: this&#46;Price_Per_Part__c}, { field: 'Part__c', value: this&#46;Part__c}, { field: 'Total_Sale_Price__c', value: this&#46;Total_Sale_Price__c}, { field: 'Packaging_Note__c', value: this&#46;Packagig_Note__c}, { field: 'Part__r&#46;Name', value: this&#46;Part__r&#46;Name}, { field: 'Line_Description__c', value: this&#46;Line_Description__c}, { field: 'Line_Item_Num__c', value: i} ], doAppend: true }); i++; PreviousStepClicked = 1; }); } &#47;&#47; We're good to go - navigate our wizard to Step 2 step&#46;navigate('step2'); 

Cheers