0

In Object Alpha, I have two fields Field currentDate__c = Date.today().format(); (say) and field relevantfield__c= “Partner__r. lmn_currentDate_c” (this field value changes and specifies when this currentDate__c should be updated )and object partner is a lookup field on object alpha When currentDate__c get updated I have to get text field value from relevantfield__c and update that field. How to do this I am stuck. Any inputs? say alpha a; I tried to use a.get (’Partner__r. lmn_currentDate_c’); to update trigger.new to update the list it is gives me error.

1

You have to "follow the path" by using the appropriate methods..Something like the following:

public Object getValueFromPath(sobject record, string path) {
  String[] parts = path.split('\\.');
  while(parts.size() > 1 && record != null) {
    record = record.getsObject(parts.remove(0));
  }
  return record == null? null: record.get(parts[0]);
}
  • Should be record.get(parts[0]). – Adrian Larson Nov 12 at 18:26
  • Also, returning record instead of explicitly null might introduce exception paths. – Adrian Larson Nov 12 at 18:33
  • @AdrianLarson Thanks, I was mobile, fixed the typo. Also, null is null. That said, I was also mobile, so one less null was less painful to do in the moment 😊. – sfdcfox Nov 12 at 19:00
  • If you declare a null SObject and try to cast it to a String, you will get an exception, no? – Adrian Larson Nov 12 at 19:17
  • @AdrianLarson Nope. A null is a null. The trick here is that we're upcasting to an Object, which we can then downcast to any type. Try this out: Object a; Integer b = (Integer)a; String c = (String)a; sObject d = (sObject)a; Account e = (Account)a; Or, even more fun: Integer a; String b; Object c = a; Object d = b; System.assertEquals(b, d); Nulls are only dangerous in terms of NullPointerException. – sfdcfox Nov 12 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.