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
Pankaj Sharma 53Pankaj Sharma 53 

Update Child Object Field value

I have two custom objects related with lookip. I want to update all Child objects address field according to Parents. I know for that, I have to write a trigger so plz help me.
Best Answer chosen by Pankaj Sharma 53
suresh sanneboina 4suresh sanneboina 4
Hi,

Please find the below code.

Account and contact has lookup relation. When i update the accounts billing street the same is applied on the contacts mailing street.
 
Trigger updatechilds on Account(after update)
{
    Set<Id> setaccId=new Set<Id>();
    for(Account acc:Trigger.new)
    {
        if(acc.BillingStreet != Trigger.oldMap.get(acc.Id).BillingStreet)
        {
            setaccId.add(acc.Id);
        }
    }
    
    List<Contact> lstcontact=[Select Id,MailingStreet,AccountId from contact where AccountId IN :setaccId];
    if(!lstcontact.isEmpty())
    {
        List<Contact> lstUpdContacts=new List<Contact>();
        for(Contact con:lstcontact)
        {
            Contact contact=new Contact(id=con.Id);
            contact.MailingStreet =Trigger.newMap.get(con.AccountId).BillingStreet;
            lstUpdContacts.add(contact);
        }
        
        if(!lstUpdContacts.isEmpty())
        {
            update lstUpdContacts;
        }
    }
}

 

All Answers

suresh sanneboina 4suresh sanneboina 4
Hi,

Please find the below code.

Account and contact has lookup relation. When i update the accounts billing street the same is applied on the contacts mailing street.
 
Trigger updatechilds on Account(after update)
{
    Set<Id> setaccId=new Set<Id>();
    for(Account acc:Trigger.new)
    {
        if(acc.BillingStreet != Trigger.oldMap.get(acc.Id).BillingStreet)
        {
            setaccId.add(acc.Id);
        }
    }
    
    List<Contact> lstcontact=[Select Id,MailingStreet,AccountId from contact where AccountId IN :setaccId];
    if(!lstcontact.isEmpty())
    {
        List<Contact> lstUpdContacts=new List<Contact>();
        for(Contact con:lstcontact)
        {
            Contact contact=new Contact(id=con.Id);
            contact.MailingStreet =Trigger.newMap.get(con.AccountId).BillingStreet;
            lstUpdContacts.add(contact);
        }
        
        if(!lstUpdContacts.isEmpty())
        {
            update lstUpdContacts;
        }
    }
}

 
This was selected as the best answer
Pankaj Sharma 53Pankaj Sharma 53
Hi Suresh 


Thanks for this code this is working fine.

but in my case i have two custom object 
Policy_Holder__c(Parent)and Premium_Amount__c and Duration__c is custom field
Life_Insurance__c (child)and Premium_Amount__c and Duration__c is custom field
Policy_holder_name__c is lookup field


and i want to populate both field value of child from parent

I tried but failed please suggest
suresh sanneboina 4suresh sanneboina 4
Hi,

Please find the code below and let me know if you face any issues
Trigger updatechilds on Policy_Holder__c(after update)
{
	Set<Id> setaccId=new Set<Id>();
    for(Policy_Holder__c acc:Trigger.new)
    {
        if((acc.Premium_Amount__c != Trigger.oldMap.get(acc.Id).Premium_Amount__c) || (acc.Duration__c != Trigger.oldMap.get(acc.Id).Duration__c))
        {
            setaccId.add(acc.Id);
        }
    }
    
    List<Life_Insurance__c> lstcontact=[Select Id,Premium_Amount__c,Duration__c,Policy_holder_name__c from Life_Insurance__c where Policy_holder_name__c IN :setaccId];
    if(!lstcontact.isEmpty())
    {
        List<Life_Insurance__c> lstUpdContacts=new List<Life_Insurance__c>();
        for(Life_Insurance__c con:lstcontact)
        {
            Life_Insurance__c contact=new Life_Insurance__c(id=con.Id);
            contact.Premium_Amount__c =Trigger.newMap.get(con.Policy_holder_name__c).Premium_Amount__c;
			contact.Duration__c =Trigger.newMap.get(con.Policy_holder_name__c).Duration__c;
            lstUpdContacts.add(contact);
        }
        
        if(!lstUpdContacts.isEmpty())
        {
            update lstUpdContacts;
        }
    }
}

 
Pankaj Sharma 53Pankaj Sharma 53
Hi Suresh

I checked this is not working.

 
suresh sanneboina 4suresh sanneboina 4
any reason