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 

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] )
Best Answer chosen by Chitral Chadda
Subhash GarhwalSubhash Garhwal
Hi Chitral,
I think you want to put account name in contact last name. than
try this one
trigger trgset on contact (before insert)

{
    
    //Set to hold account ids
    Set<Id> setAccIds = new Set<Id>();
    for(Contact con : trigger.new) {
    
    //Check for Account Id
    if(con.AccountId != null)
        setAccIds.add(con.AccountId);
     }

     Map<Id,Account> mapAccounts = new Map<Id,Account>([Select Id, Name From Account Where Id IN : setAccIds]);

     for(Contact con : trigger.new) {
    
    //Check for Account Id
    if(con.AccountId != null && mapAccounts.containsKey(con.AccountId))
        con.LastName = mapAccounts.get(con.AccountId).Name;
     }

  }

Regards
Subhash

All Answers

Jim JamJim Jam
You need to first get the account id's from the trigger.new list. something like ..

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]

Subhash GarhwalSubhash Garhwal
Hi Chitral,
I think you want to put account name in contact last name. than
try this one
trigger trgset on contact (before insert)

{
    
    //Set to hold account ids
    Set<Id> setAccIds = new Set<Id>();
    for(Contact con : trigger.new) {
    
    //Check for Account Id
    if(con.AccountId != null)
        setAccIds.add(con.AccountId);
     }

     Map<Id,Account> mapAccounts = new Map<Id,Account>([Select Id, Name From Account Where Id IN : setAccIds]);

     for(Contact con : trigger.new) {
    
    //Check for Account Id
    if(con.AccountId != null && mapAccounts.containsKey(con.AccountId))
        con.LastName = mapAccounts.get(con.AccountId).Name;
     }

  }

Regards
Subhash

This was selected as the best answer
Chitral ChaddaChitral Chadda
Would this code restrict for a particular account only ??
Subhash GarhwalSubhash Garhwal
What you mean particular account only??
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']);