Search code examples
javascriptappconkit

How do I copy a foundset?


So I have the following issue: I have a Foundset with a bunch of records, and I'd like to (deep) copy them to a new location, including all values but without the IDs. What's the best way to do this?


Solution

  • If you want to do an deep copy of an foundset you need the follow steps:

    1. Create an empty foundset over retrieveOrCreateFoundset()
    2. iterate over all records of the foundset, that should copied to a new one
    3. get the dataProviders with something like rec.dataprovider()
    4. get the value of each dataprovider on a record rec.value()
    5. be sure that the uuid dataprovider isnt copy to the new Record
    6. set the values to a the new record over the dataproviders
    7. persist the changes

    Full code example would be:

    var vMobileController = plugins.iBizClientWebService.mobileController();
    var vFoundset = vMobileController.currentFoundset();
    var vDatabaseManager = vMobileController.dataManager();
    var copyFoundSet = vDatabaseManager.retrieveOrCreateFoundset("<datasource>:<label>");
     for (var index = 0; index < vFoundset.size(); index++) {
         var rec = vFoundset.record(index);
         var loc = copyFoundSet.newRecord();
            var newRecord = copyFoundSet.record(loc);            
            var allDataproviders = rec.dataprovider();
            for(var i=0;i<allDataproviders.length;i++)
            {
                var dataProvider = allDataproviders[i];
                var dataValue = rec.value(dataProvider);
                if(dataProvider != "attribute_id")
                {    
                    newRecord.setValue(dataProvider, dataValue);
    
                }
            }
    }
    
     copyFoundSet.saveData();