3

Is it possible to insert an array of objects using Remote Object Model?

Here's my code:

<apex:remoteObjects jsNamespace="romodel">
    <apex:remoteObjectModel name="Stock__c" jsShorthand="Stock" fields="Location__c,Product__c,LocationRetailPrice__c">
    </apex:remoteObjectModel>
</apex:remoteObjects>

And then in js:

stockObject = {
    Location__c: $j('.select-store-div option:selected').val(),
    Product__c: productsArray[key]['Id'],
    LocationRetailPrice__c: productsArray[key]['newPrice']
};
stockList.push(stockObject);
var stockCall = new romodel.Stock(stockList);
stockCall.create();

How can I edit my code to get this to work?

2 Answers 2

3

Ajax toolkit is a Javascript wrapper over SOAP based API .While ajax toolkit is a good option ,the problem with Ajax toolkit has been the no of API calls consumed .It will count against the API limit .

The other option is to use Javascript remoting .Please take a look at docs.

With javascript remoting you will need apex behind scenes and you can bulk insert records by passing arrays

public class AccCtrl {
   @RemoteAction
   global static list<AccCtrl.AccWrapper> getAccounts(list<Account> lstacc) { 
     ...
    insert lstacc;
    //for wrapper rec 
    return lstaccWrapper;
   }

   public class AccWrapper{
     public Account acc;
     public list<Contact> lstcnts ;
   }
 }

Apex @RemoteAction methods must be static and either global or public. Your method can take Apex primitives, collections, typed and generic sObjects, and user-defined Apex classes and interfaces as arguments. Generic sObjects must have an ID or sobjectType value to identify actual type.

9
  • Thanks for the response. That's how I originally attempted to resolve my problem. However after the bulk insert I had to return and display the stock ID together with some information about the related product sobject. I am worried as to how to match the returned stock object ID to the array of product sobjects that I already have in js on hand.
    – Arthlete
    Commented Feb 28, 2016 at 20:22
  • 1
    You can return apex wrapper object as well .Create a wrapper object that comprises more than one object and return them . Commented Feb 28, 2016 at 20:24
  • 2
    In these sorts of situations, I normally build some sort of composite key to make the match on. It generally works, assuming you have a combination that ensures uniqueness. Commented Feb 28, 2016 at 20:24
  • 1
    @Arthlete I have updated my answer to show you sample .Hope that helps you buddy Commented Feb 28, 2016 at 20:32
  • 2
    Yes, something like that - a key made up of Location_Product_RetailPrice. You'd create a javascript keyed array with that key and when the save results came back (however they come back) you can use those results to rebuild the key and retrieve the value from your javascript array. Commented Feb 28, 2016 at 20:41
3

I don't believe it's possible (can't see any list methods in the API). So you only option if you want to use Remote Objects is looping on create - not a great way to do it.

However, there is an alternative that has no more overhead than Remote Objects - the AJAX Toolkit.

To include the library, you do this:

<script src="../../soap/ajax/36.0/connection.js" type="text/javascript"/>
<script>
  sforce.connection.sessionId='{!GETSESSIONID()}';
  //rest of your code
</script>

To insert the object above, you would add:

var stocks = [];

for (var key=0; key<10; key++) {
  var stock = new sforce.SObject("Stock__c");
  stock.Location__c = $j('.select-store-div option:selected').val();
  stock.Product__c = productsArray[key]['Id'],
  stock.LocationRetailPrice__c = productsArray[key]['newPrice']
  stocks.push(stock);
}

var result = sforce.connection.create(stocks);

This also has limits - apparently you should try to limit it to 200 records or less with 6 fields each.

Docs here

2
  • Thanks for the response! Isn't the ajax toolkit a SOAP web service? Is the REST API going to be a better choice for what I want to do?
    – Arthlete
    Commented Feb 28, 2016 at 19:50
  • 1
    Like @Mohith says, this is a wrapper for SOAP and will cost you for API calls - it all depends on how much this will be used. I didn't suggest using RemoteActions, because you need a controller for that. I was trying to keep as close as possible to what you already had. So in the end it's all about real world use. Commented Feb 28, 2016 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .