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
ViciaVicia 

Trigger is active ,but nothing happen when I save record.

/*

Purpose: When there is nobody in Account Team,Users can not create contact under the account.

*/

trigger Contact_CanNotCreatetheContact on Contact (before insert) {

    for(Contact con:Trigger.new){

        List atmList = [SELECT UserId FROM AccountTeamMember  WHERE AccountId =:con.AccountId];

        if(atmList == NULL){

            Contact conOld = Trigger.newMap.get(con.Id);

            conOld.addError('There is no Account Team Member,you can not create account children!');

        }

   }

}

 

Here is my code,When, I create the contact under account with no account team member,there is no error message and nothing happend,Finally,I just save the new record.

 

Could you help my find the problem?

 

Thanks and best regards!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Trigger.newMap isn't populated for before insert triggers - the reason being that nothing has been inserted into the database, therefore none of your contacts actually has an id.  

 

You already have a reference to the con object, as part of iterating trigger.new, so you can just add the error to that.

 

That said, is the logic correct?  It looks like it will retrieve account team members for all contact accounts, and then error every one of them if there are no account team members.  However, if one account has team members then none of the contacts will error.

All Answers

bob_buzzardbob_buzzard

If a SOQL query returns no matches, this list won't be null, instead it will be empty.  Change:

 

 if(atmList == NULL){

 to

 

 if(atmList.isEmpty()){

I'm also duty bound to point out that you have a SOQL query embedded in your loop that iterates the trigger.new list - this means that if you have more than 100 records in the trigger you will break governor limits and should look at bulkifying your trigger:

 

http://wiki.developerforce.com/page/Best_Practice:_Bulkify_Your_Code

 

 

 

ViciaVicia

Hi,

As I have adjusted to this,but another error appears,System.NullPointerException: Attempt to de-reference a null object:

trigger Contact_CanNotCreatetheContact on Contact (before insert) {
Set<Id> accId = new Set<Id>();
for(Contact con:Trigger.new){
accId.add(con.AccountId);
}
List<AccountTeamMember> atmList = [SELECT UserId FROM AccountTeamMember WHERE AccountId IN:accId];
for(Contact con:Trigger.new){

if(atmList.isEmpty()){
Contact conOld = Trigger.newMap.get(con.Id);//*****Error:System.NullPointerException: Attempt to de-reference a null object:
conOld.addError('There is no Account Team Member,you can not create account children!');
}
}
}

 

could you help me find the reason

bob_buzzardbob_buzzard

Trigger.newMap isn't populated for before insert triggers - the reason being that nothing has been inserted into the database, therefore none of your contacts actually has an id.  

 

You already have a reference to the con object, as part of iterating trigger.new, so you can just add the error to that.

 

That said, is the logic correct?  It looks like it will retrieve account team members for all contact accounts, and then error every one of them if there are no account team members.  However, if one account has team members then none of the contacts will error.

This was selected as the best answer
ViciaVicia

Thank you very much,I have solve the problem, 

excellent explain.