+ Start a Discussion
SucreemSucreem 

Pass Date from Custom Object to Standard Object

I've tried a few variations and can't seem to get it right.

 

I need to pass a date from a custom object (Appliance__c) to a standard object (Entitlement).  There is a lookup field to the custom object from the entitlement object.

 

Date on the appliance is called "Latest_Check_In_Date__c" and the date on the Entitlement is called "first_check_in_date__c"

 

Any help would be greatly appreciated.

 

Here is the code I have so far which results in an error: Error:Apex trigger EvalFirstCheckIn caused an unexpected exception, contact your administrator: EvalFirstCheckIn: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.EvalFirstCheckIn: line 16, column 1

 

trigger EvalFirstCheckIn on Appliance__c (before update, after insert) {

List<Id> Ids = new List<Id>();

    for(Appliance__c App : Trigger.new)
    {
        Ids.add(app.id);
    }

List<Appliance__c> applist = new List<Appliance__c>([Select Id, latest_check_in_date__c FROM Appliance__c WHERE Id in :Ids]);

    for(Appliance__c temp : applist )
    {
        Entitlement ent = new Entitlement();
        ent.first_check_in_Date__c = temp.Latest_Check_In_Date__c;
        update ent;
    }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
SLockardSLockard

You need to get the Entitlement object associated with taht appliance before you can update it, and you do that through the relationship field you set up (the lookup field from Entitlement to Appliance__c). Try this code, but replace Relationship_Field__c with the name of the lookup between your objects.

 

trigger EvalFirstCheckIn on Appliance__c (before update, after insert) 
{
	List<Id> Ids = new List<Id>();
	Map<Id, Date> appMap = new Map<Id, Date>();

    for(Appliance__c app : Trigger.new)
    {
        appMap.put(app.Id, app.Latest_Check_In_Date__c);
    }

	List<Entitlement> entList = [Select Id, first_check_in_Date__c, Relationship_Field__c FROM Entitlement WHERE Relationship_Field__c in :appMap.keySet()]

    for(Entitlement ent : entList )
    {
        ent.first_check_in_Date__c = appMap.get(ent.Relationship_Field__c);
    }
	
	update entList;
}

 

All Answers

SLockardSLockard

You need to get the Entitlement object associated with taht appliance before you can update it, and you do that through the relationship field you set up (the lookup field from Entitlement to Appliance__c). Try this code, but replace Relationship_Field__c with the name of the lookup between your objects.

 

trigger EvalFirstCheckIn on Appliance__c (before update, after insert) 
{
	List<Id> Ids = new List<Id>();
	Map<Id, Date> appMap = new Map<Id, Date>();

    for(Appliance__c app : Trigger.new)
    {
        appMap.put(app.Id, app.Latest_Check_In_Date__c);
    }

	List<Entitlement> entList = [Select Id, first_check_in_Date__c, Relationship_Field__c FROM Entitlement WHERE Relationship_Field__c in :appMap.keySet()]

    for(Entitlement ent : entList )
    {
        ent.first_check_in_Date__c = appMap.get(ent.Relationship_Field__c);
    }
	
	update entList;
}

 

This was selected as the best answer
SucreemSucreem

Thanks a bunch! That worked great.  I added an if statement so now the date only passes when certain criteria are met.

 

Here's the final code:

 

trigger EvalFirstCheckIn on Appliance__c (before update, after insert) 
{
    List<Id> Ids = new List<Id>();
    Map<Id, Date> appMap = new Map<Id, Date>();

    for(Appliance__c app : Trigger.new)
    {
        appMap.put(app.Id, app.Latest_Check_In_Date__c);
    }

    List<Entitlement> entList = [Select Id, first_check_in_Date__c, type, date_shipped__c, Appliance__c FROM Entitlement WHERE Appliance__c in :appMap.keySet()];

    for(Entitlement ent : entList ){
        if(ent.first_check_in_date__c ==null){
        
           if(ent.date_shipped__c < appMap.get(ent.appliance__c) | ent.type == 'Virtual Device'){
            ent.first_check_in_Date__c = appMap.get(ent.appliance__c);
            }else {ent.first_check_in_date__c = null;
                    }
                    }
            }
    
    update entlist;
    
    }