What is the best way to have a lookup that supports creating a new record if one isn't found?

  • 9
  • Question
  • Updated 3 years ago
  • Answered
In my experience, one of the biggest annoyances for standard SF users is that when they use a lookup field to try to find a record, but don't find one, they can't create a new record right then - they have to click out of the lookup, go to another place, create a new record, then come back and use the lookup field again.

With Skuid, I know I can build a better lookup field (as a picklist or a type-ahead search), but what is the best way to allow a user to say "I didn't find one, so create a new one and put it in the lookup/search field"? I can easily put a button near the lookup field that functions as a popup in which to create the new record, but the user still has to save, close the popup, and re-search for that record again in the lookup field. Is there a simple way to put that record into the right lookup field right away on the editor I came from? Or do I have to use javascript to do this?

Better yet, I'd love to see this built-in, such as a config option on lookup fields for 'Show "Create New" option' with a pop-up field configuration. When selected, it would automatically pop up a page/editor and fill the field with the new record's ID when it is saved (and close the popup).
Photo of Peter Bender

Peter Bender, Champion

  • 6,276 Points 5k badge 2x thumb

Posted 5 years ago

  • 9
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,168 Points 20k badge 2x thumb
Peter, this is not the first time the idea has emerged. It would be possible to create a custom renderer that added the capabilities of allowing a popup to open where a new relatedrecord was created, but also added the lookup relationship in the original location.  And you are right - once created once, it should be abstracted so that it is available by configuration for any lookup field.   We'll consider this an idea for future development.  

Any others out there that would appreciate this feature? 
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,748 Points 5k badge 2x thumb
Yep, this would be a great standard feature. I vote infinity times.
Photo of Marshall Vaughn

Marshall Vaughn

  • 60 Points
I didn't even know you could vote that many times. Do I second this vote? Or do I infinity +1 it? [ edited because spelling ]
(Edited)
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,826 Points 5k badge 2x thumb
Got it working! 



So we have a Loan Officer field on Opportunity that looks up to a Contact. Our NewLoanOfficer model pulls from all contacts on the Account model, which is the one Account over the Opportunity. So, you'll need to find some way to limit the lookup field's model.



I included the LoanOfficer lookup field (in double brackets) on a Template Field in my Opportunity Field Editor. Then, I selected Show Custom Popup for edit mode behavior.



The custom popup includes a table with my NewLoanOfficer model, allowing inline record creation, showing save/cancel.

Note: I don't have a search box in the image because it gave me all sorts of trouble at first, but after getting everything else working, search works great now too. 

I removed all default actions, and adding a Row Action that runs multiple actions. 

The row action is called "Select this Loan Officer".

First it saves the model NewLoanOfficer, in case you just created a new one or edited any existing ones. (Whatever you set the table to, it will always open in Edit mode, I guess that's being forced since the pop-up loads from editing the template field.)

Next it updates the LoanOfficer field on Opportunity with {{{Id}}} triple brackets to include the selected value. 

Then it saves Opportunity model, and querys it to replace the field value with the chosen Loan Office, then it closes the popup. I had some trouble with choosing close the topmost popup, but it worked with close all popups. 
(Edited)
Photo of Dave

Dave

  • 5,630 Points 5k badge 2x thumb
Jack you are a genius!

Thank you very much , it worked like a charm!
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,826 Points 5k badge 2x thumb
Glad it worked Dave! Here's the page xml for my test page using Contacts and Accounts:

<skuidpage unsavedchangeswarning="yes" personalizationmode="server" showsidebar="true" showheader="true" tabtooverride="Opportunity" theme="Modern">    <models>
        <model id="Contacts" limit="20" query="false" createrowifnonefound="true" adapter="salesforce" type="" sobject="Contact">
            <fields>
                <field id="Name"/>
                <field id="Id"/>
                <field id="FirstName"/>
                <field id="LastName"/>
                <field id="AccountId"/>
                <field id="Account.Name"/>
            </fields>
            <conditions/>
            <actions/>
        </model>
        <model id="Accounts" limit="20" query="true" createrowifnonefound="false" adapter="salesforce" type="" sobject="Account">
            <fields>
                <field id="Id"/>
                <field id="Name"/>
            </fields>
            <conditions/>
            <actions/>
        </model>
        <model id="NewAccount" limit="1" query="false" createrowifnonefound="false" adapter="salesforce" type="" sobject="Account">
            <fields>
                <field id="Id"/>
                <field id="Name"/>
            </fields>
            <conditions/>
            <actions/>
        </model>
    </models>
    <components>
        <pagetitle uniqueid="sk-3eQKFv-312" model="Contacts">
            <actions>
                <action type="savecancel" label="New Button"/>
            </actions>
            <maintitle>New Contacts with existing or new Accounts</maintitle>
        </pagetitle>
        <skootable showconditions="true" showsavecancel="false" searchmethod="server" searchbox="true" showexportbuttons="false" pagesize="10" createrecords="true" model="Contacts" mode="edit" uniqueid="sk-3aeyot-89">
            <fields>
                <field id="FirstName" valuehalign="" type=""/>
                <field id="LastName" valuehalign="" type=""/>
                <field id="AccountId"/>
            </fields>
            <rowactions>
                <action type="multi" label="Add Account" icon="sk-icon-employment-info">
                    <actions>
                        <action type="showPopup">
                            <popup title="Select Account for Contact {{Name}}" width="90%">
                                <components>
                                    <skootable showconditions="true" showsavecancel="true" showerrorsinline="true" searchmethod="server" searchbox="true" showexportbuttons="false" pagesize="10" createrecords="true" model="Accounts" buttonposition="" mode="read" uniqueid="sk-3aider-593">
                                        <fields>
                                            <field id="Name" valuehalign="" type=""/>
                                            <field id="Id"/>
                                        </fields>
                                        <rowactions>
                                            <action type="edit"/>
                                            <action type="delete"/>
                                            <action type="multi" label="Select This Account" icon="sk-icon-import">
                                                <actions>
                                                    <action type="adoptRows" sourcemodel="Accounts" targetmodel="NewAccount"/>
                                                    <action type="closeTopmostPopup"/>
                                                </actions>
                                            </action>
                                        </rowactions>
                                        <massactions usefirstitemasdefault="true">
                                            <action type="massupdate"/>
                                            <action type="massdelete"/>
                                        </massactions>
                                        <views>
                                            <view type="standard"/>
                                        </views>
                                    </skootable>
                                </components>
                                <afterclose>
                                    <action type="updateRow" fieldmodel="Contacts" field="AccountId" enclosevalueinquotes="true" value="{{$Model.NewAccount.data.0.Id}}"/>
                                </afterclose>
                            </popup>
                        </action>
                        <action type="emptyModelData">
                            <models>
                                <model>NewAccount</model>
                            </models>
                        </action>
                    </actions>
                </action>
            </rowactions>
            <massactions usefirstitemasdefault="true"/>
            <views>
                <view type="standard"/>
            </views>
        </skootable>
    </components>
    <resources>
        <labels/>
        <css/>
        <javascript/>
    </resources>
    <styles>
        <styleitem type="background" bgtype="none"/>
    </styles>
</skuidpage>
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,826 Points 5k badge 2x thumb
One thing that isn't ideal, if you choose a contact for which you've already chosen an account, click the row action to open the popup of accounts, and then close the popup without choosing an account, it will clear out the account value on that contact. So you might do something like use the myns component to disable the x button on the popup window if you find that to be a problem. 
Photo of Dave

Dave

  • 5,630 Points 5k badge 2x thumb
Good catch, I see what u mean and could be a little issue.

What is that myns component you are mentioning?

Thx
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,826 Points 5k badge 2x thumb
the genius of Barry Schnell: https://community.skuid.com/skuid/topics/popup-controller-component-disable-x-escape-key-and-hook-di...

A lot of what he did got incorporate in the After-Close Actions now available on popups, but the disable X button is still pretty useful