Problem in calling Snippet from HTML template

  • 1
  • Problem
  • Updated 2 years ago
  • Solved
  • (Edited)
when i am calling snippet from HTML template , getting following error - 

-- Uncaught TypeError: Cannot read property 'item' of undefined


code - 
<a  href="javascript:skuid.snippet.getSnippet('ReadActionSnippetPA')();">{{Alert_Message__c}}</a>

whereas snippet works fine when called from Row Action.
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb

Posted 2 years ago

  • 1
Photo of Amy Dewaal

Amy Dewaal, Official Rep

  • 8,200 Points 5k badge 2x thumb
Hi Sumeet,

Are you calling the snippet from a template component or from a template field inside a component? Could you post the code for the snippet?

Thanks!
Amy
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb
Hi Amy,

I am calling snippet from template field inside component.

Snippet code-

var $ = skuid.$,args = arguments[0],
item = args.item,
list = args.list,
model = args.model;

var loggedInUser = skuid.model.getModel('LoggedInUser');
var userRow = loggedInUser.getFirstRow();

var currentUserModel = skuid.model.getModel( 'UnReadModelPA' );
var currentUserReadModel1 = skuid.model.getModel( 'ReadModelDesktop' );
var currentUserReadModel2 = skuid.model.getModel( 'ReadModelOnline' );
var alertId = currentUserModel.getFieldValue( item.row, 'Id' ) ;


//create row in ReadAlerts
if(item.row.Alert_Type__c == 'Desktop'){
var new_row = currentUserReadModel1.createRow(

additionalConditions: 
[
{ field: 'Alert_Name__c', value: alertId},
{ field:'Name__c', value: item.row.Name },
{ field: 'User_Name__c', value: userRow.Id }
],
doAppend: true
});

//Save ReadAlerts
$.when(currentUserReadModel1.save()).done(function(){

var TAB_SET_ID = 'AlertTabSet';
var c = skuid.component.getById(TAB_SET_ID);
var modelsToLoad =[];
//modelsToLoad.push(currentUserReadModel1);
modelsToLoad.push(currentUserModel);

skuid.model.updateData(modelsToLoad);
window.location.reload();
//c.render();

});
} else if(item.row.Alert_Type__c == 'Online'){
var new_row = currentUserReadModel2.createRow(

additionalConditions: 
[
{ field: 'Alert_Name__c', value: alertId},
{ field:'Name__c', value: item.row.Name },
{ field: 'User_Name__c', value: userRow.Id }
],
doAppend: true
});

//Save ReadAlerts
$.when(currentUserReadModel2.save()).done(function(){

var TAB_SET_ID = 'AlertTabSet';
var c = skuid.component.getById(TAB_SET_ID);
var modelsToLoad =[];
//modelsToLoad.push(currentUserReadModel2);
modelsToLoad.push(currentUserModel);

skuid.model.updateData(modelsToLoad);
window.location.reload();
//c.render();

});
}
Photo of David Giger

David Giger

  • 1,758 Points 1k badge 2x thumb
Hi Sumeet,

You are getting this error because you want to read the item property of args which is undefined. When you call a Skuid Snippet with a row action, Skuid provides you with the argument params (which contain the context model, row and other useful stuff).
But you are calling the snippet without any params supplied (and not Skuid managed).
You need to figure out what exactly you need to supply as params, or (worst case) rewrite your snippet.

Your call from the template should then look something like this:

<a  href="javascript:skuid.snippet.getSnippet('ReadActionSnippetPA')('asd');">asd</a>
But of course instead of the string 'asd' you probably want to supply an ID of a row or some other context you need in your snippet (this can also be an object, you just have to assemble it in the template using a standard HTML script tag and write some js code there as well).

Cheers
(Edited)