function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jacob Elliott 1Jacob Elliott 1 

Pre-populate a text/input field based on another field when user creates a new record

Hello all, I'm extremely new to Apex, so please bear with me and forgive me for any confusion. :-) 

I have a custom object calledProject API: MPM4_BASE__Milestone1_Project__c (I know, packaged content). When a user creates a new project with the record type OKR, a field in the new project window, called Record Type, is set to OKR. There is another field, which is text/input, called Project name. After a User clicks new project and selects OKR as the record type, I want to set Project Name equal to either the record type=OKR, or the field Record Type, which will also be OKR. The end goal is to pre-populate the field Project Name with the value OKR. 

Here is what I've tried so far, but nothing pre-populates and I get the error "OKR_Project_Trigger: execution of BeforeInsert caused by: System.StringException: Invalid id: OKR ()"

Any help would be greatly appreciated!
 
Trigger OKR_Project_Trigger on MPM4_BASE__Milestone1_Project__c (before insert, before update) {

    for (MPM4_BASE__Milestone1_Project__c project: Trigger.new) {
    
        if (project.RecordTypeId == 'OKR') {
        
            project.Name = project.RecordTypeId;
        }    
    }
}
Best Answer chosen by Jacob Elliott 1
Maharajan CMaharajan C
Hi Jacob,

The trigger will prepoulate the value only after click the save button .  it won't populate the value from the new button click.

if you want to prepoulate the value on the new button click then you have to override the new button.

i think you are in the lightning so create the custom component and override the button with that component. And also check for the Quick Action option is feasible or not.

if you are going for the custom component then you can use the force:createRecord    event simply.

Refer the below links :

https://developer.salesforce.com/docs/component-library/bundle/force:createRecord/documentation
http://www.infallibletechie.com/2017/11/forcecreaterecord-example-in-salesforce.html
http://andynix.uk/forcecreaterecord-example

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Jacob,

Try the below one:

Trigger OKR_Project_Trigger on MPM4_BASE__Milestone1_Project__c (before insert, before update) 
    { 
        for (MPM4_BASE__Milestone1_Project__c project: Trigger.new)
        {
            if (project.recordType.Name == 'OKR')
            {
                project.Name = project.recordType.Name; 
            }   
        }
    }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

 
Jacob Elliott 1Jacob Elliott 1
Hi Maharajan C,

I really appreciate the help! I tried the suggested code, but when I clicked new, the Project Name text field was still empty. However, I didn't get the error, so that is a step in the right direction. I'm attaching some screenshots, hopefully they will help.

The code you suggested
User-added image

The Create new Project screen with Record type pre-populated and Project Name

User-added image


The Project Object and API Name
User-added image


The Record Type Field that auto populates to OKR
User-added image

The Project Name field that we want to populate to OKR
User-added image

 
Maharajan CMaharajan C
Hi Jacob,

The trigger will prepoulate the value only after click the save button .  it won't populate the value from the new button click.

if you want to prepoulate the value on the new button click then you have to override the new button.

i think you are in the lightning so create the custom component and override the button with that component. And also check for the Quick Action option is feasible or not.

if you are going for the custom component then you can use the force:createRecord    event simply.

Refer the below links :

https://developer.salesforce.com/docs/component-library/bundle/force:createRecord/documentation
http://www.infallibletechie.com/2017/11/forcecreaterecord-example-in-salesforce.html
http://andynix.uk/forcecreaterecord-example

Thanks,
Maharajan.C
This was selected as the best answer