I have a need to create a new record from an existing one on a Lightning page, but selecting specific fields only, not all (it's about 50/50 of used/unused fields), and adding some new values not on the existing one. This option to create will only apply to certain conditions being met on the existing record, so I can't use a standard button/link as that isn't conditional. The new record will be the same object type, but a different RecordType.

I was therefore thinking of an LWC which is displayed based on the condition being met on the record displayed, with a button to create the new record.

What is the best way to create the new record?

  1. Use an Apex class? I pass the current record Id to it and do a query to get all the necessary fields, create it and on successful create, return to display this new one?
  2. Use createRecord in the LWC? If I did this, would I have to import all the fields required off the original record in order to pass them into the new one?
  3. Navigate to the create page of the record using NavigationMixin.Navigate, passing the values from the existing one in the 'defaultFieldValues'? Again, how do I access these values from the existing one - by importing all these fields individually (ie. import STATUS_FIELD from "@salesforce/schema/myObject__c.Status__c"; for each field I need)?
  • 1
    It depends on what UX you want. Do you need the user to be able to review the creation and even change various field values before the record is created? Are the "new values" entered by hand or automatically calculated? Is the new record type selected by the user or automatically determined? Note that, server side, you have the option to clone an SObject with SObject.clone (you must have loaded all relevant fields' values first, of course) and you can detect this scenario in a trigger (where all fields are loaded for you) using SObject's getCloneSourceId to then run specific logic. – Phil W Aug 10 '20 at 7:07
  • Also worth noting that you don't have to import fields in order to use them in an LWC; you can use a string version of a field name like "Account.Name" instead of using import. This then means you can write code in an LWC that iterates the fields loaded for your existing SObject and determines their field names to then pass to either createRecord or NavigationMixin.Navigate. – Phil W Aug 10 '20 at 7:19
  • @PhilW yes the user will need to review the new record (after creation) and do some editing. Some of the original values will be editable, some not. RecordType is fixed, cannot be selected/changed. I did think about cloning but because I'd have to clear fields then for which I don't want the values(hence the 50/50), it would probably be easier to just set the values I do want? Which of the 3 options is the better one? – Irene Aug 11 '20 at 2:50
  • Does it need to work in a community? – Phil W Aug 11 '20 at 6:24
  • No, only for internal users – Irene Aug 11 '20 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.