Autopopulate the lookup fields on the new button hit

  • 2
  • Question
  • Updated 8 months ago
  • Answered
I have a scenario in which on hitting the new button a form open up, there is a lookup field in the form and i need to autopopulate the form field on look up selection. Please share the views
Photo of Saurav Kumar

Saurav Kumar

  • 170 Points 100 badge 2x thumb
  • Sad

Posted 8 months ago

  • 2
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Saurav,

Add a model to find the record that you to use to set the lookup field for your popup.  In your model for the new record, add a condition that references the Id from the 'lookup field' model.

Here is a sample page that demonstrates this.

Thanks,

Bill

<skuidpage unsavedchangeswarning="yes" personalizationmode="server" useviewportmeta="true" showsidebar="true" showheader="true" tabtooverride="Contact">
    <models>
        <model id="SetAccountModel" limit="1" query="true" createrowifnonefound="false" datasource="salesforce" sobject="Account">
            <fields>
                <field id="RecordTypeId"/>
                <field id="Id"/>
                <field id="Name"/>
            </fields>
            <conditions>
                <condition type="fieldvalue" value="Gas" enclosevalueinquotes="true" field="Name" operator="contains"/>
            </conditions>
            <actions/>
        </model>
        <model id="Contact" limit="1" query="false" createrowifnonefound="true" datasource="salesforce" sobject="Contact">
            <fields>
                <field id="FirstName"/>
                <field id="LastName"/>
                <field id="AccountId"/>
                <field id="Account.Name"/>
            </fields>
            <conditions>
                <condition type="modelmerge" value="" field="AccountId" fieldtargetobjects="Account" operator="=" model="SetAccountModel" enclosevalueinquotes="true" mergefield="Id" novaluebehavior="deactivate"/>
            </conditions>
            <actions/>
        </model>
    </models>
    <components>
        <pagetitle model="Contact" uniqueid="sk-Q27-199">
            <maintitle>
                <template>New {{Model.label}}</template>
            </maintitle>
            <subtitle>
                <template>{{Model.labelPlural}}</template>
            </subtitle>
            <actions>
                <action type="savecancel" afterCancel="/{{Model.KeyPrefix}}/o" afterSave="/{{Id}}" rollbackonanyerror="true" uniqueid="sk-Q27-197"/>
            </actions>
        </pagetitle>
        <basicfieldeditor showsavecancel="false" showheader="true" model="Contact" mode="edit" uniqueid="sk-Q27-215">
            <columns>
                <column width="50%">
                    <sections>
                        <section title="Basics">
                            <fields>
                                <field id="FirstName" uniqueid="sk-Q27-207"/>
                                <field id="LastName" uniqueid="sk-Q27-210"/>
                            </fields>
                        </section>
                    </sections>
                </column>
                <column width="50%">
                    <sections>
                        <section title="Additional Info" collapsible="no">
                            <fields>
                                <field uniqueid="sk-Q27-254" id="AccountId"/>
                            </fields>
                        </section>
                    </sections>
                </column>
            </columns>
        </basicfieldeditor>
    </components>
    <resources>
        <labels/>
        <css/>
        <javascript/>
        <actionsequences uniqueid="sk-Q27-250"/>
    </resources>
    <styles>
        <styleitem type="background" bgtype="none"/>
    </styles>
</skuidpage>
Photo of Saurav Kumar

Saurav Kumar

  • 170 Points 100 badge 2x thumb
@Bill McCullough Thanks for your suggestion. I want to autopopulate the first name and last name on selection of Account look up as per your shared example. This is my requirement
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Saurav,

You just still need a model to query the account record.  You move the actions to load the account record to the model actions on your 'new record' model.  The actions run when the AccountId field is updated.  You load the model and then update the field(s) using fields from the Account model.

In this sample, I am updating the FirstName field on the new contact model using the Description field from Account.

Thanks,

Bill

<skuidpage personalizationmode="server" useviewportmeta="true" showsidebar="true" showheader="true" tabtooverride="Contact">
    <models>
        <model id="SelectedAccountModel" limit="1" query="false" createrowifnonefound="false" datasource="salesforce" sobject="Account">
            <fields>
                <field id="RecordTypeId"/>
                <field id="Id"/>
                <field id="Name"/>
                <field id="Description"/>
            </fields>
            <conditions>
                <condition type="fieldvalue" value="" enclosevalueinquotes="true" field="Id" state="filterableoff" inactive="true" name="AccountId"/>
            </conditions>
            <actions/>
        </model>
        <model id="Contact" limit="1" query="false" createrowifnonefound="true" datasource="salesforce" sobject="Contact">
            <fields>
                <field id="FirstName"/>
                <field id="LastName"/>
                <field id="AccountId"/>
                <field id="Account.Name"/>
            </fields>
            <conditions/>
            <actions>
                <action>
                    <actions>
                        <action type="setCondition" model="SelectedAccountModel" condition="AccountId" value="{{AccountId}}"/>
                        <action type="requeryModel" model="SelectedAccountModel" behavior="standard"/>
                        <action type="updateRow" fieldmodel="Contact" affectedrows="context" field="FirstName" enclosevalueinquotes="true" value="{{$Model.SelectedAccountModel.data.0.Description}}"/>
                    </actions>
                    <events>
                        <event>row.updated</event>
                    </events>
                    <fields>
                        <field>AccountId</field>
                    </fields>
                </action>
            </actions>
        </model>
    </models>
    <components>
        <pagetitle model="Contact" uniqueid="sk-Q27-199">
            <maintitle>
                <template>New {{Model.label}}</template>
            </maintitle>
            <subtitle>
                <template>{{Model.labelPlural}}</template>
            </subtitle>
            <actions>
                <action type="savecancel" afterCancel="/{{Model.KeyPrefix}}/o" afterSave="/{{Id}}" rollbackonanyerror="true" uniqueid="sk-Q27-197"/>
            </actions>
        </pagetitle>
        <basicfieldeditor showsavecancel="false" showheader="true" model="Contact" mode="edit" uniqueid="sk-Q27-215">
            <columns>
                <column width="50%">
                    <sections>
                        <section title="Select Account" collapsible="no">
                            <fields>
                                <field uniqueid="sk-Q27-254" id="AccountId"/>
                            </fields>
                        </section>
                    </sections>
                </column>
                <column width="50%">
                    <sections>
                        <section title="Contact" collapsible="no">
                            <fields>
                                <field id="FirstName" uniqueid="sk-Q27-207"/>
                                <field id="LastName" uniqueid="sk-Q27-210"/>
                            </fields>
                            <renderconditions logictype="and"/>
                        </section>
                    </sections>
                </column>
            </columns>
        </basicfieldeditor>
    </components>
    <resources>
        <labels/>
        <css/>
        <javascript/>
        <actionsequences uniqueid="sk-Q27-250"/>
    </resources>
    <styles>
        <styleitem type="background" bgtype="none"/>
    </styles>
</skuidpage>