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
cbrocbro 

Inherit/Pass Date from Standard Object to Standard Object

I want to pass along the date, as well as the status to the Asset Object from the Contract Object - when I set the Contract Object's end date.

 

The code I have is passing along the status (so when I END the Contract, it sets the Asset's Status to 'Inactive'), but it is not passing along the date.

 

So the problem is, that the Assets are having a different end date from the Contract.

 

Can anyone help?

 

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 = '+ newAssetList.size());
    try
         {
            update newAssetlist;
         }
             catch (System.Dmlexception e)  
         {
         system.debug (e); 
         }
    }    
    
    
}

 

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

    Map <Id, Contract> contractMap = new Map <Id, Contract>();
    List<Asset> newAssetlist =  new List<Asset>();
    Date inactiveDate;

    for(Contract c: Trigger.new)
    {
    if(c.Status=='Expired')
        {
        	inactiveDate = c.End_Date__c;
        	contractMap.put(c.Id, c);
        }
    }

    List<Asset> childAssets = [Select Contract__c, Status, Inactive_Date__c FROM Asset WHERE Contract__c IN: contractMap.keySet()];
    
    for(Asset a : childAssets)
    {
        a.Status='Inactive';
        a.Inactive_Date__c = inactiveDate;
    }
}

 

cbrocbro

This code compiles and saves just fine, but it does not work.

 

Now it is not passing along the Status either.

 

hmm.

 

Why did you change this:  

List<Id> conIds = new List<Id>();
    List<Asset> newAssetlist =  new List<Asset>();

 

 

 

to this:

 

Map <Id, Contract> contractMap = new Map <Id, Contract>();
    List<Asset> newAssetlist =  new List<Asset>();
    Date inactiveDate;
ForcepowerForcepower

cbro,

 

Try this (slight mods to what sobroach had suggested). Make sure
to turn debug on and see if you're getting the End_Date__c value for Contract.

 

 

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

    //List<Id> conIds = new List<Id>();
    Map <Id, Contract> contractMap = new Map <Id, Contract>();

    List<Asset> newAssetlist =  new List<Asset>();
    //Date inactiveDate;

    for(Contract cnt:Trigger.new)
    {
    if(cnt.Status=='Expired')
        {
            //conIds.add(cnt.Id);
            //inactiveDate = c.End_Date__c;
            contractMap.put(c.Id, c);
 
       }
    }

    List<Asset> childAssets = [Select Contract__c, Inactive_Date__c, Status FROM Asset WHERE Contract__c IN: contractMap.keySet()];
    
    for(Asset a : childAssets)
    {
        a.Status='Inactive';
    Contract c = contractMap.get(a.Contract__c);
        a.Inactive_Date__c = c.End_Date__c;

        System.debug('Contract end date = ' + c.End_Date__c);
        newAssetlist.add(a);
    }
    
    
    if(childAssets.size() > 0)
            
    {
    //System.debug('Chris has values to insert = '+ newAssetList.size());
    try
         {
            update newAssetlist;
         }
             catch (System.Dmlexception e)  
         {
         system.debug (e);
         }
    }    
    
    
}

 

best,

Ram