I'm trying to create a trigger on the Task object that would stamp a field from a related object (Account), the field is Account Type.
I'm unable to use flow for this requirement since there are several tasks coming from third party apps and they cause flow errors.
I created the following Apex trigger on the Task, but instead of stamping the Account.Type into the tasks, this is just deleting the Account Type.
Would someone be able to point out the error so I can understand and fix it. Thanks
trigger AccountTypeStamp on Task (after insert) {
List<Account> acclistupdate = new List<account>();
Map<Id,string> mapaccounttype = new Map<Id,string>();
for (Task t : trigger.New) {
if (t.WhatId != Null && t.WhatId.getSobjectType() == Account.getSObjectType()) {
mapaccounttype.put(t.WhatId,t.Account_Type_Update__c );
}
}
for(account acc: [SELECT Id,Type FROM account where id=:mapaccounttype.keyset() AND Type!= Null]){
acc.Type = mapaccounttype.get(acc.id);
acclistupdate.add(acc);
}
update acclistupdate;
}