lookupObj not returning record

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

lookupObj not returning record

Postby alasdairs » Fri Mar 22, 2024 9:32 am

Hi all,

I'm currently working with the lookupObj from the svyLookup module and I'm not sure what I'm doing wrong, but the onSelect function isn't letting me use the record param it's given. Whenever I application.ouput(record.contact_uuid) it returns <null>, but if I application.ouput(record) it returns the selected record. I've tried this twice now, with the current form I'm working on, and with the orderItemsTable in the cloud sample solution, both of which haven't worked in their specific ways.

Here is the current form im working on:
Code: Select all
function onSelect(record, values, lookup) {
   
   var contactFoundset = datasources.db.cpro.contacts.getFoundSet()
   contactFoundset.find()
   contactFoundset.contact_uuid = record.contact_uuid
   var found = contactFoundset.search()
   application.output(contactFoundset)
   application.output(record)
   if (found > 0) {
      var currentContact = contactFoundset.getRecord(1);
       if (record) {
           var newOrder = currentContact.contacts_to_orders.createRecord()
           if (!databaseManager.saveData(newOrder)) {
               plugins.dialogs.showErrorDialog('Error', 'Failed to create a new order for the selected contact.');
               return;
           }
           scopes.global.showForm(forms.orderEdit, newOrder);
       }
   }
}


Here is the orderItemsTable function:
Code: Select all
function onSelect(record, values, lookup){
   if (values && values.length) {
      foundset.pricelist_fk = values[0];
   }
   
   if(record) {
      record.forEach(function(rec){
         
         if (foundset.selectRecord(foundset.pricelist_fk,rec.pricelist_uuid)) {
            // increase quantity if product already in order
            foundset.quantity = foundset.quantity + 1;
            
         } else if(foundset.selectRecord(foundset.stock_fk,rec.stock_uuid)) {
            // increase quantity if product already in order
            foundset.quantity = foundset.quantity + 1;
            
         } else {
            // create a new order line for each product selected in lookup
            var newRec = foundset.getRecord(foundset.newRecord())
            newRec.quantity = 1;
            newRec.make = rec.make;
            newRec.model = rec.model;
            newRec.price = rec.price;
            newRec.serial_number = rec.serial_number
            updateStock(0, 1, newRec)
         }

      })
   }
   orderTotal()
}


For the order items table, I basically copied over the code from the cloud sample solution, but it hasnt seemed to work with anything I try.

Also I was wondering whether you can make the lookupObj.setMultiSelect(false) but have the form searchable at the same time?

Thanks!
Alasdair
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet

Re: lookupObj not returning record

Postby alasdairs » Fri Mar 22, 2024 11:53 am

Managed to get it working, I didn't realise you had to treat record as an array instead of a record.
Here's my new code for anyone interested.

Current form:
Code: Select all
function onSelect(record, values, lookup) {
   var contactUUID = record[0].contact_uuid
   var currentContact = foundset.getRecordByPk(contactUUID);
    if (record) {
        var newOrder = currentContact.contacts_to_orders.createRecord()
        if (!databaseManager.saveData(newOrder)) {
            plugins.dialogs.showErrorDialog('Error', 'Failed to create a new order for the selected contact.');
            return;
        }
        foundset.removeFoundSetFilterParam('type')
        scopes.global.showForm(forms.orderEdit, newOrder);
    }
}


orderItemsTable:
Code: Select all
function onSelectPricelist(records, values, lookup) {
   if(records) {
      for (var i = 0; i < records.length; i++) {
         var record = records[i];
         var fk = record.pricelist_uuid;
         foundset.find()
         foundset.pricelist_fk = fk
         var found = foundset.search()
         if (found) {
            var matchingRecord = foundset.getRecord(1);
            matchingRecord.quantity += 1;
            foundset.loadAllRecords()
         } else {
            // create a new order line for each product selected in lookup
            var newRec = foundset.getRecord(foundset.newRecord())
            newRec.quantity = 1;
            newRec.make = record.make;
            newRec.model = record.model;
            newRec.price = record.price;
            newRec.pricelist_fk = record.pricelist_uuid
            foundset.loadAllRecords()
            updateStock(0, 1, newRec)
         }
      }
   }
   orderTotal()
}

function onSelectStock(records, values, lookup) {
   if(records) {
      for (var i = 0; i < records.length; i++) {
         var record = records[i];
         var fk = record.stock_uuid;
         foundset.find()
         foundset.pricelist_fk = fk
         var found = foundset.search()
         if (found) {
            var matchingRecord = foundset.getRecord(1);
            matchingRecord.quantity += 1;
            foundset.loadAllRecords()
         } else {
            // create a new order line for each product selected in lookup
            var newRec = foundset.getRecord(foundset.newRecord())
            newRec.quantity = 1;
            newRec.make = record.make;
            newRec.model = record.model;
            newRec.price = record.serial_number;
            newRec.pricelist_fk = record.stock_uuid
            foundset.loadAllRecords()
            updateStock(0, 1, newRec)
         }
      }
   }
   orderTotal()
}
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 30 guests