+ Start a Discussion
Chandrashekhar GoudChandrashekhar Goud 

how can i call relationship object fields in trigger?

how can i call relationship object fields in trigger?
Vishal Negandhi 16Vishal Negandhi 16
You should be specifying more details, however at a high level this is how you do it

Supposing your trigger is on Contact and you want to access Account fields, this is how you do it.

trigger triggerContact on Contact (after insert){
     for(Contact cont : trigger.new){

The bold text is where I am fetching the account id for each contact in trigger. Similarly you can refer other fields. 

NOTE: for standard relationships, you simply use the . (dot) notation and refer the fields. 
However for a custom relationship, you need to use __c and __r

example: custom object patient__c is related to Account standard object (relationship field : Account__c). 
patient__r.Account__c - this will give me account id for a patient record. 

Hope this helps!
Chandrashekhar GoudChandrashekhar Goud
my object namee are Deal_Sheet__C ,Ng_Pipe__C, Meter_Name__C and Imbalances__c.
Deal_Sheet__C having lookup relationship with both NG_Pipe__c,Meter_Name__c
Meter_name__C having Lookup relation With NG_Pipe.
Ng_Pipe__C field  is the Controlling Field of Meter_Name__C  field in the deal_Sheet__C object

now i am writing trigger on Deal_Sheet object to update one of the field Meter_Name___C(field Name) in  Imbalaces__C object..
so how i call the field MeterName__c from Object Meter_Name__C in my Deal_Sheet Trigger...
How is object Imbalances__c related to Deal_Sheet__c??
on what condition you want to update field on Imbalances__c
(BTW, what i have understood is : Deal_Sheet__c has two llokup fields for NG_Pipe__c and Meter_Name__c with same names,
and Meter_NAme__c object has a lookup field for NG_Pipe__c with same name)

Correct me if i am wrong


to Refer any lookup field on any object, you need to write objectname__c.lookupFieldName__c -> to just get id.
And write objectname__c.lookupFieldName__r.field name of lookupObjectField -> to access other field
Chandrashekhar GoudChandrashekhar Goud
hi Mritzi,

  ya you are correcct ..there is no relation bn Imbalncces__C and Deal_Sheet__C ,
  using Deal_Sheet__c  Object i wanna Update the field Meter_name__C is in Imbalces__C object.