1

I know we have to query the parent fields to access them on the child object trigger. How can I get the old and new values of the parent fields when its parent record has been changed. I have two objects.

  • Case (Child of Look-up to WebID Object)
  • WebID

I need to access old and new value of the Status__c custom field on WebID__c whenever Case gets its parent WebID Changed. Any inputs on the single line logic to retrieve the old and new values of parent field WebID__r.Status__c?

Thanks in Advance!

3
  • 1
    If the trigger fires on Case, there is no trigger.old or trigger.new on the look-up object WebID. Trigger.new and Trigger.old are only available in the context of the object the trigger is executing on. You can only query to get the current value of a related object.
    – crmprogdev
    Commented Jul 28, 2015 at 14:44
  • 1
    Now that I understand your question better, those aren't "trigger.old" and "trigger.new" values, they're simply the values of two different related records to the case, one of which is no longer a related record because of the reparenting. Had the record not been relinked to a different related record, you'd not have been able to retrieve a previous value of a related record.
    – crmprogdev
    Commented Jul 28, 2015 at 16:00
  • @crmprogdev Thanks for the knowledge share, I updated the question.
    – sam_s
    Commented Jul 28, 2015 at 17:15

2 Answers 2

1

I would recommend creating a map of WebIDs to it's Status by querying the WebID object after creating a set from the old and new ids using the WebID__c field from the old/new child records. Then you can use that map to access the status of the WebID record based on the ID of the old record vs the new record.

3
  • a little confused by your answer, in the child trigger you cannot get the parents trigger.old values unless you implement a design where the parent updates some static value and then the child can reference. It that what you are saying?
    – Eric
    Commented Jul 28, 2015 at 14:31
  • @Eric I believe he's asking what the value of the parent was before the lookup field was changed. So to get that you query the parent object with the Id before and after it was changed to access the status of the parent record before the child was reassigned.
    – Jenny B
    Commented Jul 28, 2015 at 14:47
  • 1
    I just reread it and now I see what you are saying. Question was a bit convoluted......So basically when Case is reparented to a different WebID record, get the status from the first WebID record and the New WebID record....Good eye....
    – Eric
    Commented Jul 28, 2015 at 14:57
0

From the above @JennyB answer, here is the code below.

trigger caseTrigger on Case(before update)
{
  set<Id> newSet = new set<Id>(); 
  set<Id> oldSet = new set<Id>();
  for(Case cs:Trigger.New)
  {
     newSet.add(cs.WebID__c);
  }
  for(Case cs:Trigger.Old)
  {
     oldSet.add(cs.WebID__c);
  }
  map<Id, WebID__c> allWebIdMap = new map<Id, WebID__c>(
                    [select Id, Status__c from WebID__c where Id IN: newSet 
                    or Id IN:oldSet]);
  map<Id, WebID__c> newWebIDMap = new map<Id, WebID__c>();
  map<Id, WebID__c> oldWebIDMap = new map<Id, WebID__c>();

  for(Id WebIDId : allWebIDMap.keySet()) 
  {
      WebID__c currentWebID = allWebIDMap.get(WebIDId);
     if(newSet.contains(WebIDId)) 
     { 
          newWebIDMap.put(WebIDId, currentWebID); 
     }
     if(oldSet.contains(WebIDId)) 
     { 
          oldWebIDMap.put(WebIDId, currentWebID); 
     }
  }

  for(Case c:Trigger.New)
  {
     String oldStatus = oldWebIDMap.get(Trigger.oldMap.get(c.Id).WebID__c).Status__c;
     String newStatus = newWebIDMap.get(c.WebID__c).Status__c;
     system.debug('---------oldStatus----------'+oldStatus);
     system.debug('---------newStatus----------'+newStatus); 
     // perform required operation here  

  }
}  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .