You need to sign in to do that
Don't have an account?
Chitral 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();
}
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();
}
change account to accountid. Try below code and let me know.
All Answers
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?
change account to accountid. Try below code and let me know.
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 .
like in this case.
Try below code and let me know