function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NewToForceDevelopmentNewToForceDevelopment 

Help with update with parent object

I have custom object called payment and its parent is the standard object contact.  I am writing a trigger after insert of 'payment' object to update a field on the contact object.  I am able to get the payment object after insert in the trigger and also update it's properties but when i try to access the parent object (contact)  I am umable to what should the apex code for this look like.

 

trigger newPaymentTrigger on Payment__c (after insert) {    

Payment__c payment = [SELECT Id FROM Payment__c WHERE Id IN :Trigger.new];    

update payment;        

//Contact contact = [SELECT Id FROM Contact WHERE Id = :payment.Contact__c];

}

 

The commented line does not work because payment.Contact__c is null

 

The relation ship between the 2 objects is look up relation ship.

Best Answer chosen by Admin (Salesforce Developers) 
Jaffer Ali.Jaffer Ali.

First of all I am not able to understand the need of this trigger that why you want that.Can you please share any reason.

 

Please use below code it would work in your case. Field payment.Contact__c was missing in the code that's why you were receiving null value in it.

 

Payment__c payment = [SELECT Id , payment.Contact__c FROM Payment__c WHERE Id IN :Trigger.new];  

All Answers

Jaffer Ali.Jaffer Ali.

First of all I am not able to understand the need of this trigger that why you want that.Can you please share any reason.

 

Please use below code it would work in your case. Field payment.Contact__c was missing in the code that's why you were receiving null value in it.

 

Payment__c payment = [SELECT Id , payment.Contact__c FROM Payment__c WHERE Id IN :Trigger.new];  

This was selected as the best answer
NewToForceDevelopmentNewToForceDevelopment

Thanks and that worked. I need some fields in the parent object that needed to be updated.