setCondition does not return the filtered data

  • 1
  • Question
  • Updated 2 years ago
  • Answered
  • (Edited)
Hi,
I was trying to deactivate some conditions and keep 1 live condition active and bring some data based on that,but the below code doen't query the new data rows as I expected.

I still can see in the console, the query is modified as I expected though the getFirstRow() returns nothing

Please help, Thanks in advance.

var EventRelationModel = skuid.model.getModel('EventRelation');
var EventRelationsData = EventRelationModel.getFirstRow();
var WhoModel = skuid.model.getModel('Who');
var WhoData = WhoModel.getFirstRow();
if((!WhoData) && (existingEvent.WhatId === null || existingEvent.WhatId === 'undefined' || existingEvent.WhatId === '')) {

$.each(WhoModel.conditions,function(i,condition){

if (condition.name && condition.name !== 'whoFilter4'){
WhoModel.deactivateCondition(condition,false);
}
});

var whoFilter4 = WhoModel.getConditionByName('whoFilter4',false);
WhoModel.setCondition(whoFilter4, EventRelationsData.RelationId);
WhoModel.updateData();

WhoData = WhoModel.getFirstRow();

// THIS PRINTS UNDEFINED
console.log('TEST DATA ------ ');
console.log(JSON.stringify(WhoData));
console.log('TEST ------ ');
EventModel.updateRow(
existingEvent,{ WhatId : WhoData.AccountId}
);
}
Photo of Hasantha Liyanage

Hasantha Liyanage

  • 1,446 Points 1k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Hasantha Liyanage

Hasantha Liyanage

  • 1,446 Points 1k badge 2x thumb
Hi All, found the issue here, due to asynchronous Skuid Model operation  I need to perform the updateRow withing the call back of the updateData() function;
see the solution below;

Cheers!
var EventRelationsData = EventRelationModel.getFirstRow();	    
var WhoModel = skuid.model.getModel('Who');
var WhoData = WhoModel.getFirstRow();


if((!WhoData) && (existingEvent.WhatId === null || existingEvent.WhatId === 'undefined' || existingEvent.WhatId === '')) {

    var filter2 = WhoModel.getConditionByName('whoFilter2',false);
    var filter3 = WhoModel.getConditionByName('whoFilter3',false);
    WhoModel.deactivateCondition(filter3,false);
    var filter4 = WhoModel.getConditionByName('whoFilter4',false);


    WhoModel.setCondition(filter2, EventRelationsData.RelationId);
    WhoModel.setCondition(filter4, EventRelationsData.RelationId);
    
    WhoModel.updateData(function() {
        WhoData = WhoModel.getFirstRow();
        EventModel.updateRow(
            existingEvent,{ WhatId : WhoData.AccountId}
        );
    });

}
(Edited)
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
Thanks! Just curious, how did you figure out that the condition setting function was performed asynchronously?
Photo of Velvel Marasow

Velvel Marasow

  • 172 Points 100 badge 2x thumb
All network requests are naturally Async. The issue here was activating the condition by calling updateData() which fetches the data for the model (using the network) with the given condition, that's why the fix was to put the updateRow() inside the callback from updateData().
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
Ah thanks, Velvel. I was setting a condition inside of a snippet and trying to update the model outside of the snippet, using declarative actions, right after.
When updating model data outside of the snippet context, the query conditions weren't taken into account.

I didn't add the condition to the callback function, but so long as the update was happening inside of the snippet context after the conditions were set, the query took into account the set conditions.