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
JJSHHSJJSHHS 

DB trigger on the same object

Hello,

Had this dumb question. I can hear you telling that this can be done by setting up default values on the platform itself vs. a trigger and I'm completely trying the below as an experiment.


trigger spnInsert on Position__c (before Insert) {

    For (Position p: trigger.new) {
   
        p.Max_Pay__c = 99999;
    }
}


I'm getting this error below.

Error: Compile Error: Invalid type: Position at line 3 column 10

When I tried changing the for loop to state, however, I'm getting a different error - trigger name is already used

For (Position__c p: trigger.new) {

3. I also tried the below one.

trigger spnInsert on Position__c (before Insert) {

    list <position__c> p = new list <position__c>();

    For (p : trigger.new) {
   
        p.Max_Pay__c = 99999;
    }
}

Pl advise, what am I missing here.
Best Answer chosen by JJSHHS
Elie.RodrigueElie.Rodrigue
In the page layout editor, the red star indicate a required field.
The blue dot means that you cant remove the field from the page layout.
Screenshot

All Answers

Adnubis LLCAdnubis LLC
Just a small error. Try this instead. In your for loop you were using a Type of Position when it should have been Position__c.

trigger spnInsert on Position__c (before Insert) {

    For (Position__c p: trigger.new) {
  
        p.Max_Pay__c = 99999;
    }
}
Adnubis LLCAdnubis LLC
Also, make sure that no other triggers already exist with the name spnInsert.
Elie.RodrigueElie.Rodrigue
In your first sample, you are missing the __c on the position object.
The trigger name is already used might just have been an issue with the IDE as sometime you can do 2 saves at the same time.

there's no point in declaring a list (list <position__c> p = new list <position__c>();) as this list stay empty anyway. 

The for loop structure should be as follow : 

for(Type varName : iterable)  { code }

so doing for(Position__c p : trigger.new) { p.Max_Pay__c = 99999; } is the way to go.
JJSHHSJJSHHS
Hi Adnubis and Elie,

Thanks for your help. I was using the same "trigger name" within my dev org. However, within 2 different apps. But it's not working. When I changed the trigger name, it started to work.


One more question - when I look at custom object fields - using the field layout, how do I know which fields are required vs. not without having to go into every single field.

Adnubis LLCAdnubis LLC
SPN,

When you view the page layout you should see red asterixs next to the fields that are required.

There are two ways to require a field though, the first is to do at the database level and require it for every record. The other way is at the page level where it can be required on a specific page layout. 
Elie.RodrigueElie.Rodrigue
In the page layout editor, the red star indicate a required field.
The blue dot means that you cant remove the field from the page layout.
Screenshot

This was selected as the best answer
JJSHHSJJSHHS
You both are awesome, I learnt a couple of things today. Thanks for your time and patience.