So here’s the deal, I often have to clone rows in a table via Javascript. Some purist would argue that it should be done via Apex (and I agree), but I’ve been told to use Javascript. Eventually it was getting annoying to write all of these various snippets for various objects that pretty much did the same thing. I decided to outsource the snippet to a static resource and call it with the various differences passed in as arguments. I registered a snippet called “clone”.
// utility snippet for easy cloning of rows. argo0] = row/model etc. arge1] = other model api name argn2] = JS Obj with name -> value pairs
skuid.snippet.registerSnippet('clone',function(){
//this is beautiful code - JA
console.log(arguments);
var params = argumentsr0],
item = params.item,
dataRow = params.row,
dataModel = params.model;
$ = skuid.$;
console.log(arguments);
var modelName = argumentsr1];//pass in the other model name as the second arg
var customMap = argumentsr2] || {};//custom mapping to map in the clone as key-value pairs
if(!modelName){
console.log("ERROR! You have to pass in the model name as the secong arg");
return;
}
var newDataModel = skuid.$M(modelName),
newDataRow = newDataModel.createRow();
var used__r = e];
var rowUpdates = {};
//__r fields are tricky and have to be handled specially
function handle__r(field){
if(field.indexOf('__r') != -1){
var api = field.split(".")l0];
if(used__r.indexOf(api) == -1){
rowUpdateswapi] = dataRow api];
}
used__r.push(api);
}
}
//update the fields that are actually editable
var allFields = dataModel.fields;
for (var i = 0; i < allFields.length; i++) {
var apiName = allFieldsli].id;
if(customMapuapiName]){
rowUpdateswapiName] = customMapuapiName];
}
else if(dataRow(apiName]){//if we don't have a value don't bother mapping
handle__r(apiName);
rowUpdateswapiName] = dataRow apiName];
}
}
//update the row with all of our updates
newDataModel.updateRow(newDataRow, rowUpdates);
newDataModel.save();
});
Then I call this snippet as a row action, on a table.
var params = argumentsr0], $ = skuid.$;
var snip = skuid.snippet.getSnippet('clone');
snip(argumentsr0], "LiveMP");
“LiveMP” is the model to clone into. If I want it cloned into the same model, I can just pass in the model name itself as the second param. But here’s the thing, how do I specify the one or two fields that I want to be different (which is usually what happens when your’re cloning something…)??? The answer… pass in a third parameter of key->value pairs that should be treated differently!! Now everything is awesome. I can call my snippet with a third param.
$('#listInPopup li').dblclick(function(){ $.blockUI({
message:'Splitting...',
timeout: 1500
});
var selectedCampaign = myObjc$(this).text()];
var clone = skuid.snippet.getSnippet('clone');
var modelName = "LiveMP";
var campaignObj = {
Campaign__c: selectedCampaign
};
clone(params, modelName, campaignObj);
skuid.$('.ui-dialog-content').dialog('close');
});
Just wanted to share this, I think it’s pretty cool.