Default value for reference field based on condition

  • 1
  • Problem
  • Updated 4 years ago
  • Acknowledged
Hello -

I have a model that is marked as "Create Default Row if Model Has None" and has a condition applied to a reference/lookup field.  When the condition fails, a new row is created in the model and it contains the object id for the field where the condition is set.  So far so good...

The problem that I am having is that I need properties of the reference field in order to conditionally render certain portions of the screen.  When the record already exists, the appropriate fields get pulled down for the reference field and conditional rendering works as expected.

Unfortunately, when a new row is created in the model, the additional properties of the reference field are not retrieved on page load so my conditional rendering does not work properly.

Here is sample:

System Model has property Color
Order Model has property System (reference to System Object)

When Order already exists, Color is available via OrderModel.System__r.Color

When Order is a new record, OrderModel.System__c gets the right value based on the condition but OrderModel.System__r.Color is not retrieved from server.  I did specify Color is a retrieved field on System Model and on Order Model.

What I have tried so far:

1) Setting field.options.returnFields but this doesn't seem to work on initial page load.  Only seems to work when value in the conditioned field subsequently changes.
2) Creating a condition on OrderModel.System__r.Color_c setting it to SystemModel.Color__c but skuid seems to never set "__r.XXX" fields programmatically or in conditions.

This is a blocking issue for us currently so appreciate a quick response on this one.

Thank you!






Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb

Posted 4 years ago

  • 1
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
Barry,

You will have to either set the related field values with a model Condition or set the values with our Javascript API. Skuid should let you do this for fields that are writeable (i.e. CreatedBy.Name won't work, but Owner.Name will). See the following XML which demonstrates this using the Contact object:

<skuidpage showsidebar="true" showheader="true" tabtooverride="Contact">
   <models>
      <model id="Contact" limit="1" query="false" createrowifnonefound="true" sobject="Contact">
         <fields>
            <field id="FirstName"/>
            <field id="LastName"/>
            <field id="CreatedById"/>
            <field id="CreatedBy.Name"/>
            <field id="OwnerId"/>
            <field id="Owner.Name"/>
            <field id="Name"/>
            <field id="AssistantName"/>
            <field id="AccountId"/>
            <field id="Account.Name"/>
         </fields>
         <conditions>
            <condition type="fieldvalue" value="Jacques" enclosevalueinquotes="true" field="FirstName"/>
            <condition type="fieldvalue" value="Cousteau" enclosevalueinquotes="true" field="LastName"/>
            <condition type="fieldvalue" value="123abc" enclosevalueinquotes="true" field="CreatedById"/>
            <condition type="fieldvalue" value="Steve Zissou" enclosevalueinquotes="true" field="CreatedBy.Name"/>
            <condition type="fieldvalue" value="456def" enclosevalueinquotes="true" field="OwnerId"/>
            <condition type="fieldvalue" value="Steve Zissou" enclosevalueinquotes="true" field="Owner.Name"/>
         </conditions>
      </model>
   </models>
   <components>
      <pagetitle model="Contact">
         <maintitle>
            <template>{{FirstName}} {{LastName}}</template>
         </maintitle>
         <subtitle>
            <template>{{Model.label}}</template>
         </subtitle>
         <actions/>
      </pagetitle>
      <basicfieldeditor showsavecancel="false" showheader="true" model="Contact" mode="read">
         <columns>
            <column width="50%">
               <sections>
                  <section title="Basics">
                     <fields>
                        <field id="FirstName"/>
                        <field id="LastName"/>
                        <field id="Name"/>
                        <field id="AssistantName"/>
                        <field id="AccountId" displaytemplate=""/>
                     </fields>
                  </section>
               </sections>
            </column>
            <column width="50%">
               <sections>
                  <section title="System Info">
                     <fields>
                        <field id="CreatedById" displaytemplate=""/>
                        <field id="OwnerId"/>
                     </fields>
                  </section>
               </sections>
            </column>
         </columns>
      </basicfieldeditor>
   </components>
   <resources>
      <labels/>
      <css/>
      <javascript>
         <jsitem location="inline" name="newInlineJS" cachelocation="false" url="">(function(skuid){
    var $ = skuid.$;
    $(function(){
        var myModel = skuid.model.getModel('Contact'),
            myRow = myModel.getFirstRow();
            
        myModel.updateRow(myRow,{
            AssistantName : 'Klaus',
            AccountId : '1a2b3c',
            Account : {Name : 'Deep Dives'}
        });
    });
})(skuid);</jsitem>
      </javascript>
   </resources>
</skuidpage>


Note that the Conditions on that model are just setting values. To get Salesforce formula fields to evaluate or anything like that, you actually have to query Salesforce (this is why the Contact.Name field in the example should be blank, even though you've got values for "First Name" and "Last Name").

Of course, Conditions affect legitimate queries, so you may want to use our Javascript API to manually manipulate the model data instead (see updateRow and updateRows in the skuid.model.Model docs). I'm populating the AssistantName, AccountId and Account.Name fields in this example this way. It's not completely declarative, but it allows you to do things like check to see if the model found a row before creating an empty row (skuid.model.Model.createRow) and setting values.

If this doesn't shed some light on the situation, could you paste in your XML or grant login access so we can take a look? Thanks.
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
Thanks J.

Unfortunately, the scenario you capture above is slightly different than mine, sorry for the confusion.  The way you describe above works as expected.  However, in my case, the "value" in the condition is coming from another model instead of straight text.

I'm going to try the API approach to see if that will works.  

In the meantime, I've granted login access so you can see the details behind what I'm attempting.  The page you want to look at is SampleTestSysCompanyChangeEdit.

Thank you for your continued support!