1

i have a issue in retrieving the old value of parents record onthe junction object update scenario, do anyone have come across this scenario, please throw lights.

Junction__c oldlrj = [SELECT Id, Relationship__r.id, objA.fielda FROM Junction__c WHERE Id IN: JunctionOldMap.keySet() limit 1];  

oldValue = oldlrj.objA.fielda;

This oldvalue is not returning the oldvalue, instead gives the current value.

1
  • This question is not quite up to the expected quality. I would recommend adding in more information on your use case, as well as anything you have already tried or discovered and why it doesn't or won't work. See here for more info: salesforce.stackexchange.com/help/how-to-ask May 23, 2019 at 14:50

1 Answer 1

0

If you are using an after update trigger, you need to create a Map and query the old parents:

Map<Id, Parent__c> oldParents = new Map<Id, Parent__c>();
for(Child__c record: Trigger.old) {
  oldParents.put(record.Parent__c,null);
}
oldParents.putAll([SELECT FieldA__c FROM Parent__c WHERE Id = :oldParents.keySet()]);
for(Child__c record: Trigger.old) {
  String oldValue = oldParents.get(record.Parent__c).FieldA__c;
  Child__c newRecord = Trigger.newMap.get(record.Id);
  // Compare old to new here
}

However, if you use a before update trigger, the lookup field values will still point to the old parent:

Child__c[] children = [SELECT Parent__r.FieldA__c FROM Child__c WHERE Id = :Trigger.new];
for(Child__c record: children) {
  String oldValue = record.Parent__r.FieldA__c;
  Child__c newRecord = Trigger.newMap.get(record.Id);
  // Do stuff here
}

In the more general sense of querying parent records, I recommend always using a Map for efficiency purposes; it will always be at least as efficient to use a Map than any other technique.

4
  • i still seeing the current value in the parent in the after update trigger on junction object.
    – user8132
    May 23, 2019 at 15:45
  • @user8132 You might need to explain your problem better, then. Both of the proposed solutions here would work for the situation as I understand it.
    – sfdcfox
    May 23, 2019 at 15:50
  • is it different for the junction object, because i have used this for child object and used to work fine, but this time it is the junctiion object, which has a lookup relationship to the parent.
    – user8132
    May 23, 2019 at 16:05
  • @user8132 There's no inherent difference between a junction object and a child object as far as the platform is concerned. I think we're going to need more context surrounding this situation.
    – sfdcfox
    May 23, 2019 at 16:27

You must log in to answer this question.

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