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
Manu@devManu@dev 

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

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

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

Satish_SFDCSatish_SFDC
trigger TestTrigger on CustomObjectName__c (before insert) {
     for(CustomObject__c record : Trigger.new){
         record.Status__c = true;
     }
}

 

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.

Manu@devManu@dev
Hi satish,

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
souvik9086souvik9086

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

This was selected as the best answer
Yoganand GadekarYoganand Gadekar

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';

}

 

}

Manu@devManu@dev

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

Manu@devManu@dev
Hi Yoganand,

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
souvik9086souvik9086

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

Manu@devManu@dev
Hi Souvik,

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.
souvik9086souvik9086

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

Yoganand GadekarYoganand Gadekar

Thank you for your comments, i will surely put more basic examples on vf and apex.

Manu@devManu@dev
If i use after update in trigger the following error is coming while updating the record.

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
souvik9086souvik9086

Post your trigger. What you have written in after update

Manu@devManu@dev
Trigger as below:-

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';
}

}
}
souvik9086souvik9086

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

Bhaskar ChowdaryBhaskar Chowdary

Hi this is not working in my custom obj

souvik9086souvik9086

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