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;
}
}
Set < Id > setRecIds = new Set < Id >();
for ( Task t : trigger.New) {
if ( String.isNotBlank( t.WhatId ) ) {
setRecIds.add(t.WhatId);
}
}
if ( setRecIds.size() > 0 ) {
List < sObject > listRecs = new List < sObject >();
for ( Id recId : setRecIds ) {
sObject obj = recId.getSObjectType().newSObject( recId );
listRecs.add( obj );
}
update listRecs;
}
}