You need to sign in to do that
Don't have an account?
Anuj.Shukla
Global trigger for any object being updated ?
Hi All,
I have one strange requirement.
I need to create "Time taken" field on Case object (and also can create this field on any standard/custom object).
Assuming that my all objects have these two fields
- Start time
- End time
Time taken will be calculated as difference of end time & start time (if start time and end time have proper entries).
Now, I need to write a generic code, that would populate the time taken field whenever I update an object
(and if object being updated has these above mentioned 3 fields, this I can find out)
I can very well write an update trigger for every object and then populate time taken field but this will have to be replicated for any future custom object.
My intended logic should be generic, and should be called irrespective of Object being updated
(something like a global trigger which checks what's the object being updated, and then update the time taken field of that object).
Is this possible in Salesforce? I have no idea, please help.
Thanks,
Anuj
I dont think its possible to write a generic trigger that will work for any object.
You have to write trigger for each object being used.
And write a class that will be invoked from any Trigger that gets fired.
for ex: Write trigger on all the required objects as below.
trigger contactUpdate on Contact(after update)
{
//Invoke the class method
GenericClass.updateObject(Trigger.new);
}
//Class method invoked irrespective of which object is updated.
public class GenerciClass
{
public static void updateObject(List<SObject> objList)
{
for(SObject obj: objList)
{
//any calculations
//in the end
obj.put('Time_Taken__c', value);//Assuming Time_Taken__c to be the field in all objects
}
update objList;
}
}
Yes, I agree with your point.
I too was going to do it same way.
But this would mean that :-
In future if any new object gets created which needs to have this time taken field updated on every save,
then I will have to write few lines of code in trigger of that object.
Doesn't work really like write once and forget.
Any other lines of thought?
Thanks,
Anuj
To my knowledge there is no way to achieve such functionality unless and until few lines of trigger code is written for each object.
There is nothing coming to my mind that could be such generic.
Hey it sounds to me that you could use a formula field for time taken.
Formula = End_date__c - start_date__c
That seems to do what you are saying and will update on item creation or update. You could even add a couple of nested if statements to check if start date and end date exist. Like:
if(start_date__c != NULL, if(end_date__c != NULL, if (end_date__c>start_date__c, End_date__c - start_date__c, NULL), NULL), NULL)
That should give you functionality like you want as well as the trigger.