How to insert a value from an unsaved model into a contains condition and not query the whole database if that value is blank

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Hello!

I'm sure there's a simple answer to this, I just can't quite put my fingers on it...

I have a condition:

Postcode CONTAINS ________ (value inserted via action from another model)

If the value inserted via an action is blank the whole database is queried and served up (understandably given the CONTAINS logic). What I'm looking to do is have a second condition that says if inserted value from another model is blank then don't employ the first condition. So (1 AND 2) oughta do it.

The problem is that the value being inserted via an action into the first condition is from an unsaved edit field on a different model - i.e. the field value has not yet been committed.

Any ideas would be much appreciated.

Cheers!

Louis
Photo of Louis Skelton

Louis Skelton

  • 4,878 Points 4k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Anna Wiersema

Anna Wiersema

  • 10,890 Points 10k badge 2x thumb
Hi Louis, I'm not sure if this is what you on Field from Another Model conditions there is a property (in the Value part)  where you can choose what happens "If No Row in Source Model" - try setting this to "Abort this Model's query"  rather than deactivating the condition.   Then you can have your model actions activate the condition (and set its value if you want) when the condition field is updated.

I did this having a "dummy" model with one unsaved row so this method may not work if your other model already has one or more saved rows.  

OOh, also, depending on when you load the other model, you can just not load the one you're filtering until the other model's field is updated and it activates the condition and queries the first model.

Was this helpful at all?  If this doesn't work for you, I think you can use javascript to decide when the condition is applied.
Photo of Louis Skelton

Louis Skelton

  • 4,878 Points 4k badge 2x thumb
Hi Anna! Thanks for getting in touch! I'm really stuck on this and it's holding me up.

Unfortunately I can't use the 'Field from Another Model' condition as the field in question is not saved to the database. Hopefully this will explain it a little better:





The second model retrieves the whole database because of the CONTAINS rule (which I need to use). I need a way of saying that if the unsaved field in the first model is blank, then don't use the condition on the second model. The field on the first model, however, isn't saved - so I need to see if the field in the UI is blank, rather then the committed record in Salesforce.

Sorry, I'm not feeling terribly eloquent at the moment - I'm not explaining this very well!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
I think you will get you desired behavior if you conditionally render the button so its not available until PostCode is not blank. 

The only way to get the opposite effect - clearing out the condition value and requerying the second model - will be with some Javascript.  Action framework value injenction can not currently enter a blank value. 
Photo of Louis Skelton

Louis Skelton

  • 4,878 Points 4k badge 2x thumb
Hi Rob,

Thanks for that - unfortunately I can't use conditional rendering on the button because I'm actually using several fields, not just the postcode.

The idea is that the user starts filling out some contact information, firstname, lastname, postcode, business name and then Skuid suggests existing Accounts and Contacts already in the system that contain some of those details.

I'll have a look at a javascript solution.

Cheers!
Louis
Photo of Louis Skelton

Louis Skelton

  • 4,878 Points 4k badge 2x thumb
In the end I got this working by stringing together a snippet like the below for each field I want to query on in a multiple action. I'm sure there's a more elegant way of doing this - but if it works, it works, right?

var params = arguments[0],
var newContact = skuid.model.getModel('Fundraising_Payment_New_Contact');
var newContactRow = newContact.getFirstRow();//or whatever row you want
var newContactRow_FirstName = newContactRow.FirstName;
//Define Suggested Contacts Model
var suggestedContacts = skuid.model.getModel('Fundraising_New_Suggested_Contacts');
//Find Suggested Contacts Model Condition
var suggestedContactsFirstNameCondition = suggestedContacts.getConditionByName('FirstName');
if (newContactRow_FirstName === '' || newContactRow_FirstName === undefined) {
    console.log('FirstName = BLANK VALUE');
    suggestedContacts.activateCondition(suggestedContactsFirstNameCondition);
    suggestedContacts.setCondition(suggestedContactsFirstNameCondition, 'SOME_RANDOM_TEXT_THE_CONTAINS_STATEMENT_WONT_FIND_ASDFH!£$(ASDFHJ12498asFAHSDFKASJD2');
} else {
    console.log('FirstName = ' + newContactRow_FirstName);
    suggestedContacts.activateCondition(suggestedContactsFirstNameCondition);
    suggestedContacts.setCondition(suggestedContactsFirstNameCondition, newContactRow_FirstName);
}
	$ = skuid.$;
//Define new Contact model and field values

This is the sort of thing I was trying to achieve:

Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Louis,

Nice work!

A couple thoughts to help optimize your script:

setCondition() is equivalent to "Activate and Set Value", so you don't have to call activateCondition() first.

Since you're in javascript already, it would probably be easier to just deactivate the condition if the field is blank instead of forcing it to try to find a random long string. I think it's deactivateCondition().
Photo of Louis Skelton

Louis Skelton

  • 4,878 Points 4k badge 2x thumb
Thanks for the setCondition() note - the fewer lines the merrier!

The issue I was having with deactivating the condition was that, regardless, the model is queried. And so deactivating the condition ended up querying the whole database if other fields hadn't been filled in to further limit the query through adjacent conditions. Having said that, I think I ended spending too long staring at this and can no longer see the wood for the trees! I'll have another crack at deactivating the conditions later, as it's certainly far more elegant than inserting 'SOME_RANDOM_TEXT_THE_CONTAINS_STATEMENT_WONT_FIND_ASDFH!£$(ASDFHJ12498asFAHSDFKASJD2'....
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
What about just setting a limit of 20 or so records to your model? Then it shouldn't matter too much if the whole db is queried?

Alternatively, can you change "regardless, the model is queried"?

Only query the model when there are values in at least one of your fields.
(Edited)
Photo of Louis Skelton

Louis Skelton

  • 4,878 Points 4k badge 2x thumb
Ah yes! Conditionally querying the model rather than conditionally inserting dummy text into the condition sounds like a much neater solution. I'll give it a go!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Thanks for the good interaction guys!  I love it when I see ideas flying around the community.