Conditional Custom Object, Lookup a Row or Create new Row

  • 1
  • Question
  • Updated 2 years ago
  • Answered
I'm looking to find out how to do my conditions(i think) I have two models 1 is standard Opportunities  and other is a custom object, i'm am displaying data from the Opportunities and by default I am creating a new record for my custom object when saving data. What I am looking/wanting to do is by default with my custom object created a new record BUT if there is a related record that matches the Opportunity then display that row from the custom object with the data contained. Does this make since? FYI is the public force.com page. 
Photo of Brandon

Brandon

  • 110 Points 100 badge 2x thumb

Posted 2 years ago

  • 1
Photo of Karen Waldschmitt

Karen Waldschmitt, Official Rep

  • 8,240 Points 5k badge 2x thumb
Brandon~

If I understand correctly, you will need 3 models: 1 for the opportunity object and 2 for the custom object.
  • One of the custom object model will show the related record, if there is any. You will need a condition on that model where the opportunity ID equals the opportunity ID from the opportunity model
  • The second custom object model will be to create a new record. 
You would then use conditional rendering to display and hide the appropriate parts of the page.

Hope that helps!
Karen 
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb

Hello Brandon -

Possibly Karen is misunderstanding or I am misunderstanding your situation but you should not need 3 models.  Having 3 models for this situation (again, assuming I'm understanding correctly) would create several issues including the fact that you'd have to duplicate every component that uses your custom object with the only difference between each pair being the model used and then use conditional rendering to show an hide the correct one.  This would create a maintenance nightmare especially if your component configurations are complex.

In order to accomplish what you are after, the key is using the "Field form Another model" condition and also the "load model data" and "create default row if none" for the custom object model.

Here's a breakdown:

1) Create Opportunity Model as normal
2) Create Custom Object Model applying the following
a) Properties
Load Model data on page load: true
Create default row if Model has none: true
b) Conditions
FIeld: OpportunityId (or whichever field in your custom object associates it to the Opportunity)
Operator: =
Value: Field from another model, using Source model of Account, Source Field of Id
State: Always On

You did mention that you create the row when you save so if that is the case, things get a little more tricky as you need to use the action framework.  Even in this situation, however, the model configuration would be similar, you just wouldn't want "load model data" and instead, in the action framework, you would query the custom object model first, then do the rest of the steps (e.g. set field values, save, etc.)

Here's a sample page that uses Account and Contact replacing your Custom Object for Contact but the process is identical.  If you preview this page with an Account that has Contacts, you will see them table on the page.  If you preview this page for an account that has no contacts, a new record will automatically be created and you will see that on the page in the table.

Hope this helps!

<skuidpage unsavedchangeswarning="yes" personalizationmode="server" showsidebar="true" showheader="true" tabtooverride="Account">
    <models>
        <model id="Account" limit="1" query="true" createrowifnonefound="false" adapter="salesforce" service="salesforce" sobject="Account">
            <fields>
                <field id="Name"/>
                <field id="CreatedDate"/>
            </fields>
            <conditions>
                <condition type="param" enclosevalueinquotes="true" operator="=" field="Id" value="id"/>
            </conditions>
            <actions/>
        </model>
        <model id="Contact" limit="20" query="true" createrowifnonefound="true" adapter="salesforce" type="" sobject="Contact">
            <fields>
                <field id="Name"/>
                <field id="FirstName"/>
                <field id="LastName"/>
            </fields>
            <conditions>
                <condition type="modelmerge" value="" field="AccountId" operator="=" model="Account" enclosevalueinquotes="true" mergefield="Id" novaluebehavior="noquery" state=""/>
            </conditions>
            <actions/>
        </model>
    </models>
    <components>
        <pagetitle model="Account" uniqueid="sk-1FwzjQ-76">
            <maintitle>
                <template>{{Name}}</template>
            </maintitle>
            <subtitle>
                <template>{{Model.label}}</template>
            </subtitle>
            <actions>
                <action type="savecancel" window="self">
                    <models>
                        <model>Contact</model>
                    </models>
                </action>
            </actions>
        </pagetitle>
        <basicfieldeditor showsavecancel="false" showheader="true" model="Account" mode="read" uniqueid="sk-1FwzjR-77">
            <columns>
                <column width="50%">
                    <sections>
                        <section title="Basics">
                            <fields>
                                <field id="Name"/>
                            </fields>
                        </section>
                    </sections>
                </column>
                <column width="50%">
                    <sections>
                        <section title="System Info">
                            <fields>
                                <field id="CreatedDate"/>
                            </fields>
                        </section>
                    </sections>
                </column>
            </columns>
        </basicfieldeditor>
        <skootable showconditions="true" showsavecancel="false" showerrorsinline="true" searchmethod="server" searchbox="false" showexportbuttons="false" pagesize="10" createrecords="true" model="Contact" buttonposition="" mode="edit" uniqueid="sk-1Fxp6l-122" emptysearchbehavior="query">
            <fields>
                <field id="FirstName" valuehalign="" type=""/>
                <field id="LastName" valuehalign="" type=""/>
            </fields>
            <rowactions>
                <action type="edit"/>
                <action type="delete"/>
            </rowactions>
            <massactions usefirstitemasdefault="true">
                <action type="massupdate"/>
                <action type="massdelete"/>
            </massactions>
            <views>
                <view type="standard"/>
            </views>
            <searchfields/>
        </skootable>
    </components>
    <resources>
        <labels/>
        <css/>
        <javascript/>
    </resources>
    <styles>
        <styleitem type="background" bgtype="none"/>
    </styles>
</skuidpage>