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
Walter@AdicioWalter@Adicio 

Which trigger event do I use to default a field on the edit page?

I am trying to set the case origin field to a value on the edit page right after you click the new button. I have been trying several different ways but have had no luck so far.
 
Trigger: 
 

trigger setOriginTrigger on Case (before insert) {Case[] accs = Trigger.new;setOrigin.addOrigin(accs);}

 

 Class:
 

public class setOrigin { public static void addOrigin(Case[] accs){ for (Case c:accs){ if (c.Origin != 'Customer Portal') { c.Origin = 'Customer Portal' ; } } } static testMethod void runPositiveTest() { string mySubject = ''; string validateNewSubject = 'Customer Portal'; System.debug('Create test case and insert it into the database'); Case c = new Case(Subject = '', Due_Date__c = System.today(), new_subject__c = 'Customer Portal' ); insert c; for(Case c1: [select Subject, new_subject__c from Case where Due_Date__c = TODAY and Subject = null ] ) { mySubject += c1.new_subject__c; } System.assertEquals(validateNewSubject, mySubject); } }

 

 Thank you.
Best Answer chosen by Admin (Salesforce Developers) 
micwamicwa

You can't do this using a trigger. The insert trigger is only fired when the user clicks the save button. The before insert trigger does not fire when the new button is clicked.

 

You can achieve setting a field to a certain value overwritting the "New" button using a s-control where you call an url where the parameter is set.

All Answers

micwamicwa

You can't do this using a trigger. The insert trigger is only fired when the user clicks the save button. The before insert trigger does not fire when the new button is clicked.

 

You can achieve setting a field to a certain value overwritting the "New" button using a s-control where you call an url where the parameter is set.

This was selected as the best answer
Walter@AdicioWalter@Adicio
Thank you.
JAW99JAW99

How would one do this based on a selected record type?

 

For example, I have a lead record type "Individual" and I'd like to pre-load the value of "Invidual" in the required "Company' field.

 

Thanks.