You need to sign in to do that
Don't have an account?
Chitral Chadda
trigger to populate parent field from child object using relationship query from "parent to child"
We have two standard objects account(parent) and contact (child ). i want to write a trigger to populate the lastname field of contact with the name field of account
//the trigger below performs the same task but soql query is from child to parent .
i want the trigger which performs the same work but using soql query ( relationship query ) from parent to child .
trigger trgSetLastName on Contact (after insert) {
List<Contact> lstConUpdate = new List<Contact>();
List<Contact> lstContact = [select id,Account.Name from Contact where id in: trigger.newmap.keyset()];
for(Contact con: lstContact){
con.LastName = con.Account.Name;
lstConUpdate.add(con);
}
if(lstConUpdate.size() > 0){
update lstConUpdate;
}
}
i want a trigger for this .help
//the trigger below performs the same task but soql query is from child to parent .
i want the trigger which performs the same work but using soql query ( relationship query ) from parent to child .
trigger trgSetLastName on Contact (after insert) {
List<Contact> lstConUpdate = new List<Contact>();
List<Contact> lstContact = [select id,Account.Name from Contact where id in: trigger.newmap.keyset()];
for(Contact con: lstContact){
con.LastName = con.Account.Name;
lstConUpdate.add(con);
}
if(lstConUpdate.size() > 0){
update lstConUpdate;
}
}
i want a trigger for this .help
The trigger you have posted does what you require - can you tell us why you need another version?
Use below code it will solve your problem.
Please mark my answer as a best solution to your question to help others if it solves your problem.
{
List<Contact> conToUpdate = new List<Contact>() ;
for(Account acc : [Select id,Name,(Select id,LastName from Contacts) from Account where Id in: trigger.newmap.keyset() ] )
{
for(Contact con : acc.Contacts)
{
con.LastName = acc.Name ;
conToUpdate.add(con) ;
}
}
if(conTOUpdate.size() > 0)
{
update conTOUpdate ;
}
}
when i save the contact the , lastname is not changed it remains the same like if i put account name as "ab".
when i go to contact and put lastname as "cd" on saving it . It remains "cd ",and not "ab"
And to update LastName of Contact when a new Contact is insert use you previous code in following way.
You have to write two trigger one is on Account and Second on Contact to acheive this.
Please mark my answer as a best solution to your question and Like it to help others if it solves your problem.
again the same issue lastname is not changed with the acoount name .
if lastname i save "ab" and account name is "ac"
on saving the contact the lastname remains the same "ab" , dosent change to "ac"
like initially if i save lastname of conatct as "a" and account name is saved as " b" .... after saving the contact the last name should be changed to "b".
{
List<Contact> conToUpdate = new List<Contact>() ;
for(Account acc : [Select Name,(Select id ,LastName from Contacts where id in: trigger.new ) from Account ] )
{
for(Contact con : acc.Contacts)
{
con.LastName = acc.Name ;
conToUpdate.add(con) ;
}
}
if(conTOUpdate.size() > 0)
{
update conTOUpdate ;
}
}
is this right ??
for(Account acc : [Select Name,(Select id ,LastName from Contacts where id in: trigger.new ) from Account wher account.id==contact.id] )
[Select Name,(Select id ,LastName from Contacts ) from Account where account.id=='xxxxxxxxx' ]
you can modify Where Condition for Account according to your need .
and if you want some more filter on your Inner Query then you can put those in following way
[Select Name,(Select id ,LastName from Contacts where LastName = 'Test Name' Limit 10) from Account wher account.id=='xxxxxxxxx' ] ;
hope it'll help you.
I think code witter by you is "for(Account acc : [Select Name,(Select id ,LastName from Contacts where id in: trigger.new ) from Account where account.id==contact.id] )" is Comparing Contact id with Account Id (please check).
Please mark my answer as a best solution to your question to help others if it solves your problem.
{
set<id> accSet=new set<id>();
List<Contact> conToUpdate = new List<Contact>() ;
if (Trigger.isAfter)
{
if (Trigger.isInsert)
{
system.debug('<<<<<<<<<trigger isInsert>>>>>>>: '+ Trigger.new);
for(Contact cnct:Trigger.new)
{
if(cnct.AccountId!=null)
{
accSet.add(cnct.AccountId);
}
}
system.debug('accSetId: '+ accSet);
List<contact> conList = new List<contact>();
List<Account> accList = new List<Account>();
IF(accSet <> null && accSet.size() > 0)
{
accList = [select id, name from account where id in:accSet];
system.debug('accList: '+ accList);
for(Contact cnct:trigger.new)
{
for(Account accnt:accList)
{
Contact cntObj = new Contact();
cntObj.id = cnct.id;
cntObj.LastName = accnt.name +'--' + cnct.LastName;
conToUpdate.add(cntObj) ;
}
}
}
system.debug('conTOUpdate: '+ conTOUpdate);
if(conTOUpdate.size() > 0)
{
update conTOUpdate ;
}
}
}
}