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
Jenny Chen 9Jenny Chen 9 

Asset trigger not working, SObject Type is not "Asset", but customized

I had tried different triggers on Assets, however, none of them works. Even simple 'before insert' trigger. Then I did a simple workflow, it worked, however, for my requirements, simple workflow does not work. I need to update a value on Account which is parents of Assets, the value need to come from most recent date value from latest created Asset. 
 
I created my triggers under the standard object 'Asset', triggers. The only thing i noticed here is after i created the trigger, the sobject on top of the trigger section from is not 'Assets', which is different than other objects is
Sobject TypeAssets Owned - Epicor
 
Not sure why it is not “Assets”. Do you have any ideas of how to make this trigger work?



Apex Trigger Detail
Sobject Type:  Assets Owned - Epicor
Code below: 
trigger MaintenanceExpiration on Asset (after insert) {

    Set<ID> acctId = new Set<ID>();

    for(Asset ass: Trigger.new) {
        acctId.add(ass.Account.id);
    }
    List<Account> acctList=[SELECT id, Maintenance_Expiration__C from Account where id in :acctId];
    
    for(Asset ass: Trigger.new) {
        for( Account a: acctList){
           if (ass.Account.id==a.Id){
                if (a.Maintenance_Expiration__C==null){
                    Datetime epday = trigger.new[i].Maintenance_Expiry_Date__c;
                    a.Maintenance_Expiration__C= date.newInstance(epday.year(), epday.month(),epday.day());
                }
                else if(a.Maintenance_Expiration__C<trigger.new[i].Maintenance_Expiry_Date__c){
                    Datetime epday = trigger.new[i].Maintenance_Expiry_Date__c;
                    a.Maintenance_Expiration__C= date.newInstance(epday.year(), epday.month(),epday.day());

                }
                
          }
        }
    }
     if(acctList!=null && acctList.size()>0){
           //update account list;
           update acctList;
       }
    
}


 
Austin MuellerAustin Mueller
Hey Jenny, are you able to try something like this?
 
trigger MaintenanceExpiration on Asset ( after insert ) {

    Set< ID > acctId = new Set< ID >();

    for( Asset ass : Trigger.new ) {

        acctId.add( ass.AccountId );

    }

    Map< Id, Account > acctList = new Map < Id, Account >( [SELECT Id, Maintenance_Expiration__C FROM Account WHERE Id IN :acctId] );
    Map< Id, Account > acctUpdate = new Map< Id, Account >();

    if( !acctList.isEmpty ) {
    
        for( Asset ass : Trigger.new ) {

            if ( acctList.containsKey( ass.AccountId ) ) {

                if ( acctList.get( ass.AccountId ).Maintenance_Expiration__C == null || acctList.get( ass.AccountId ).Maintenance_Expiration__C < ass.Maintenance_Expiry_Date__c ) {

                    Datetime epday = ass.Maintenance_Expiry_Date__c;
                    acctList.get( ass.AccountId ).Maintenance_Expiration__C = date.newInstance( epday.year(), epday.month(),epday.day() );

                    acctUpdate.put( ass.AccountId, acctList.get( ass.AccountId ) );

                }

            }

        }

    }

     if( !acctUpdate.isEmpty() ) update acctUpdate;
    
}

 
Jenny Chen 9Jenny Chen 9
Thank you. It is working now. I found the Soject Type does not matter here.