[SalesForce] Pre-populate Lookup field when creating a new record with Lookup record’s name

Not sure if this is possible, but I'd like to be able to prepopulate the value in a lookup field when creating a new record.

Specifically:

I have a lookup field to a custom Office object on my Opportunity object.
On my User object, I have a picklist that correspond to the names of each record of the Office object.
When I create a new Opportunity, I would like for the Office field on my new Opp to be prepopulated with the Office that is named on the running user's record.

E.g. User record has office picklist of "New York", Branch record with name "New York" exists in system. User creates a new opp and the "New York" field is already populated in the Branch lookup field.

I tried using "Lightning URL hacking" with a custom URL button using something like:
/lightning/o/Opportunity/new?defaultFieldValues=Office__c=New York, but naturally passing a string here doesn't work and causes the field to not appear in the new record UI and causes an error upon saving.

Any help or confirmation that this is even possible is appreciated. Note: I want this to exist as a button on the Opportunity list view.

Best Answer

For this i would suggest the new lwc headless action where you can have apex controller to query for the id of the Office record based on the Name from the user record. Then fire from the invoke method navigation to a new record page with a predefined Office field on Opportunity.

Posting the code for the navigation as it could be hard to find.

{
    type: 'standard__objectPage',
    attributes: {
        objectApiName: 'Account',
        actionName: 'new'
    },
    state: {
        defaultFieldValues = 'AccountNumber=ACXXXX,CustomCheckbox__c=true,Name=Salesforce%2C%20%231%3DCRM,NumberOfEmployees=35000,OwnerId=005XXXXXXXXXXXXXXX',
        nooverride: '1'
    }
}
Related Topic