Create New Row - Custom Snippet

  • 1
  • Problem
  • Updated 3 years ago
Hello,

I'm trying to do create new row snippet with the use of tutorial:
http://help.skuidify.com/m/components/l/109496-mass-create-records

and I would like to ask you for help. Thank you in advance. 

The goal is to select accounts or contact from one skuid table (mass action), select the corresponding campaing (built on custom object) and load the accounts or contacts to the object campaign membership (built on custom object). 

The corresponding model are as follow:
a) Accounts;
b) Sales Plan; 
c) Sales Plan Membership (with filterable default off field Account); 

Actions used are: 
a) Mass Action that triggers pop-up showing Sales Plans and Row Action on Sales Plan table that create new row in Sales Plan Membership, populating lookup Sales_Plan__c with {{Sales_Plan_c.Id]]
b) Run Custom Java Snippet in the form of: 

var params = arguments[0], $ = skuid.$;
var $ = skuid.$;
    var model = params.model,
        list = params.list,
        selectedItems = params.item ? [params.item] : list.getSelectedItems();
var models = skuid.model.map();
var Account = models.Accounts;
var SalesPlanMembership = models.SalesPlanMembership;
            
$.each(Account.detail,function(){
    var row = SalesPlanMembership.createRow({
        additionalConditions: [
            { field: 'Account__c', value: this.Id, operator: '=', nameFieldValue: this.Name}]
});
});

Could you please give me a hint? 

Best regards.
Photo of Jakub Dulny

Jakub Dulny

  • 200 Points 100 badge 2x thumb

Posted 3 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Jakub,

First thing I noticed is that $.each(Account.detail, function(){ ... } should be $.each(Account.data, function(){..}

Irvin
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Also, I am not familiar with this syntax: operator: '=', nameFieldValue: this.Name}.

Take a look here how to properly use the createRow() method:
http://help.skuidify.com/m/11720/l/12...
Photo of Jakub Dulny

Jakub Dulny

  • 200 Points 100 badge 2x thumb
Hello Irvin, thanks for you reply. I tried to follow you suggestion but nothing has been changed. 

The function looks like this:
$.each(Account.data,function()
{    var row = SalesPlanMembership.createRow({
        additionalConditions: [
            Account_Membership__c = Account.Id]});
});
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Try this:

var params = arguments[0];var $ = skuid.$;
var models = skuid.model.map();
//make sure the model name is spelled right. is it Account or Accounts?
var Account = models.Accounts;
//see what you get in the console
console.log(Account);
var SalesPlanMembership = models.SalesPlanMembership;
            
$.each(Account.data, function(i, row){
    var newRow = SalesPlanMembership.createRow({
        additionalConditions: [
            { field: 'Account_Membership__c', value: row.Id, operator: '=', nameFieldValue: row.Name }
        ]
});
});
Photo of Jakub Dulny

Jakub Dulny

  • 200 Points 100 badge 2x thumb
Hi Moshe, Sorry for the late reply, but I didn't noticed that you have replied to this post. Well I tried both ways (presented by you and by Irvin) and it didn't worked. 

The good news is that rows are created but Account_membership__c is not populated. 

The final version of the snippet is as follow (I even changed the names of the models in order to double check it): 

var params = arguments[0];
var $ = skuid.$;

    var model = params.model,
        list = params.list,
        selectedItems = params.item ? [params.item] : list.getSelectedItems();
var models = skuid.model.map();
var ParentAccounts = models.ParentAccounts;
console.log(ParentAccounts);
var Membership = models.Membership;
            
$.each(ParentAccounts.data, function(){
    var row = Membership.createRow({
        additionalConditions: [
            { field: 'Account_Membership__c', value: this.Id, operator: '=', nameFieldValue: this.Name}]
});
});

Do you see my mistake?
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You got to give some more detail about what exactly isn't working, try some console.log()'s and then check it out in the console (Ctrl + Shift + J in Chrome) and see what you're getting.

$.each(ParentAccounts.data, function(){
    console.log(this);
    var row = Membership.createRow({

        additionalConditions: [
            { field: 'Account_Membership__c', value: this.Id, operator: '=', nameFieldValue: this.Name}]
    });
    console.log(row);
});
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,470 Points 10k badge 2x thumb
The code looks ok to me. One thing to check would be that field names are case-sensitive in Javascript.  Make sure that 'Account_Membership__c' is actually the field name, and not something like 'Account_membership__c'
Photo of Jakub Dulny

Jakub Dulny

  • 200 Points 100 badge 2x thumb
Ben -> you're right that it should be m instead of M i.e. Account_membership__c. 

Moshe -> thank you for the attributes for console logs. 

I think we're close, but still the field is not populated. Look:

Object {Id: "4", Id15: "4", Account_membership__r: Object, Account_membership__c: "00124000008RYwWAAW"}Account_membership__c: "00124000008RYwWAAW"
Account_membership__r: Object
Name: "Test Account"
__proto__: Object
Id: "4"
Id15: "4"
__proto__: Object

The Name and Id ok. But it is not empty on this Account_membership__c. 
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Can you explain your last sentence? What exactly is wrong?
Photo of Jakub Dulny

Jakub Dulny

  • 200 Points 100 badge 2x thumb
Yes, of course. 

Please see:

Log:
Object {Id: "4", Id15: "4", Account_membership__r: Object, Account_membership__c: "00124000008RYwbAAG"}

  1. Account_membership__c: "00124000008RYwbAAG"
  2. Account_membership__r: Object
    1. Name: "New Test Account"
    2. __proto__: Object
  3. Id: "4"
  4. Id15: "4"
  5. __proto__: Object
Id of Account and Name of Account is ok. 

But please find that the newly created row and field Account_membership__c is empty. 
Photo of Jakub Dulny

Jakub Dulny

  • 200 Points 100 badge 2x thumb
Hello, maybe someone from you guys can help me with this ? :)