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
Sheetal Chhabra 7Sheetal Chhabra 7 

Before insert trigger not working


I need custom picklist fields on Account to be autopopulated on creating New Account. I have written trigger for this. It is working on before update but not working for Before insert. Please help.
User-added image
Pawani MutturuPawani Mutturu
Hi,
We can do field update using triggers as well, then why have a trigger for the same?
Sheetal Chhabra 7Sheetal Chhabra 7
@Pawani: That is what I have done. Use Triggers.
Nitin SharmaNitin Sharma
Hi Sheetal,

Use below code. 
Suppose Note__c is a custom field on account object and you want to auto populate Note__c = 'XYZ' before insert and update.
trigger XYZ on Account (before insert, before update) {
for(Account acc: Trigger.new){
    acc.Note__c= 'XYZ' ;
    }   
}
Thanks,
Nitin Sharma
 
Harish RamachandruniHarish Ramachandruni
Hi,

Check filed lavel security for using  user profile . And check from system admin profile and check trigger is active or inactive . and user is having data or not for perticular firlds data . 



Regards ,
Harish.R.
Sumeet_ForceSumeet_Force
1. Use System debug to check if user[0].picklistfield API name has values during insert event as well .
2. Also, check if the custom picklist field is writeable for updates.

3. You can also check for any workflow/validation rules that might be limiting/updating old values for this field and specific to insert operations.
Sheetal Chhabra 7Sheetal Chhabra 7
Hi Sumeet and Harish,

I have checked the logs and field security setting. It is working on save of the record, but not autopopulation on click of New. Please help.

Thanks,
Sheetal
Sheetal Chhabra 7Sheetal Chhabra 7
In logs also it is showing only before Insert. 
Medhya MahajanMedhya Mahajan
Hi Sheetal, 

You need not write a trigger for what you  are trying to achieve. 
Your trigger will only display values when the record is saved.

In order to auot populate values you can try the following :

To Auto-populate picklists try setting the default values for picklists.
In order to auto-populate  text fields you need to do URL hacking.

I am assuming from your question all your fields are picklists, so i suggest you simply set default values for them instaed of writing a trigger.

Let me know if you need any help.

Mark solved if this resolves your issue.

Regards
Medhya Mahajan
Sumeet_ForceSumeet_Force
The debug log shows every assignment when the log levels are rightly set. Checking that line if it gets set or not will help you to debug further.
Sheetal Chhabra 7Sheetal Chhabra 7
@Medhya: I need these values to be read from the User. I cannot set the default values. It is based on User fields.
I was doing it with URL hacks but that does't work in Lightning.

 
Sumeet_ForceSumeet_Force
One different approach I would say would be to seperate both before insert and before update trigger and check execution difference.
Sheetal Chhabra 7Sheetal Chhabra 7
Actually my issue is with URL hacks which are not working in Lighting and throwing an error everytime user creates an Account. 
I tried it with triggers but I am not able to find why Before insert trigger is not updating the values.

If anyone of you has any alternate to URL hacks or auto-populate fields on creation of new record. Please help.

Thanks,
Sheetal