Adding rows in one object based on another

  • 1
  • Question
  • Updated 4 years ago
I have a custom "Account Rep" object that is a child of the "Account" object.  When an opportunity is entered under this account, I am trying (unsuccessfully) to iterate over the rows in the "Account Rep" object to add these reps to another custom object (commissions) that is a child of the opportunity object.

I tried to steal code from the "mass create records" example and my modifications are still lacking.  Specifically, I am having issues copying the owner id from one model to the other and I don't know how to iterate through the rows.

My first model is called "Reps" and I want to copy the following fields:

Owner
Rate1__c
Rate2__c
Rate3__c

To the model called "Commissions".  Let's just say the field names are the same.
Photo of bimbyc

bimbyc

  • 408 Points 250 badge 2x thumb

Posted 4 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
The "mass create records" example has been the source of everything I've been able to do that looks like that.  You should be able to iterate over all records and send the owner ID and the relevant Rate values into the commission records. 

Another place this is done is in our opportunity detail page that is in our samples.   Download it here.   Just be ware that there is a static resource you need to add to your org for some of the capabilities to work correctly.    Once you ahve that built,  look at the Products tab and select the mass action "Generate Assets"

You should be able to make this work...  
Photo of bimbyc

bimbyc

  • 408 Points 250 badge 2x thumb
No matter what I do, the only field that I can't get to copy is my owner id.

Here's my code:

var params = arguments[0];var $ = skuid.$;
// Get references to our two Models
var models = skuid.model.map();
var reps = models.Reps;
var commissions = models.Commissions;

// Copy details from Reps to Commissions
$.each(reps.data,function(i,row){



var HTRate = reps.getFieldValue(row,'Hang_Ten_Rate__c');
var COOLARate = reps.getFieldValue(row,'COOLA_Rate__c');
var TBRate = reps.getFieldValue(row,'Tommy_Bahama_Rate__c');
var theOwner = reps.getFieldValue(row,'OwnerId');
var newrow = commissions.createRow({
        additionalConditions: [
            { field: 'Hang_Ten_Rate__c', value: HTRate, operator: '='},
            { field: 'COOLA_Rate__c', value: COOLARate, operator: '='},
            { field: 'Tommy_Bahama_Rate__c', value: TBRate, operator: '='},
           { field: 'OwnerId', value: theowner, operator: '='},
        ]
    });
})
Photo of bimbyc

bimbyc

  • 408 Points 250 badge 2x thumb
I fixed the spelling of "value:theowner" to "value:theOwner" and it still doesn't work.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You have to add a "nameFieldValue" parameter with the owner name. So basically you would add a variable for the name in the loop:

var ownerName = reps.getFieldValue(row,'Owner.Name');

and then change the last line to this:

{ field: 'OwnerId', value: theowner, operator: '=', nameFieldValue: ownerName}
Photo of bimbyc

bimbyc

  • 408 Points 250 badge 2x thumb
Thank you so much for this Moshe!!!!!!!!!

It worked perfectly!!!!!!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Glad you were able to get this working..