0

I'm attempting to create 'default text' in a standard field on the Opportunity object. Obviously, I can't use system defaults since it's a standard field. I also can't use Workflows because it updates after the record is saved. I'm attempting to do it through an APEX trigger. I have the below:

trigger Opportunity_Defaults on Opportunity (before insert) {
for(Opportunity o : Trigger.New) {
    o.Name = 'text goes here';
}  

}

Despite using 'before insert', this does not default until AFTER the Opportunity record is saved. What am I doing wrong here?

1

Harley,

The trigger process is only kicked off when a record is saved. I would recommend taking a look at this article Order of Execution

When a trigger is written in the way you have described, Salesforce will set the default value before the record is committed to the database.

Are you attempting to set a default value when the a user is inserting a record in the user interface?

enter image description here

If so, that is not supported with standard Salesforce page layouts. A custom Lightning component would be necessary to achieve that functionality.

Also, I recommend you checkout this trail on trailhead to learn about a better way to write triggers and their handlers.

1

Harley, if you are on classic, you can use the URL hack to prefill the data. If you are on lightning, you would need a custom lightning component.

  • I'm in Lightning, so I'll need to go the component route. – Harley Oct 31 '19 at 12:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.