0

I have a custom object A, that has a currency field and has a look-up relationship to object B.

Now within object B, I am trying to access the value of the currency field of object 'A'. What is the best way to define a field to capture that calue?

I tried to use a currency type field and performed 'objectA__C.Cost__A', but got an error stating field 'objectA__C' does not exist.

8
  • did you try objectA__r.Cost__A?
    – m Peixoto
    Commented Nov 4, 2019 at 15:50
  • Thanks Peixoto. Doesnt work :-(.
    – Spiderman
    Commented Nov 4, 2019 at 15:59
  • 1
    Which object is the parent, and which is the child? The child object is the one that contains the actual relationship field. E.g. Opportunity is a child object of Account because the Opportunity object has the relationship field AccountId.
    – Derek F
    Commented Nov 4, 2019 at 16:03
  • Also, are you trying to get at this value (these values?) via a formula field/workflow/etc..., or via a trigger?
    – Derek F
    Commented Nov 4, 2019 at 16:15
  • Thanks Derek and Phil, I am trying to get the values via formula field and not trigger. I am trying to avoid writing triggers to be honest.
    – Spiderman
    Commented Nov 4, 2019 at 16:17

1 Answer 1

3

Salesforce automatically creates Child Relationships (effectively "reverse relationships") when you create a lookup field. Where you have a relationship between A__c and B__c having a lookup field on A__c you get the child relationship defined too. The specific name of this relationship is set when you define the lookup field.

So, with A__c having field "Some_B__c" as a lookup to "B__c" you can query the B object and get its related A's like:

SELECT Id, ..., (SELECT Id, Currency_Field__c FROM A__r) FROM B__c WHERE Id = :someId

This will load your B__c instance such that you have an ARRAY of A__c instances (all A__c instances that reference the given B__c instance) that you can access and get the currency field from using something like:

for (B__c b : [SELECT ... ]) { // Using the select from above you will get one match
    for (A__c a : b.A__r) {
        Currency c = a.Currency_Field__c;
        ... // Do something with the related A__c's currency
    }
}
0

You must log in to answer this question.

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