+ Start a Discussion
cbrocbro 

Trigger: Update Status on related Asset Record(s) - Whenever Status on related Contract changes

I am having an issue with my trigger.  The problem is probably that the Object B (Asset) is NOT a child record, but a related object via a lookup field.

 

Can anyone help with this code?  

 

Trigger: I need to update Status on related Asset Record(s) to 'Inactive' - whenever Status on related Contract Record changes to 'Expired'

 

It is not compiling b/c of it, I guess:   Save error: No such column 'Contract' on entity 'Asset'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

 

trigger ContractStatusUpdatesAsset on Contract (after insert, after update) 
{


// 1
List<Asset> childAsset = [Select Contract, Status FROM Asset WHERE Asset IN: Trigger.newMap.keySet()];
        
// 2
    for(Asset child :childAsset)
    {
// 3
    //if(trigger.isInsert && child.Status == 'Active', 'Pending')
    //    {
    //        child.Status = 'Inactive';
    //    }
// 4
    //else 
    if(trigger.isUpdate && child.Status == 'Active', 'Pending' && trigger.NewMap.get(child.Contract).Status != trigger.OldMap.get(child.Contract).Status)
        {
            child.Status = 'Inactive';
        }
    }
        
// 5
    if(childRecords.size() > 0)
            
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
         {
        update childAsset;
         }
         catch (System.Dmlexception e)  
         {
         system.debug (e); 
         }
    }
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Try below code ,

 

trigger ContractStatusUpdatesAsset on Contract(before insert,before update){

	List<Id> conIds = new List<Id>();
	List<Asset> newAssetlist =  new List<Asset>();
	
	for(Contract cnt:Trigger.new){
	if(cnt.Status=='Expired'){
		conIds.add(cnt.Id);
	}
	}

	List<Asset> childAssets = [Select Contract__c, Status FROM Asset WHERE Contract__c IN:conIds];
	
	for(Asset as : childAssets){
		as.Status='Inactive';
		newAssetlist.add(as);
	}
	update newAssetlist;
}

 

All Answers

Vinit_KumarVinit_Kumar

Change your code from

 

List<Asset> childAsset = [Select Contract, Status FROM Asset WHERE Asset IN: Trigger.newMap.keySet()];

 

to 

 

List<Asset> childAsset = [Select Contract__c, Status FROM Asset WHERE Asset IN: Trigger.newMap.keySet()];// Assuming the relationship field on Asset is called Contract__c

cbrocbro

Thank you Vinit, - this is correct for me.  

 

It is now compiling after some further changes...  HOWEVER,

 

What I am attempting to do, is whenever the Contract.Status = 'Expired' - it will change the Asset.Status = 'Inactive'...

 

(However, the Asset is NOT a child - but a Lookup relationship to the Contract)...  I think I have written it as if the Asset is a child record, which it is not.  Can anyone help to improve this code?

 

I have also commented out the 4th part of the code, which I don't understand. 

 

Thanks in advance.

 

 

 

trigger ContractStatusUpdatesAsset on Contract (after insert, after update) 
{


// 1
List<Asset> childAssets = [Select Contract__c, Status FROM Asset WHERE Contract__c IN: Trigger.newMap.keySet()];
        
// 2
    for(Asset child :childAssets)
    {
// 3
    if(trigger.isInsert && child.Status == 'Expired')
        {
            child.Status = 'Inactive';
        }
// 4
    //else 
    //if(trigger.isUpdate && child.Status == 'Expired') && trigger.NewMap.get(child.Contract).Status != trigger.OldMap.get(child.Contract).Status)
    //    {
    //        child.Status = 'Inactive';
    //    }
    }
        
// 5
    if(childAssets.size() > 0)
            
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
         {
        update childAssets;
         }
         catch (System.Dmlexception e)  
         {
         system.debug (e); 
         }
    }
}

 

 

 

Vinit_KumarVinit_Kumar

Try below code ,

 

trigger ContractStatusUpdatesAsset on Contract(before insert,before update){

	List<Id> conIds = new List<Id>();
	List<Asset> newAssetlist =  new List<Asset>();
	
	for(Contract cnt:Trigger.new){
	if(cnt.Status=='Expired'){
		conIds.add(cnt.Id);
	}
	}

	List<Asset> childAssets = [Select Contract__c, Status FROM Asset WHERE Contract__c IN:conIds];
	
	for(Asset as : childAssets){
		as.Status='Inactive';
		newAssetlist.add(as);
	}
	update newAssetlist;
}

 

This was selected as the best answer
cbrocbro

Here is the code I have used from your code...

 

'as' does not work as an abbrev. for Asset (so it's just 'a' now) - but otherwise it works great!

 

Disregard - it does not change the status/Asset.InactiveDate__c, if it is already set to 'Inactive':  However, I only want this to update the status of the Asset if the Asset is in 'Active' or 'Pending' status (or another way to look at it would be to only update the status of the Assets if it is Status not equal to 'Inactive')  - is there a way to do this?

 

Because when Asset.Status is updated to 'Inactive' - it also changes the Asset.InactiveDate__c field to today's date.  

 

Some Assets may have ended well before the related Contract has ended, and I don't want to change the date of the Assets that have already been set to 'Inactive' and already have a value for Asset.InactiveDate__c ,..

 

Thanks again,

Chris

 

trigger ContractStatusUpdatesAsset on Contract(before insert,before update){

    List<Id> conIds = new List<Id>();
    List<Asset> newAssetlist =  new List<Asset>();
    
    for(Contract cnt:Trigger.new)
    {
    if(cnt.Status=='Expired')
        {
        conIds.add(cnt.Id);
        }
    }

    List<Asset> childAssets = [Select Contract__c, Status FROM Asset WHERE Contract__c IN:conIds];
    
    for(Asset a : childAssets)
    {
        a.Status='Inactive';
        newAssetlist.add(a);
    }
    
    
    if(childAssets.size() > 0)
            
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
         {
            update newAssetlist;
         }
             catch (System.Dmlexception e)  
         {
         system.debug (e); 
         }
    }    
    
    
}