You need to sign in to do that
Don't have an account?
Chitral Chadda
restriction on account
trigger trgset on contact (after insert)
{
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 ;
}
}
how can i restrict the code for a particular account only ...like if ther are 1000 account it will pick all ..how do i restrict it for a particular account ... in the suquery
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] )
{
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 ;
}
}
how can i restrict the code for a particular account only ...like if ther are 1000 account it will pick all ..how do i restrict it for a particular account ... in the suquery
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] )
I think you want to put account name in contact last name. than
try this one
Regards
Subhash
All Answers
list<string> accountIds = new list<string>();
for (contact c :trigger.new){
accountIds.add(c.AccountId);
}
and then in your query.... set the where clause to the accountId list ...
Select Name,(Select id ,LastName from Contacts where id in: trigger.new ) from Account where account.id in :accountIds]
I think you want to put account name in contact last name. than
try this one
Regards
Subhash
Ex. If any contact created under Account 'A' than you want to set last name = A
else if contact created under Account 'B' or any other than no change.
If this is true than you need to apply one more condtion on your account Query.Otherwise above code is change every contacts last name to their related account name.
Map<Id,Account> mapAccounts = new Map<Id,Account>([Select Id, Name From Account Where Id IN : setAccIds AND Name = 'A']);