Fist pump time!!! Got my crazy cloning snippet done in way less time expected.

  • 4
  • Praise
  • Updated 3 years ago
Praise to the Skuid folks!!!

Your app allows me to do so much in such little time where it would otherwise cost me and my client 10X more without it. The following was quoted to take 25-35 hours in APEX. Probably more as I learned about some additional hiccups during the process. Always impressed with what's possible.

My snippet clones a 9 object data set where approx. 30-50 records are being queried using model Clone property "Yes, Always". So now when the factory manager submits a production order for products of any kind, the template (9 object record set) of this product (the Master record type), is cloned, set to record type "Production" and linked to the original Master record using a custom formula field called GhostRecordId prior to saving the cloned record.

AWESOME!!!!! Look at how little code is required to do this!!!!!

var params = arguments[0],
	$ = skuid.$,
	prodProductions = skuid.$M('POLineProductProductions'),
	prodCount = prodProductions.data.length,
	prodProductionRow,
	cloneProductCode = skuid.$M('cloneProductCode'),
	cloneProductCodeRow,
	cloneProductCodeRowId,
	models = [],
	dfd = $.Deferred();
    
    $.blockUI({message: 'Placing order'});
    
    function queryCloneModels(prodIndex){
        // query more clone models once per product production
        $.blockUI({message: 'Placing order on product production ' + prodIndex + '/' + prodCount});
        $.when(skuid.model.updateData(models))
            .done(function(){
                // loop through all models and rows to set Master with GhostRecord
                $.each(models,function(m,model){
                   $.each(model.getRows(),function(r,row){
                       model.updateRow(row,{Master__c: row.GhostRecordId__c});
                   });
                });
                
                $.when(skuid.model.save(models))
                    .done(function(){
                        
                        prodProductionRow = prodProductions.getFirstRow();
                        cloneProductCodeRow = cloneProductCode.getFirstRow();
	                    cloneProductCodeRowId = cloneProductCodeRow.Id;
                        
                        // set first Product Production in model to current Product Code
                        prodProductions.updateRow(prodProductionRow,{Product_Code__c: cloneProductCodeRowId});
                        
                        $.when(prodProductions.save())
                            .done(function(){
                                // abandon first row from prodProductions as this row has been processed
                                prodProductions.abandonRow(prodProductionRow);
                                
                                // if any Product Productions still left then cycle again
                                if(prodProductions.data.length){
                                    queryCloneModels(prodCount - prodProductions.data.length + 1);
                                } else {
                                    dfd.resolve();
                                    $.unblockUI();
                                }
                            })
                            .fail(function(){
                                console.log('something went wrong updating and saving product production with new product code');
                            });

                    })
                    .fail(function(){
                        console.log('something went wrong saving models');
                        dfd.reject();
                    });
            })
            .fail(function(){
                console.log('something went wrong querying models');
                dfd.reject();
            });
    }

    $.each(skuid.model.list(),function(m,model){
        if (model.id.search("clone") != -1){
            models.push(model);
        }
    });

    queryCloneModels(1);

    return dfd.promise();
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb

Posted 3 years ago

  • 4
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Nice recursion Pat! From Little League to the Majors in less than a year!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Fist pump indeed.  I agree with Moshe.  You are approaching Skuid Javascript Guru status.