0

I have pretty complicated situation here!

I have a field on User named Credit__c and another field on Time_Off__c named Days__c.

When I add a Time_Off__c with a number of days => Credit__c -= Days__c (we subtract number of days from the credit) BUT I need to update the Days__c field so the new Credit__c will be update as well.

My solution is:

Credit__c new = ABS(Days__c old - Days__c new) + Credit__c old

(p.s. ABS the absolute value)

I need to manipulate my trigger with old and new records.

Is this possible?

  • Speaking from experience, it would be good to get started on something and ask direct questions. The guys here are really good and helpful but they won't write the code for you. – EricSSH Mar 31 '14 at 17:42
  • 2
    What is the relationship between the two objects? On which object your are trying to write the trigger? – highfive Mar 31 '14 at 17:45
  • Yeah I know Eric!!! m working on the code ! but getting confused with old and new record! so if you have any example thank you in advance REGARDS – LoveLace Mar 31 '14 at 17:56
  • Please show us what you have. – EricSSH Mar 31 '14 at 18:03
  • The user with logged session has the right to create his own time_Off__c. I'm trying to update a custom field on User object – LoveLace Mar 31 '14 at 22:47
0

This code is something of a guess because you have not responded to highfive's comment and your question is not entirely clear. This code assumes that there is a lookup relationship between Time_Off__c and User via an id field called User__c.

It illustrates that the Trigger context variable holds references to the values of the objects before they were changed (the old fields) and after they have been changed (the new fields) and this allows you to look at the difference between old and new values:

trigger TimeOffTrigger on Time_Off__c (before update) {

    // Get the current Credit values
    Set<Id> userIds = new Set<Id>();
    for (Time_Off__c newTimeOff : Trigger.new) {
        userIds.add(newTimeOff.User__c);
    }
    Map<Id, User> users = new Map<Id, User>([
            select Id, Credit__c
            from User
            where Id in :userIds;
            ]);

    // Update Credit values with the changed days
    for (Time_Off__c newTimeOff : Trigger.new) {
        Time_Off__c oldTimeOff = Trigger.oldMap.get(newTimeOff.Id);
        User u = users.get(newTimeOff.User__c);
        u.Credit__c += (oldTimeOff.Days__c - newTimeOff.Days__c);
    }
    update users.value();
}

This is only handling the update case, so you may also need to code for the insert case and perhaps the delete case too. And if fields allow nulls you will need more defensive code.

If none of this makes any sense, I recommend you Google and watch some YouTube presentations and generally read more on the subject. It is particularly important to get comfortable with using sets and maps.

PS

The recent comments on the question suggest that this is what is required (though at first sight it looks quite unusual):

trigger TimeOffTrigger on Time_Off__c (before update) {
    User u = [
            select Id, Credit__c
            from User
            where Id in :UserInfo.getUserId()
            ];
    for (Time_Off__c newTimeOff : Trigger.new) {
        Time_Off__c oldTimeOff = Trigger.oldMap.get(newTimeOff.Id);
        u.Credit__c += (oldTimeOff.Days__c - newTimeOff.Days__c);
    }
    update u;
}
| improve this answer | |
  • I have a problem when I put my code in method on apex class and call it with after update trigger. Can't figure out why! here is the error : Save error: Illegal assignment from SObject to SOBJECT:Time_Off__c in this line of code : Time_Off__c oldTimeOff= Trigger.oldMap.get(newTimeOff.Id); – LoveLace Apr 2 '14 at 9:46
  • @KawtarBRAZI I think the problem is that inside the trigger the type can be inferred, but outside it can't. If moving code outside the trigger, best to pass Trigger.oldMap and any other Trigger values you need as method parameters rather than grabbing them from the Trigger global. Then as well as being clearer code, the type is tied down both in the trigger and in the class i.e. "methodName(Map<Id, Time_Off__c> oldMap, Map<Id, Time_Off__c> newMap)" and newMap.values() can be used in place of Trigger.new. – Keith C Apr 2 '14 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.