Conditional rendering on add new using related record

  • 2
  • Question
  • Updated 4 years ago
  • Answered
I'm trying to use conditional rendering in a new record setting and I can't bend it to my will.

I have an "New Account" page with a NewAccount model that has a lookup field called Source that looks up to a related object called Source which has a field called Source Category.

In my field editor, the user selects a value for Source and I want to conditionally display another field editor based on the value of Source__r.Source_Category__c in the NewAccount model. But it just doesn't seem to do anything.

I'm on v4.8, which may be relevant.

Thanks!
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
  • ever hopeful

Posted 5 years ago

  • 2
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
You've run into an interesting edge case here Glenn.  Conditional rendering is listening to change events on the data in your model to affect the display of elements on the page,  but when you change a reference field,  the associated fields from the referenced object are not changed in the model until there is a save event.  (As best I can tell).  In fact, if you look at the model data in the console after you have changed the reference field,  the other fields from the referenced object are not even in the data (though they are still in the original data property).  A save event will make your conditional rendering effective,  but I'm guessing that's not all the sizzle you expected,  and a "save on the fly" action might cause unanticipated problems.  We'll talk about this more tomorrow... 
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
Thanks, that code does something different from what I'm looking for, I think... 
(This next part isn't directed to you in particular...)

I determined that part of my issue is I need to have the wizard "Defer Rendering of Step Contents" Checked.  If I don't, fields reused/displayed in Step 2 don't show values entered in Step1. (Presumably because they were rendered when the object was new/empty)

So I can now show the country selected in Step1 on Step2.

However, I still can't get render conditions to work cross-models.
Step 1 fills out the Student__c object.

Step 2 fills out a StudentProgram__c object which has a Master-Detail link to the Student__c object.

In Step 2, I have a condition "Student__r.Country__r.Name='DR'" 
This never works.
If I display "Student__r.Country__r.Name", nothing displays from the StudentProgram__c model.

I have the StudentProgram model conditioned/linked to the Student model with "Student__c = (NewStudent)(Id)"

..

From what I can tell, this is the condition that Rob discussed above as far as I can tell he never got back to discuss it.

My guess is since the StudentProgram row is new/empty, the condition to link it to the Student Model, isn't complete and it doesn't load the Student__r fields/

But how do you get around it?  I've seen numerous threads nearly this same issue, but no resolution.

I tried saving the Student__c object, but that doesn't do it.  Since the Render Conditions don't work cross-model, I need to get the Student__r object/fields loaded into the new/unsaved StudentProgram__c object.

Thanks.

 



Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
I'm assuming Student_Program__c is a child of Student__c, and you are probably using the wizard step with a StudentProgram model that has "create default row if model has none" checked. In that case the lookup relationship doesn't completely exist until the student program is saved. Meaning as you said above "Student__r" is blank, because the relationship has not yet been created. I would suggest basing the filter off of the Student__c model that you use in step one. if you save the student model between step 1 and 2 using JavaScript as I detailed above, you should be able to filter based off of the values in the saved student model.

One tip, when using JavaScript between steps in a wizard add these lines: 

var wizard = $('.nx-wizard, .wizard').data('object');
wizard.skooWizard('navigate','step2');

"step2" is the step id for your wizard step.
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
Thanks for sticking in there with me,

You are correct, StudentProgram__c is a child of Student__c and I have StudentProgram "create default row if model has none".

I've tried having it save the Student__c model in step1, but the that doesn't fill out the StudentProgram__c.Student__r (as I'd expect)

I'm trying to do conditional rendering, not filtering at this point. and, you can't (at least I can't) set it up to use a condition on a model different from the field editor's model.

On step2 I have a field editor for the StudentProgram__c object.  I'm wanting to put a Render Condition on a "Tuition" section, but the only available fields are from the StudentProgram__c object.  The field which gets back to the object from Step1, is as you've explained still 'empty' for the new/unsaved object.

It would seem that if I could just select the model for the condition field, ie. <NewStudentModel>.Country__r.Name='DR', then all would be golden.  I'm sure that is much simpler described than implemented.

I can't 'pre-save' the StudentProgram__c object in Javascript between , to 'load' the Student__r field due to empty required fields.  That could get messy too if they didn't complete the wizard, leaving half created records...

My current thought is to make a Country field in the StudentProgram__c object, and fill that at the step1->step2 transition via the Javascript NextStep button. Not too fond of that option for data-normalization purposes, but I don't think it would work as a general work around for cross-model Conditional Rendering.

Long term, I suppose they could also allow conditional rendering on a snippet that returns true/false.  That would let advanced Conditional Rendering options, but not cross-model condition complications. (That sounds like a good feature request to me..)

Thanks again



Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
OK I think were almost there, while you are correct, that a field editor SECTION can't be rendered based on a field from another model, a field editor itself CAN be rendered based on another model.



You probably want to add a new field editor on the Student_Program__c model but render it based on the Student__c model. 
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
Moshe,

THANK YOU, THANK YOU, THANK YOU!!!!
It worked like a charm.  Added a panel, made two different field editors, set that setting and boom, it worked fine.

I did not know that existed.  Guess I need to stop trying to implement and play around more.

Thank you very much again.

Seth


Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
I thought it was kind of like that. But I did some checking in the console and the related object field values seemed to change along with the UI without any save required.

After setting the field first:



then after changing it:



It remains a mystery.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You probably checked this already, but just double check that the model you are using to define the rendering conditions, is actually querying the necessary fields. Are "NewAccount" and "NewOrganisation" the same model?
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
Thanks Moshe. Yes, the model is in fact NewOganisation and it has all the requisite fields for the condition (i've been caught out on that before!). The mystery continues.
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
I'm still puzzled by this. I did some further testing and when I make a change to a reference field in the UI, the model shows the new reference field value as well as all the new values in fields on the looked up object. It's a mystery why the conditional rendering doesn't pick up on that change. I'm about to start coding this up, but thought I'd post here again as a last ditch declarative attempt. :)
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,470 Points 10k badge 2x thumb
Hi Glenn,

There is a bug in Skuid where conditional rendering based on a reference field does not work.  As of Skuid 5.1, this is fixed!  Should be out today or tomorrow.
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
Hey Ben ... I see v5.6 is out. Is this bit in that release? I couldn't quite tell from the release notes.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,470 Points 10k badge 2x thumb
Yeah, it should be in 5.6
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
Ben ... that's very exciting. I'll keep an eye out and give it a whirl.