How to update parent records when task is created or updated?

Sample Trigger:

trigger TaskTrigger on Task (after insert, after update) {
    Set < Id > setOpptyIds = new Set < Id >();
    for ( Task t : trigger.New) {
        if ( t.WhatId.getSobjectType().getDescribe().Name == 'Opportunity' )
        setOpptyIds.add(t.WhatId);
    }
    if ( setOpptyIds.size() > 0 ) {
        List < Opportunity > listOppty = new List < Opportunity >();
        for ( Id opptyId : setOpptyIds ) {
            listOppty.add(new Opportunity(Id = opptyId));
        }
        update listOppty;
    }
}

Cheers!!!

No comments:

Post a Comment