Issue with creating multiple records onsaving

We have a requirement to create multiple records on saving a Skuid page .

There are 3 models used in the page 

  1. Opportunity ( Store Opportunity details)
  2. Contact (List Contacts)
  3. OpportunityContactRole (Store Opportunity Contacts)
On entering and saving details
  1. Opportunity details entered should be saved to the Opportuntiy Model 
  2. Based on the # of contacts selected (multipickist field) that many records should get created in the OpportunityContactRole model and should get linked to the newly created Opportunity).

JS Snippet is being used for this 

var params = arguments[0],
$ = skuid.$;
//to retrieve a list of page models
skuid.model.map();

// obtain a reference to a specific model
var OpptyModel = skuid.$M(‘Opportunity’);
var OpptyContactModel = skuid.$M(‘OpportunityContactRole’);

//create a new Opportunity in our Oppty model
skuid.model.save([OpptyModel],{callback:function(result){
if(result.totalsuccess){
    alert(‘Oppty creation Success’);
    var firstOppty = OpptyModel.getFirstRow();
    //Get Contact Array - Based on how many contacts are selected 
    ContactArray = firstOppty.Contacts.split(’;’);
    alert(ContactArray.length);
    for(var j=0; j < ContactArray.length;j++)
    {
    // Create Oppty-Contact Role records     
        var row = OpptyContactModel.createRow({
        additionalConditions: [
        { field: ‘ContactId’, value:ContactArray[j] },
        { field: ‘Role’, value: ‘Influencer’},
        { field: ‘OpportunityId’, value:firstOppty.Id }
        
    ], doAppend: true
    });
    row.Save();
    var firstConOppty = OpptyContactModel.getFirstRow();
    //alert(firstConOppty.ContactId); 
    //alert(ContactArray[j]);
   }   
    
}else{
      alert('Oppty creation error: ’ + result.insertResults[0]);
}
}});

The Opportunity records gets created sucessfully and also able to loop through different contact ids .However not able to save records to opportunitycontactrole model.

Am i missing anything or using the wrong syntax ?

Thanks

The main problem I see is that you’re calling row.Save() — you should call OpptyContactModel.save() at the end of the loop over ContactArray.

Here’s a revised version I put together that uses Promises instead of callbacks, to keep things a bit cleaner

var params = arguments[0],<br> $ = skuid.$;<br>//to retrieve a list of page models<br>skuid.model.map();<br>// obtain a reference to a specific model<br>var OpptyModel = skuid.$M('Opportunity');<br>var OpptyContactModel = skuid.$M('OpportunityContactRole');<br>//create a new Opportunity in our Oppty model<br>OpptyModel.save().then(function(result){<br>if(result.totalsuccess){<br>alert('Oppty creation Success');<br>var firstOppty = OpptyModel.getFirstRow();<br>//Get Contact Array - Based on how many contacts are selected <br>var ContactArray = firstOppty.Contacts.split(';');<br>alert(ContactArray.length);<br>for(var j=0; j &lt; ContactArray.length;j++) {<br>// Create Oppty-Contact Role records <br> OpptyContactModel.createRow({<br> additionalConditions: [<br> { field: 'ContactId', value: ContactArray[j] },<br> { field: 'Role', value: 'Influencer'},<br> { field: 'OpportunityId', value:firstOppty.Id }<br> ],<br> doAppend: true<br> });<br> }<br><b>return OpptyContactModel.save();</b><br> } else{<br>throw new Error('Oppty creation error: ' + result.insertResults[0]);<br> }<br>}).then(function(result) {<br>if (result.totalsuccess) {<br>alert("OpptyContactRole creation success!");<br> } else {<br>throw new Error('Oppty contact role creation error: ' + result.insertResults[0]);<br> }<br>}).catch(function(err) {<br>console.error(err.message);<br>});