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
MeaghanMeaghan 

Modify trigger to fire on create only

Hi all - 
I am hoping you can help me modify a trigger - currently, it fires every time a record is updated, but we need to modify it to fire upon create only - thank you!! 
1trigger AssetTrigger on Asset (before insert, before update, before delete, after insert, after update, after delete) {
2        if(Trigger.isBefore) {
3            if(Trigger.isInsert && AssetTriggerHelper.firstRunBefore) {
4                AssetTriggerHelper.setAccountOnAsset(trigger.new);
5            }
6            if(Trigger.isUpdate) {
7                AssetTriggerHelper.setAccountOnAsset(trigger.new);
8            }
9            if(Trigger.isDelete) {
10                //do nothing for now
11            }
12            AssetTriggerHelper.firstRunBefore = false;
13        }
14        
15        if(Trigger.isAfter && AssetTriggerHelper.firstRunAfter) {
16            if(Trigger.isInsert) {
17                //do nothing for now
18            }
19            if(Trigger.isUpdate) {
20                //do nothing for now
21            }
22            if(Trigger.isDelete) {
23                //do nothing for now
24            }
25            AssetTriggerHelper.firstRunAfter = false;
26        }
27    }

Anthony McDougaldAnthony McDougald
Hello Ability,
Hope that your day is off to an amazing start. Your request trigger is below and we're very happy to serve you. Hope this helps and may God bless you abundantly.
trigger AssetTrigger on Asset (before insert) {
        if(Trigger.isBefore) {
            if(Trigger.isInsert && AssetTriggerHelper.firstRunBefore) {
                AssetTriggerHelper.setAccountOnAsset(trigger.new);
            
        }
    }
}


Best Regards,
Anthony McDougald
Adheesh GuptaAdheesh Gupta
Hi Ability,

Your code is good but currently it will execute for create as well as update. So, commenting out line 7 will solve your problem and will then only execute for BeforeInsert, i.e. only when a new record is inserted.

Hope this helps!

Thanks,
Adheesh