Skip to main content
added 178 characters in body
Source Link
David Reed
  • 93.7k
  • 14
  • 90
  • 166

The solution turns out to be rather simple, if non-obvious: change cs.Case__c to cs.Case__r.

When you're dealing with relationship fields in Apex, the __c version of the lookup field is typed as an Id value. The __r variant of a lookup field to Some_Object__c is typed as a Some_Object__c, and can be assigned (or extracted as) an sObject class instance in Apex. The __r name of a child relationship is typed as a List<Child_Object__c> and can be interacted with as you'd expect.

These latter two are actually parallel to how standard fields work. Look at the difference in type between Contact.Account and Contact.AccountId, for example, and the type of Account.Contacts. You can safely do someContact.Account.Name (in both SOQL and Apex), and you can do Account a = someContact.Account, but you can't do either of those things with AccountId. It's just that the syntax doesn't make this distinction quite as clear when dealing with custom fields.

The solution turns out to be rather simple, if non-obvious: change cs.Case__c to cs.Case__r.

When you're dealing with relationship fields in Apex, the __c version of the lookup field is typed as an Id value. The __r variant of a lookup field to Some_Object__c is typed as a Some_Object__c, and can be assigned (or extracted as) an sObject class instance in Apex. The __r name of a child relationship is typed as a List<Child_Object__c> and can be interacted with as you'd expect.

These latter two are actually parallel to how standard fields work. Look at the difference in type between Contact.Account and Contact.AccountId, for example, and the type of Account.Contacts. It's just that the syntax doesn't make this distinction quite as clear when dealing with custom fields.

The solution turns out to be rather simple, if non-obvious: change cs.Case__c to cs.Case__r.

When you're dealing with relationship fields in Apex, the __c version of the lookup field is typed as an Id value. The __r variant of a lookup field to Some_Object__c is typed as a Some_Object__c, and can be assigned (or extracted as) an sObject class instance in Apex. The __r name of a child relationship is typed as a List<Child_Object__c> and can be interacted with as you'd expect.

These latter two are actually parallel to how standard fields work. Look at the difference in type between Contact.Account and Contact.AccountId, for example, and the type of Account.Contacts. You can safely do someContact.Account.Name (in both SOQL and Apex), and you can do Account a = someContact.Account, but you can't do either of those things with AccountId. It's just that the syntax doesn't make this distinction quite as clear when dealing with custom fields.

Source Link
David Reed
  • 93.7k
  • 14
  • 90
  • 166

The solution turns out to be rather simple, if non-obvious: change cs.Case__c to cs.Case__r.

When you're dealing with relationship fields in Apex, the __c version of the lookup field is typed as an Id value. The __r variant of a lookup field to Some_Object__c is typed as a Some_Object__c, and can be assigned (or extracted as) an sObject class instance in Apex. The __r name of a child relationship is typed as a List<Child_Object__c> and can be interacted with as you'd expect.

These latter two are actually parallel to how standard fields work. Look at the difference in type between Contact.Account and Contact.AccountId, for example, and the type of Account.Contacts. It's just that the syntax doesn't make this distinction quite as clear when dealing with custom fields.