Get Salesforce Id of new row in model

I’m creating a new row on an empty model in JavaScript, and would like to retrieve the Salesforce Id of the newly created row. Here’s what I’m doing, “m” is the model:

m.createRow(); m.save(); r = m.getFirstRow(); alert(r.Id);

The new row is being saved to the Salesforce object successfully. However the Id I get is “1”, which I gather is a Skuid temporary Id for the row. How can I get the real Salesforce Id? I need it to use in other new rows that will reference my new row. I’ve tried doing this: skuid.model.updateData([m]); after my save thinking it might retrieve the Salesforce Id, but it throws this error : “Uncaught Model ‘m’ has unsaved changes. To update this model’s data, you must first save or cancel the changes.”

Hi Ryan, You are very close! The trick here is that the save operation is asynchronous, so it will not be completed by the time that you call m.getFirstRow(). What you need to do is to wait until the save is finished, by utilizing a callback function:

m.createRow(); m.save({callback: function(result){ if (result.totalsuccess) { r = m.getFirstRow(); alert(r.Id); // should be a real SF Id now } else { // There was a problem. Let's see what went wrong. console.log(result.insertResults[0]); } }}); 

Thanks. Obvious in hindsight!

Hi Zach,

I also used m.createRow(); to create row in model.
And during create row i passed some value in fields as default value.

When after m.createRow(); the skuid table that is attached with model added a blank row. The default values is not visible in newly created record.

Can you please let me know how i can show values in skuid table whenever a new row is added using m.createRow() ? 

Rajendra, what version of Skuid are you running?

Hi Zach,

We are using “6.8.19” version.

Thanks

Hi Zach,

When i did add row on salesforce standard object (Contact) then row is added with specified value in fields in skuid table.

But when i used external object then its only adding row with blank value in column.

Here is xml of skuid page :






















{{Model.label}}

















































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

var Contact = skuid.model.getModel(‘Contact’);
Contact.emptyData();

Contact.createRow({
additionalConditions: [
{ field: ‘FirstName’, value:‘Rajendra Rathore’},

                        { field: 'LastName', value: 'lastname'}
                    ], doAppend:true 
                    
                    });
                    
                    var ResultModel = skuid.model.getModel('Result');

ResultModel.emptyData();

                    ResultModel.createRow({
                    additionalConditions: [
                        { field: 'StaffUserId__c', value:'Rajendra'},
                        
                        { field: 'LeadStaffEmail__c', value: 'test@gmail.com'}
                    ], doAppend:true 
                    
                    });</jsitem>
  </javascript>
  <css/>

Hi Zach,

So in case of external object is not possible to add row with default value in columns in a skuid table?

Thanks

Rajendra,

In the case of External Objects it will not be possible to create / update / delete any way at all, regardless of how you try to do it — Salesforce’s External Objects feature is currently universally Read-Only. Skuid is detecting that you are trying to update field values on an external object row, and those fields’ metadata are returning read-only, so Skuid is not applying your updates. 

You will not be able to use Skuid to write values to external data objects at this point because Salesforce does not support that.

Okey Zach! Thanks 

This isn’t working for me, somehow the save isn’t waiting to be completed. I’m trying to do this in a $.each function so maybe that’s why? It should be creating three rows in the DREFs1 model, and it does, but for my alert I just get 4, then 6, then 6 instead of a SF ID.

Here’s the code

var params = arguments[0], $ = skuid&#46;$, models = skuid&#46;model&#46;map(); &#47;&#47; grab ids of all selected rows &#47;&#47; model to add seleted rows from var conditionTemplates = skuid&#46;model&#46;getModel('ConditionTemplates'); var rows = conditionTemplates&#46;getRows(); &#47;&#47; model to add to var conditions = skuid&#46;model&#46;getModel('Conditions'); var drefs = skuid&#46;model&#46;getModel('DREFs1'); &#47;&#47;other models var oppModel = skuid&#46;model&#46;getModel('Opportunity'); var oppRow = oppModel&#46;getFirstRow(); var oppId = oppModel&#46;getFieldValue(oppRow,'Id'); &#47;&#47; for each selected row, add a new row $&#46;each(rows, function(){ &#47;&#47; get selected row data var templateRow = this; &#47;&#47; set variables for selected row field(s) var templateId = conditionTemplates&#46;getFieldValue(templateRow, 'Id'); var templateName = conditionTemplates&#46;getFieldValue(templateRow, 'Name'); var templateType = conditionTemplates&#46;getFieldValue(templateRow, 'Type__c'); var templateDescription = conditionTemplates&#46;getFieldValue(templateRow, 'Description__c'); var templateHelp = conditionTemplates&#46;getFieldValue(templateRow, 'Help__c'); var drefTemplate = conditionTemplates&#46;getFieldValue(templateRow, 'DocumentReferenceTemplate__r&#46;Id'); var drefTitle = conditionTemplates&#46;getFieldValue(templateRow, 'DocumentReferenceTemplate__r&#46;Title__c'); var drefParty = conditionTemplates&#46;getFieldValue(templateRow, 'DocumentReferenceTemplate__r&#46;ProvidingParty__c'); drefs&#46;createRow({ additionalConditions: [ {field: 'DocumentReferenceTemplate__c', value: drefTemplate, operator: '='}, {field: 'DocumentTitle__c', value: drefTitle, operator: '='}, {field: 'Opportunity__c', value: oppId, operator: '='}, {field: 'ProvidingParty__c', value: drefParty, operator: '='} ] }); drefs&#46;save({callback: function(result){ if (result&#46;totalsuccess) { d = drefs&#46;getFirstRow(); alert(d&#46;Id); var drefId = drefs&#46;getFieldValue(d, 'Id'); var conditionRow = conditions&#46;createRow({ additionalConditions: [ {field: 'ConditionTemplate__c', value: templateId, operator: '='}, {field: 'Name__c', value: templateName, operator: '='}, {field: 'Type__c', value: templateType, operator: '='}, {field: 'Description__c', value: templateDescription, operator: '='}, {field: 'Help__c', value: templateHelp, operator: '='}, {field: 'Opportunity__c', value: oppId, operator: '='}, {field: 'DocumentReference__c', value: drefId, operator: '='} ] }); } else { console&#46;log(result&#46;insertResults[0]); } }}); }); &#47;&#47; save updated model conditions&#46;save({callback: function(result){ if (result&#46;totalsuccess) { &#47;&#47;console&#46;log(quotelines); } else { console&#46;log(result&#46;insertResults); console&#46;log(result&#46;updateResults); console&#46;log(result&#46;deleteResults); } }}); 

Jack, I recommend NOT doing a .save() call within a loop (i.e. within your $.each) — I recommend refactoring your code to do this:

1. create all of your drefs, e.g. one createRow() for each dref you want to create
2. save all the new drefs you created — using just ONE call to drefs.save()
3. in the drefs.save() callback, iterate over result.insertResults and create condition rows in your conditions Model
4. Do ONE call to conditions.save()

Anytime you’re doing a .save() within an $.each(), you’re going to problems. Not only will your code be very very slow (because you’re doing a separate save call, which goes to the server, for each row you’re saving!), it’s going to be hard to debug and understand.

Thanks!

So, I wasn’t sure how to get at each of the Condition Templates again in the drefs.save results, since the Condition Templates aren’t related to the drefs after they are created.

My workaround was within the $.each to create all the drefs rows, then create all the conditions rows, then after creating all the rows and outside the $.each, save the drefs, then save the conditions within the success results of the drefs.

This matches the right drefs to the new conditions and doesn’t make any on-screen bold red errors, but it does throw a silent error and stops the saving. The result is I’ve got my table of Conditions with the right info but unsaved changes.

What I could do is manually hit Save on the table - instead in the action framework, I use an On-Error action with the Snippet that saves the Conditions model, and it all comes out perfect.

So the final code comes out like below, finished off with the action framework.

var params = arguments[0], $ = skuid.$, models = skuid.model.map(); // grab ids of all selected rows // model to add seleted rows from var conditionTemplates = skuid.model.getModel('ConditionTemplates'); var rows = conditionTemplates.getRows(); // model to add to var conditions = skuid.model.getModel('Conditions'); var drefs = skuid.model.getModel('DREFs1'); //other models var oppModel = skuid.model.getModel('Opportunity'); var oppRow = oppModel.getFirstRow(); var oppId = oppModel.getFieldValue(oppRow,'Id'); var oppTemplateGroup = oppModel.getFieldValue(oppRow,'Template\_Group\_\_c'); // for each selected row, add a new row $.each(rows, function(){ // get selected row data var templateRow = this; // set variables for selected row field(s) var templateId = conditionTemplates.getFieldValue(templateRow, 'Id'); var templateName = conditionTemplates.getFieldValue(templateRow, 'Name'); var templateType = conditionTemplates.getFieldValue(templateRow, 'Type\_\_c'); var templateDescription = conditionTemplates.getFieldValue(templateRow, 'Description\_\_c'); var templateHelp = conditionTemplates.getFieldValue(templateRow, 'Help\_\_c'); var drefTemplateId = conditionTemplates.getFieldValue(templateRow, 'DocumentReferenceTemplate\_\_r.Id'); var drefTitle = conditionTemplates.getFieldValue(templateRow, 'DocumentReferenceTemplate\_\_r.Title\_\_c'); var drefParty = conditionTemplates.getFieldValue(templateRow, 'DocumentReferenceTemplate\_\_r.ProvidingParty\_\_c'); var drefMan = conditionTemplates.getFieldValue(templateRow, 'DocumentReferenceTemplate\_\_r.Mandatory\_\_c'); var drefRow = drefs.createRow({ additionalConditions: [{field: 'DocumentReferenceTemplate\_\_c', value: drefTemplateId, operator: '='}, {field: 'DocumentTitle\_\_c', value: drefTitle, operator: '='}, {field: 'Opportunity\_\_c', value: oppId, operator: '='}, {field: 'ProvidingParty\_\_c', value: drefParty, operator: '='}, {field: 'Mandatory\_\_c', value: drefMan, operator: '='}, {field: 'TemplateGroup\_\_c', value: oppTemplateGroup, operator: '='}] }); var conditionRow = conditions.createRow({ additionalConditions: [{field: 'ConditionTemplate\_\_c', value: templateId, operator: '='}, {field: 'Name\_\_c', value: templateName, operator: '='}, {field: 'Type\_\_c', value: templateType, operator: '='}, {field: 'Description\_\_c', value: templateDescription, operator: '='}, {field: 'Help\_\_c', value: templateHelp, operator: '='}, {field: 'Opportunity\_\_c', value: oppId, operator: '='}, {field: 'DocumentReference\_\_c', value: drefRow.Id, operator: '='}] }); }); drefs.save({callback: function(result){ if (result.totalsuccess) { console.log('Success DREFs'); conditions.save({callback: function(result){ if (result.totalsuccess) { console.log('Success Conditions 1'); } else { console.log(result.insertResults); console.log(result.updateResults); console.log(result.deleteResults); } }}); } else { console.log(result.insertResults); } }});