Create records in a table based on selected records in another table.

I haven’t quite seen this in the discussions, so I apologize it there is an existing thread. My problem is this. I have an object (Object A) that contains hundreds of thousands of records in it. Object A acts as a lookup object only and is not associated with accounts, opportunities, cases, or anything like that. I have another object (Object B) that that references Object A through a lookup. Object B is associated with Accounts, cases…all that jazz. The purpose of this structure is for easy record creation for Object B. When someone calls in, we find their information in Object A and create a record in Object B that will tie the account we are creating. I would like to search for a record in Object A table and add a mass action to create a record in Object B with the lookup already populated (lookup that references Object A). How do I go about doing this with a javascript inline snippet?

Hi Glenn, check out this tutorial : http://help.skuidify.com/s/tutorials/m/components/l/109496-mass-create-records pay close attention to the JavaScript at the end of the tutorial. Specifically you want to focus on this part :

  1. $.each(contacts.data,function(){
  2. var row = newStatements.createRow({
  3. additionalConditions: [
  4. { field: ‘Contact__c’, value: this.Id, operator: ‘=’, nameFieldValue: this.Name }
  5. ]
  6. });
  7. });
You can create a row and pass in values as “Additional Conditions”.

Thank you for the quick response! I have used this code in other areas of our deployment (works great!), but I did not think I could use for this case because of the $.each section of the code. I simply want to select records in Object A and create rows in Object B with the lookup Id of Object A already populated in Object B. I can use getFirstRow() to populate the account and contact we just created. Each account/contact could have multiple records I want associated with it. What object would I reference in the $.each section if I use this method. I’m afraid if I used ‘Object A’ in this section it would create hundreds of thousands of records, when I only need to create x number of rows for x number selected in the table & pass the Id of the selected records to the new rows. Is there a way to create a row for $.each ‘selected row in Object A’

You can use something like this from the mass action, to get the ids of all the Object A records:

var Ids = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){    
return item.row.Id;
});

You can then use a for loop to create a row for each Id in Ids, and populate the lookup with that Id field. I’m not sure if a “$.each” loop will work for this, although I think it can. You might be able to use a “for in” loop as well. 

thank you…I think that gets me on the right path. I’ll post back later with any success or modifications to get me further. 

Moshe - thanks for your participation in these posts.  Its great to see the community working together.  You are making a serious bid to become our next community champion! 

I have the script below created as a mass action in a table that references a model called ‘property’. ‘clientProperty’ is the model where I want a record to be created for every ‘property’ record that is selected. When I click on the dropdown for the mass action nothing happens. What I want to happen is all the records that are selected in the ‘property’ model to create a record in the ‘clientProperty’ model and autopopulate the ‘Property__c’ (lookup field between ‘clientProperty’ and ‘property’ ) filed with the selected record’s Id.

Script:

var params = arguments[0];var step = params.step;
var $ = skuid.$;
var models = skuid.model.map();

var Ids = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){     
return item.row.Id;
});

var clientproperty = skuid.model.getModel(‘clientProperty’);


$.each(Ids.data,function(){
    var ClientProprow = clientproperty.createRow({
        additionalConditions: [
            { field: ‘Property__c’, value: this.Id, operator: ‘=’},



        ]
    });
});

Ok let’s get the dumb questions out of the way.  You have the mass action set to type “Custom” and have made the calling the right snippet name?   If you console log “hello world” in the snippet - does it show?  Is somthing happening at all? 

yes, with alert(‘Hello World’); on mass action I get the alert in the browser. Name of snippet and mass action type seem to working just fine.

Hi Glenn,

I see a couple possible issues with the Javascript you’ve posted.

First, your ‘Ids’ variable is an array of strings. There is no “data” property, so you are attempting to iterate over an undefined value. Try removing “.data” from your “$.each” function.

Second, I see a stray comma after the condition object literal. Try removing that as well.

Your code should like like this:

$.each(Ids,function(){<br>&nbsp; &nbsp; var ClientProprow = clientproperty.createRow({<br>&nbsp; &nbsp; &nbsp; &nbsp; additionalConditions: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Property__c', value: this.Id, operator: '='}<br>&nbsp; &nbsp; &nbsp; &nbsp; ]<br>&nbsp; &nbsp; }); 

I also have one more suggestion. It’s generally a good idea to explicitly set the name field in your condition. If Skuid has any trouble looking up the name field in your model, then you’ll just end up with a fairly unreadable Id in place of a name in your relationship. You can explicitly set the name field like so:

&nbsp; &nbsp; &nbsp; &nbsp;additionalConditions: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Property__c', value: this.Id, operator: '=',&nbsp;nameFieldValue: 'Name' }<br>&nbsp; &nbsp; &nbsp; &nbsp; ]

Thank you JD! That got me ALMOST there. The correct number of rows are being created now, but the ‘Property__c’ filed is not being populated. If I use { field: ‘Property__c’, value: Ids, operator: ‘=’}, I get the array to display in the Propery__c field. How can I it to create a row and populate only 1 record from the Ids array?

I think the scope of the “this” keyword is no longer pointing to the id that you’re iterating over.  The “this” now has scope within the createrow function.  I would try explicitly setting

var currentId = this.Id;

outside the createRow function and then referencing it inside the createRow function like this…

$.each(Ids,function(){&nbsp; &nbsp; var currentId = this.Id; var ClientProprow = clientproperty.createRow({<br>&nbsp; &nbsp; &nbsp; &nbsp; additionalConditions: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Property__c', value: currentId, operator: '='}<br>&nbsp; &nbsp; &nbsp; &nbsp; ]<br>});

just to clarity. I did use { field: ‘Property__c’, value: this.Id, operator: ‘=’, nameFieldValue: ‘Name’ }, but only the actual text “Name” appeared in the Property__c field. I also tried { field: ‘Property__c’, value: this.Id, operator: ‘=’} which returned no result in the Property__c field.

Ben, thank you for chiming in, but I am getting the same result as if I used this.Id…Property__c is not being populated at all.

Sorry Glenn, I came in this late and didn’t have all my ducks in a row.  When you’re creating your array of ids using the skuid.$.map function, you’re not setting any properties on that object.  So I think the code below should be correct.  You just need to remove the .Id after “this”.

$.each(Ids,function(){&nbsp; &nbsp; var currentId = this; var ClientProprow = clientproperty.createRow({<br>&nbsp; &nbsp; &nbsp; &nbsp; additionalConditions: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Property__c', value: currentId, operator: '='}<br>&nbsp; &nbsp; &nbsp; &nbsp; ]<br>});

Ben thanks so much. That did the trick! Thank you ALL. What a team effort! I cannot express how grateful I am to the community. When I learn more I will give back for sure.

here is the code that works. 

var params = arguments[0];var step = params.step;
var $ = skuid.$;
var models = skuid.model.map();

var Ids = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){     
return item.row.Id;
});

var clientproperty = skuid.model.getModel(‘clientProperty’);


$.each(Ids,function(){ 
var currentId = this;
var ClientProprow = clientproperty.createRow({ 
        additionalConditions: [
{ field: ‘Property__c’, value: currentId, operator: ‘=’}

        ]
    });
});

Glenn,  we do appreciate the community.  We look forward to seeing what you can come up with.  In the mean time,  a really cool expression of gratitude would be a app exchange review.  (And here I’m shamelessly asking… )   Here is the link:   https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009wyDjEAI

Thanks. 

done! you guys deserve it.

Hi guys, just came across this thread and it has been VERY useful. Great thread. I have one additional problem I can’t get my head around though, I’m trying to pull in the id property of an additional (related) field in the row that is being selected - this is the Account ID to which the object belongs. How could this be done? My snippet is pasted below, along with a screenshot:

var params = arguments[0];var step = params.step;
var $ = skuid.$;
var models = skuid.model.map();

var Ids = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){    
return item.row.Id;
});

var addingQuotes = skuid.model.getModel(‘BrokerQuote’);
var aircraftdata = skuid.model.getModel(‘database’);


$.each(Ids,function(){
var currentId = this;

var quoteRow = addingQuotes.createRow({
        additionalConditions: [
{ field: ‘stack__Aircraft__c’, value: currentId, operator: ‘=’, nameFieldValue: this.Name}

//need to add another field update here which says
//{ field: ‘stack__Operator__c’, value: ???, operator: ‘=’, nameFieldValue: this.Name}

        ]
    });
});

addingQuotes.save({

    callback: function(result){
       
        if (result.totalsuccess) {

        var aircraftData = skuid.model.getModel(‘database’);
        aircraftData.updateData();
        }
    }
});


(trying to pull in the id of the ‘Operator Name’ field below, ‘Tail Number’ is the record id which works fine.)