+ Start a Discussion
LAMCORPLAMCORP 

Trigger to update a child custom object from a parent (Account) record

Dear all,

 

I am trying to write a simple trigger that will update child custom object fields from a parent (Account) record.

When I save the record it seems to make the fields null. Can anyone suggest anything? Probably something very simple I just can't see it.

 

trigger ElecPassME1ME2Team on Electricity_Meter__c (after insert, after update) {

    For(Electricity_Meter__c Elec: Trigger.New){
        Elec.Sales_Team__c = Elec.Account__r.Team_s_Dept_s__c;
        Elec.ME1__c = Elec.Account__r.ME1__c;
        Elec.ME2__c = Elec.Account__r.ME2__c;  
    }
Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989

Hi 

  Try this....

         

     

trigger ElecPassME1ME2Team on Electricity_Meter__c (before insert, before update) {

For(Electricity_Meter__c Elec: Trigger.New){
List<Account> account =[select Id ,Team_s_Dept_s__c,ME1__c,ME2__c from Account where Id=:Elec.Account__c];
Elec.Sales_Team__c = account.get(0).Team_s_Dept_s__c;
Elec.ME1__c = account.get(0).ME1__c;
Elec.ME2__c = account.get(0).ME2__c;

}
}

 

Did this post answers your problem ..If so please mark it solved so that It can be refered .

 

thanks

asish

All Answers

asish1989asish1989

Hi 

  Try this....

         

     

trigger ElecPassME1ME2Team on Electricity_Meter__c (before insert, before update) {

For(Electricity_Meter__c Elec: Trigger.New){
List<Account> account =[select Id ,Team_s_Dept_s__c,ME1__c,ME2__c from Account where Id=:Elec.Account__c];
Elec.Sales_Team__c = account.get(0).Team_s_Dept_s__c;
Elec.ME1__c = account.get(0).ME1__c;
Elec.ME2__c = account.get(0).ME2__c;

}
}

 

Did this post answers your problem ..If so please mark it solved so that It can be refered .

 

thanks

asish

This was selected as the best answer
LAMCORPLAMCORP

Hi,

 

Thanks for your help and quick response. It works!!

 

Can I ask what I was doing wrong?

 

I see you have added a list with nested SOQL query.

asish1989asish1989

Hi

    You have written this line

       Elec.Sales_Team__c = Elec.Account__r.Team_s_Dept_s__c;

       This is just a field binding . if you require a perticular  value of Account record  then You need to specify by writting Query .

       Anyways your problem is solved that;s a big thing I think .

 

 

 

Thanks

asish

 

 

LAMCORPLAMCORP

Many thanks.

 

Good to know for future use.

jee 547jee 547

i want test class for this one as soon as possible sir

jee 547jee 547

trigger FiniPopulateCoachingInfo on Fini_Coaching_Information_del__c(before insert,before update)
{

For(Fini_Coaching_Information_del__c coach: Trigger.New)
{
if(trigger.isInsert || trigger.isUpdate){
List<Account> account =[select Id ,Fini_Marital_Status__pc,Preferred_Name__pc,FirstName,LastName,PersonMobilePhone,
Fini_Time_Zone__c,Phone,Fini_Alternate_Phone__pc,Fini_Can_Recieve_Texts__pc,PersonEmail,
Fini_Alternate_Email__pc from Account where Id=:coach.Fini_Account__c];
coach.Fini_Marital_Status__c =account.get(0).Fini_Marital_Status__pc;
coach.Fini_First_Name__c=account.get(0).FirstName;
coach.Fini_Last_Name__c=account.get(0).LastName;
coach.Fini_Time_Zone__c=account.get(0).Fini_Time_Zone__c;
coach.Fini_Primary_Phone__c=account.get(0).Phone;
coach.Fini_Mobile_Phone__c=account.get(0).PersonMobilePhone;
coach.Fini_Alternate_Phone__c=account.get(0).Fini_Alternate_Phone__pc;
coach.Fini_Primary_Billing_Email__c=account.get(0).PersonEmail;
coach.Fini_Alternate_Email__c=account.get(0).Fini_Alternate_Email__pc;
coach.Fini_Can_Receive_Texts__c=account.get(0).Fini_Can_Recieve_Texts__pc;

}
}
}