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
Vamsi VaddipalliVamsi Vaddipalli 

Write a Trigger to update parent record when child got created.

Hi,
This is Vamsi need one help friends
I wrote a trigger to upadte parent record while child got created. But it is not working can yoy guide me.

trigger InTri on APEX_Invoice__c (after insert) {

List<APEX_Invoice__c> inn = new List<APEX_Invoice__c>();
List<APEX_Customer__c> cuu = new List<APEX_Customer__c>();
    List<APEX_Customer__c> cus = new List<APEX_Customer__c>();
    List<ID> ids = new List<ID>();
    for(APEX_Invoice__c obj1:Trigger.new)
    {
        ids.add(obj1.APEX_Customer__r.id);
    }
cuu = [SELECT id,Name,APEX_Customer_Status__c FROM APEX_Customer__c WHERE ID IN: ids];
for(APEX_Customer__c obj:cuu)
{
obj.APEX_Customer_Status__c = 'Paid';
   cus.add(obj); 
}
 update cus; 
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Vamsi,

Greetings to you!

You need to use the API name of the parent field in child object while getting the id. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger InTri on APEX_Invoice__c (after insert) {
    
    List<Id> ids = new List<Id>();
    List<APEX_Customer__c> cus = new List<Book__c>();
    
    for(APEX_Invoice__c obj1 : trigger.new){
        ids.add(obj1.Parent_In_Child__c);  //API name of parent field in child object
    }
    
    for(APEX_Customer__c obj : [SELECT Id, Name, APEX_Customer_Status__c FROM APEX_Customer__c WHERE Id IN :ids]){
            obj.APEX_Customer_Status__c='Paid';
            cus.add(obj);
    }
    if(cus.size()>0){
        UPDATE cus;        
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Damodhar Reddy 7Damodhar Reddy 7
Hi,

 I want triggers friends
Account and Contact i have created one checkbox field.when ever account checkbox is checked automatically contact check box also checked.
Contact checkbox is checked automatically account checkbox should be checked.

how to write a trigger for this requirement?
 
Ajay K DubediAjay K Dubedi
Hi Vamsi,
Try this below code:
trigger InTri on APEX_Invoice__c (after insert) {

    List<APEX_Invoice__c> inn = new List<APEX_Invoice__c>();
    List<APEX_Customer__c> cuu = new List<APEX_Customer__c>();
        List<APEX_Customer__c> cus = new List<APEX_Customer__c>();
        List<ID> ids = new List<ID>();
        for(APEX_Invoice__c obj1:Trigger.new)
        {
            if(obj1.APEX_Customer__c != NULL)
            {
                ids.add(obj1.APEX_Customer__c);
            }
        }
    cuu = [SELECT id,Name,APEX_Customer_Status__c FROM APEX_Customer__c WHERE ID IN: ids];
    for(APEX_Customer__c obj:cuu)
    {
        obj.APEX_Customer_Status__c = 'Paid';
        cus.add(obj);
    }
    if(cus.size()>0){
        update cus;
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi