You need to sign in to do that
Don't have an account?
Trigger: Before or after insert?
Hello,
Please tell me if you don't understand my question:
I want the field Name of my custom object to be something like {000000}-{000}, where {000000} is the number of the object and {000} is the number of the version, that should be incremented not when a record is created, but when some specific fields or child objects are updated. I can't use Auto Number, so I was thinking about using a Text name field and fill it with a trigger. I would have an Auto Number field called Object_Number__c and a Number Field called Version_Number__c. So, I would set the object name with: MyObject__c.Name = Object_Number__c + '-' + Version_Number__c; or something like that.
If I try to do this with a Before Insert Trigger, what I get in the name of my object is null-null because the Auto Number and the Number Fields are null before inserting. If I try to do this with an Afetr Insert Trigger, I can't change any field and I get the error "Record is Readonly". Does anyone have any idea of how to achieve this?
trigger formatName on MyObject__c (after insert) { for(MyObject__c o : Trigger.new) { MyObject__c.Name = MyObject__c.Object_Number__c + '-' + MyObject__c.Version_Number__c; } }
You should be able to handle the inserts with a workflow update. Create a workflow rule that fires when a record is created. Use a field update (with a formula) to insert the correct value.
The updates will probably have to be handled via trigger, but that won't be a problem since the field will have been populated.