You need to sign in to do that
Don't have an account?

Trigger
Hi all,
This is the first time I am using trigger on salesforce. So please explain in detail.
I have an custom object and there is a field called status (checkbox datatype). Whenever a record is created this field should be automatically change the status to open.
Please explain how to write trigger for this.
Regards,
Manu
I think your status field is not checkbox. I hope.As you told that it become open. So use the below code.
trigger StatusChange on CustomObject__c(before insert){
for(CustomObject__c record : Trigger.new){
record.Status__c = 'Open';
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
Trigger.new is a Trigger Context variable (List) which holds all the new records which are created. This trigger example above runs on the before insert (before inserting the record), and makes the necessary changes to each of the new records in the Trigger.new list.
If you create a new record through the Salesforce UI, probably there is just one record in the Trigger.new list, but if you do a bulk import then this Trigger.new has multiple new records.
Hope this helps.
Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
Many thanks for your reply. It was my mistake telling that field was checkbox, instead it is Pick list. Sorry for inconvenience. Now please change the code accordingly.
R - Manu
I think your status field is not checkbox. I hope.As you told that it become open. So use the below code.
trigger StatusChange on CustomObject__c(before insert){
for(CustomObject__c record : Trigger.new){
record.Status__c = 'Open';
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
You can achieve this by workflow, but since you are trying hands on trigger you can try following,
Trigger myFirstTrigger on Custom_Object__c (before insert){
for(Cistom_Object__c cs:trigger.new){
cs.status__c ='Open';
}
}
Hi Souvik,
Now the same operation should be done after update. (ie the status should be changed to closed on updating the field)
I have written the below code but it is not working.:( please help...
trigger changestatus on Test_Object__c (before insert, after update) {
for (Test_Object__c record: trigger.new){
if(isBefore){
record.Status__c = 'open';
}
if(isAfter){
record.Status__c = 'Closed';
}
}
}
R - Manu
I have visited http://cloudforce4u.blogspot.in, it is very helpful for me. please put some more codes examples that will explain the most basic level things in coding. This will be helpful for starters like me. Unfortunately all blogs I visited shows heavy and complex codes.
R - Manu
Change like this
trigger changestatus on Test_Object__c (before insert, before update) {
for (Test_Object__c record: trigger.new){
if(trigger.isInsert){
record.Status__c = 'open';
}
if(trigger.isUpdate){
record.Status__c = 'Closed';
}
}
}
If the post is helpful please throw KUDOS
Thanks. Your code is working and finally please can you let me know why we should not use 'after update' in the first (following) line
trigger changestatus on Test_Object__c (before insert, before update) {
I tried using this. The trigger is compiling correctly but while updating record it throws error.
We can use after update as well. But we need to avoid it as far as possible. Because if we write after update we have to write the update statement as well. It will not automatically assign the value. And it will increase code statement. Also another problem is recursive trigger call. When the update statement is executed it will again call the trigger and it will go to infinite loop. It can be handled too. Thats why before is the best way.
By the way what is teh error coming? If you want I can change the trigger for you to after and modify the code. let me know.
If this post is helpful please throw Kudos.
Thanks
Thank you for your comments, i will surely put more basic examples on vf and apex.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger changestatus caused an unexpected exception, contact your administrator: changestatus: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.changestatus: line 9, column 1
Post your trigger. What you have written in after update
Trigger changestatus on Test_Object__c (before insert, after update) {
for (Test_Object__c record: trigger.new){
if(trigger.isInsert){
record.Status__c = 'open';
}
if(trigger.isUpdate){
record.Status__c = 'Closed';
}
}
}
Create a controller first. This is to avoid recursion.
public class FutureTriggerController{
public static boolean isFutureUpdate = false;
}
Modify the trigger as below
Trigger changestatus on Test_Object__c (before insert, after update) {
if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate =true;
List<Test_Object__c> listToBeUpdated = new List<Test_Object__c>();
for (Test_Object__c record: trigger.new){
if(trigger.isInsert){
record.Status__c = 'open';
}
if(trigger.isUpdate){
record.Status__c = 'Closed';
listToBeUpdated.add(record);
}
}
if(listToBeUpdated.size() > 0){
update listToBeUpdated;
}
}
}
If the post is helpful please throw KUDOS
Hi this is not working in my custom obj
Can you elaborate what is the issue coming? I guess you tested the above for your custom object. right?
If the post is helpful please throw KUDOS