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
Chitral ChaddaChitral Chadda 

populate parent field from child field

we hv two objects(standard) one is account (parent) and second is contact(child)

i want to populate the name field of account with the lastname field of contact .




here is what i could code .
; but this is not working 



trigger trigsetLastName on Contact (after insert,after update)
{
   Map<id,account> parentacc = new Map<id,account>();
    List<id> listids = new List<id>();
   
   
    for( contact childobj :Trigger.new)
    {
            listids.add(childobj.account);   // error in this line
    }
   
    parentacc= new map<id,account> ([select id,Name ,(select id,LastName from contacts) from account where id in: listids]);
   
    for ( contact c :trigger.new)
    {
        account myacc = parentacc.get(c.account);
        myacc.Name=c.LastName;
    }
    Update parentacc.values();
    }
Best Answer chosen by Chitral Chadda
Pavan Kumar KajaPavan Kumar Kaja
Hi Chitral,

change account to accountid. Try below code and let me know.

trigger trigsetLastName on Contact (after insert,after update)
{
   Map<id,account> parentacc = new Map<id,account>();
    Set<id> listids = new Set<id>();
   
   
    for( contact childobj :Trigger.new)
    {
            listids.add(childobj.accountid);   // error in this line
    }
   
    parentacc= new map<id,account> ([select id,Name ,(select id,LastName from contacts) from account where id in: listids]);
   
    for ( contact c :trigger.new)
    {
        account myacc = parentacc.get(c.accountid);
        myacc.Name=c.LastName;
    }
    Update parentacc.values();
    }



All Answers

Vinnie BVinnie B
I got burned by this just recently.  Basically, when you're doing a series of 'adds' to a list in a loop, you have to create the new object prior to the add.  Try putting the following right before your error.

  Account anAccount = childobj.account

And then add 'anAccount' to the list.

I must admit I'm not sure this will work on the code you have here but it's an easy thing to try.  I'm also not convinced that your code is going to succeed in doing what you're trying to do.  The main part that's confusing me is the subquery to 'contacts'.  Why do you need that?  And what if there is more than one contact in the account?
Chitral ChaddaChitral Chadda
Forget about this code , my basic requirement is that i have to populate name field of account ( parent ) from the lastname field of contact ( child ) Isnt is posible ?
Pavan Kumar KajaPavan Kumar Kaja
Hi Chitral,

change account to accountid. Try below code and let me know.

trigger trigsetLastName on Contact (after insert,after update)
{
   Map<id,account> parentacc = new Map<id,account>();
    Set<id> listids = new Set<id>();
   
   
    for( contact childobj :Trigger.new)
    {
            listids.add(childobj.accountid);   // error in this line
    }
   
    parentacc= new map<id,account> ([select id,Name ,(select id,LastName from contacts) from account where id in: listids]);
   
    for ( contact c :trigger.new)
    {
        account myacc = parentacc.get(c.accountid);
        myacc.Name=c.LastName;
    }
    Update parentacc.values();
    }



This was selected as the best answer
Chitral ChaddaChitral Chadda
Error: Invalid Data. Review all error messages below to correct your data. Apex trigger trigsetLastName caused an unexpected exception, contact your administrator: trigsetLastName: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.trigsetLastName: line 17, column 1 again the same error
Chitral ChaddaChitral Chadda
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger trigsetLastName caused an unexpected exception, contact your administrator: trigsetLastName: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.trigsetLastName: line 17, column 1

again the same issue .
Chitral ChaddaChitral Chadda
@vinnie : yes  is there any way out where i could populate parent field from child field (object)( parent to child is one to many although ) , how to code for it then , to populate parent field)
like in this case.
Pavan Kumar KajaPavan Kumar Kaja
Chitral Chadda,

Try below code and let me know

trigger trigsetLastName on Contact (after insert,after update)
{
   Map<id,account> parentacc = new Map<id,account>();
    List<id> listids = new List<id>();
   
   
    for( contact childobj :Trigger.new)
    {
            listids.add(childobj.accountid);   // error in this line
    }
   
    parentacc= new map<id,account> ([select id,Name from account where id in: listids]);
   
    for ( contact c :trigger.new)
    {
        if(parentacc.containskey(c.accountid)){
            account myacc = parentacc.get(c.accountid);
            myacc.Name=c.LastName;
        }
    }
    Update parentacc.values();
    }


Chitral ChaddaChitral Chadda
i am able to save the contact now no error but when i go to the parent object (account ) and click on new account the name field is not populated with the last name field of contact which is the required purpose when i save it
Pavan Kumar KajaPavan Kumar Kaja
Skype: pavanthetech
Chitral ChaddaChitral Chadda
?
Chitral ChaddaChitral Chadda
the second last answer is somwhat related