JavaScript snippet export data from 2 models

Jesus Gonzalez
Jesus Gonzalez ✭✭✭
edited November 26, 2019 in Questions
Hello!

I am trying to export data from 2 related models into 1 csv file.
I am using the exportData() function but I understand that it accepts only 1 model so I wondered if I should use a third Ui-Only model to populate the data before.

A test version of my current snippet would be:

var $ = skuid.$;
var model1 = skuid.model.getModel('Model 1');
var model2 = skuid.model.getModel('Model 2');

var fields = [
    model1.getField('Id'),
    model1.getField('Field1'),
    model1.getField('Field2'),
    **Here is where I would like to include a new column using a field from model2 (related to model 1 via a lookup field)**
];

model1.exportData({
    fileName: 'MyExportFile.csv',
    fields: fields
});

Many thanks,
Jesus

Comments

  • Zach McElrath
    edited November 21, 2019
    Jesus, one way to accomplish this would be to have a 3rd Model which contains fields shared by Model 1 and Model 2, and then to use the "Adopt Rows" action to grab all the data rows from Model 1 and Model 2 and adopt those rows into Model 3 --- and then export Model 3. To do this you'll need to ensure that all of the fields that are in Model 1 and Model 2 which you want to export are included in Model 3.
  • Jesus Gonzalez
    Jesus Gonzalez ✭✭✭
    edited November 21, 2019
    Thank you for your reply Zach,

    The 2 models make reference to different objects so I guess that instead of adoptRow I should use the createRow action, right?

    In that case I still have the question of how to assign a value from a different model to a field while creating a new row. Any suggestions?

    var $ = skuid.$;
    var model1 = skuid.model.getModel('Model_1'); --> Source model 1 (object A)
    var model2 = skuid.model.getModel('Model_2'); --> Source model 2 (object B, related to object A via lookup field)
    var model3 = skuid.model.getModel('Model_3'); --> Target model used in the export

    $.each(model1.data,function(i,row) {
        var newRow = model3.createRow({
            additionalConditions:[
            { field: 'Name', value: row.Name__c, operator: '=', nameFieldValue: 'Name__c'},
            { field: 'Country', value: row.Country__c, operator: '=', nameFieldValue: 'Country__c'}
            { field: 'Year', value: Here is where I would like to query a value from model 2, something like SELECT Year__c FROM model2 WHERE Id = row.Id}
            ],
        });
    });

    Many thanks!
    Jesus
  • Jesus Gonzalez
    Jesus Gonzalez ✭✭✭
    edited November 21, 2019
    Hello,

    I have figured out how to do it: setting a filterable condition in the model 2 and assigning the value of each row of the model 1 within the loop. Something like this:

    var $ = skuid.$;
    var model1 = skuid.model.getModel('Model_1');
    var model2 = skuid.model.getModel('Model_2');
    var model3 = skuid.model.getModel('Model_3');
    var myCondition = model2.getConditionByName('Condition_1');

    $.each(model1.data,function(i,row) {
        model2.setCondition(myCondition, row.Id);
        model2.updateData();
        var year = model2.getFirstRow();
        var newRow = model3.createRow({
            additionalConditions:[
            { field: 'Name', value: row.Name__c, operator: '=', nameFieldValue: 'Name__c'},
            { field: 'Country', value: row.Country__c, operator: '=', nameFieldValue: 'Country__c'}
            { field: 'Year', value: year.Year__c, operator: '=', nameFieldValue: 'Year__c'}
            ],
        });
    });

    Regards,
    Jesus
  • Germany3
    Germany3 🛠️ 

    Hey @Jesus Gonzalez !

    Skuid has implemented an easier way to export data as well as added other features and enhancements in the new Chicago release which is now available on the Skuid Releases page. Best practices for upgrading can be found in Upgrading Skuid on Salesforce.  As a reminder, Salesforce does NOT allow reverting back to prior versions of managed packages. Skuid always recommends installing new versions in a non-business critical sandbox environment to test all mission-critical functionality before installing into a production environment.

    We also recommend that you update out-of-date themes and design systems after you upgrade. Please let us know if you continue to encounter any problems with this issue after upgrading.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!