Creating Parent Object when creating child Objects

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Let's Consider Accounts and Contacts.
I have a table which shows a list of Contacts. When a new row for Contact is created then an user has an option to either select an existing account or add new account details. And on save account should get saved and the contact should get linked with this Account.

Can you please suggest how can I achieve this?
Photo of Sneha

Sneha

  • 624 Points 500 badge 2x thumb

Posted 3 years ago

  • 1
Photo of Skuidward Tentacles (Raymond)

Skuidward Tentacles (Raymond), Champion

  • 17,224 Points 10k badge 2x thumb
I've done something similar. My solution gets a little complicated.....
First, I would disable inline new row creation. You will want the user to create new rows in a pop up. Add a global action to your table (or a page title button or navigation component near your table) to add new row.
-Have the actions of the button open a pop up window. put a page include in the pop up to the page you will now create:
-create a new page to create a new account
1) add a field editor for your contacts object that includes all the fields you want the user to populate
2) add a tab set above the field editor and set it to picklist instead of tabs on top.
- The tab set has two tabs named : 1) Add existing Account 2) Create new Account
- In the first tab add a field editor based on your contact model with the look up to Accounts (this allows the user to choose an account)
- in the second tab, have a field editor based on the accounts model with all the fields that you want the user to populate for a new account.

-Now the trick is to have two buttons at the top of your page. Each button has a different action sequence. Name the first one "Save" and the second one "Create Account And Save"
- the first button simply saves the Contact model. Easy Squeezy
- the second one saves the account model, then updates field on rows on the contact model where the account relationship field equals the Id of the account model. Then it saves the contact model

You can conditionally render the bottons based on whether or not a specific field on the account model is blank. If the user picks an existing account, all account model fields will be blank. If the user creates an account the the fields on the account model will have values. Render the save button if a field from the account model is blank. Render the the "create account and save" button if the field on the account model is not blank.

Hope this gets you started... Good luck!
Photo of Sneha

Sneha

  • 624 Points 500 badge 2x thumb
Thanks Raymond.
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Thanks for writing this up Raymond.  That is a slick solution.