function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChristineVWChristineVW 

Can it be done? Activity Trigger needs to update an Asset field

I need a trigger to do the following: 

When an Activity ("log a call") is created with a numeric value in one of my custom Activity fields, a numeric field needs to be updated on an Asset.  The Activity getting created will be related to an Account, and in turn, the Account will have several related Assets. 

The trigger needs to get a list of Assets that are related to the common Account and, based on the Asset type or name, the trigger should identify the one Asset that has a numeric field that needs to be updated. 

I am having a hard time with this because while I can write a trigger to update information on the Account, how can I get the information about the related Assets so that I can update one of them?  Do I need TWO triggers, one before insert on the Activity and one before update on an Account? 

Thanks much.
TehNrdTehNrd
You shouldn't need two triggers but you will need two SOQL calls in one trigger. This isn't exactly how to do it but hopefully it can set you on the right path.

Code:
Set<Id> acctIds = new Set<Id>();

for(Task t : trigger.new){
 if(WhatId.startsWith('001'){//this confirms it is attached to an account as all account Ids start with 001
  acctIds.add(t.whatId);
 }
}

Asset assetList = [select Name, Type, NumericField from Asset where AccountID IN :acctIds];

for(Asset a : assetList){
 //do your logic here
}

update assetList;




Message Edited by TehNrd on 05-13-2008 03:43 PM