+ Start a Discussion
MattMet86MattMet86 

Can't get apex error message to display.

I can't get my error message to display. I just keep getting the default error when no rows are returned.  Any help on what I am doing wrong would be appreciated. 
 
trigger AccountTeam on Account_Counselor__c (after insert, after update, before delete) {

                
    if(Trigger.IsInsert) {
        for (Account_Counselor__c ac : Trigger.new) {
        
        //Check that HUB exists
        List<BCS__c> bcscheck =  [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];

        if (bcscheck.size() > 0) {
        } else {
            ac.addError('No User linked to this Hub.');
        }
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                
        //Check that public group exists
        List<Group> groupcheck = [SELECT Id from Group WHERE Name = :Acc.Name];

        if (groupcheck.size() > 0) {
        } else {
             ac.addError('No Public Group found for this account.');
        }
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put Names in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;
                string accID = ac.Account__c;
                
            system.debug('UserID='+userId);
            system.debug('GroupID='+groupId);
            system.debug('AccountID='+ac.Account__c);     

//and so on and so on.......

 
Best Answer chosen by MattMet86
Vishal_GuptaVishal_Gupta
Hi Matt,

Please use the below line of codes  :

List<Group> lstGP = [SELECT Id from Group WHERE Name = :Acc.Name];
    if(lstGP.size() < 1)
    {
        //add your custoom error message
    }
    else
    {
        //Put IDs in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;   
                
            system.debug('UserID='+userId);
            system.debug('GroupID='+groupId);
            system.debug('AccountID='+ac.Account__c);     
            
    }

Please let me know if I can help you more.

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi Matt,

What default error you are getting, actually its depend on the order of execution. your code is executing after insert,after update but before that system execute the system validations if any exists on that object as well the code written in before insert and before update trigger, so there might be a chance that your code is not failing due to some validation or some code written in before(insert,update) trigger of Account_Counselor__c.

Please verify or share the default error message with us, so we can provide you the reason.

Thanks,
Vishal
Vishal_GuptaVishal_Gupta
Correcting my last statement of my last post :

so there might be a chance that your code is failing due to some validation or some code written in before(insert,update) trigger of Account_Counselor__c.

Thanks,
Vishal
 
MattMet86MattMet86
Here is my entire code for the trigger. There are no other triggers. 
FYI, I am a new developer so this is probably something simple. 
 
trigger AccountTeam on Account_Counselor__c (after insert, after update, before delete) {

                
    if(Trigger.IsInsert) {
        for (Account_Counselor__c ac : Trigger.new) {
        
        //Check that HUB exists
        List<BCS__c> bcscheck =  [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];

        if (bcscheck.size() > 0) {
        } else {
            ac.addError('No User linked to this Hub.');
        }
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                
        //Check that public group exists
        List<Group> groupcheck = [SELECT Id from Group WHERE Name = :Acc.Name];

        if (groupcheck.size() > 0) {
        } else {
             ac.addError('No Public Group found for this account.');
        }
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put Names in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;
                string accID = ac.Account__c;
                
            system.debug('UserID='+userId);
            system.debug('GroupID='+groupId);
            system.debug('AccountID='+ac.Account__c);     

                //Add user to AccountTeam
                AccountTeamMember Team    = new AccountTeamMember();
                Team.AccountId            = ac.Account__c;
                Team.TeamMemberRole       = ac.Role__c;
                Team.UserId               = userID;
                insert Team;
                
                //Call AddAccountShare Method
                BCI_Helper_Methods.AddAccountShare(userID,accID);
                
                //Call Future method
                BCI_Helper_Methods.AddMember(userID,groupID);
        }
        }
        
        if(Trigger.IsUpdate) {
        for (Account_Counselor__c ac : Trigger.new) {
                
                //Convert IDs
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put IDs in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;   
                string accID = ac.Account__c;
                                
            system.debug('UserID='+userId);
            system.debug('AccountID='+ac.Account__c);   
                
                //Overwrite Account Team
                AccountTeamMember Team    = new AccountTeamMember();
                Team.AccountId            = ac.Account__c;
                Team.TeamMemberRole       = ac.Role__c;
                Team.UserId               = userID;
                insert Team;

        }
        }
        
        if(Trigger.IsDelete) {
         for (Account_Counselor__c ac : Trigger.old) {
                
                //Convert IDs
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put IDs in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;   
                
            system.debug('UserID='+userId);
            system.debug('GroupID='+groupId);
            system.debug('AccountID='+ac.Account__c);     
            
             //Delete AccountTeam
                List<AccountTeamMember> delTeam = [SELECT Id From AccountTeamMember WHERE AccountId = :ac.Account__c AND UserId = :userId];
                system.debug('delteam='+delTeam);
                delete delTeam;

            //Delete PublicGroup
                BCI_Helper_Methods.RemoveMember(userID,groupID);
        }
    }
 }

 
William TranWilliam Tran
Try putting in a Try catch finally block and adding/displaying your custom messages in the catch/finally blocks.

Thx
MattMet86MattMet86
Still no joy but I am probably doing it wrong. I did chance the trigger to (before Insert, before update, before delete) while trying to figure out why this message won't display. 
 
trigger AccountTeam on Account_Counselor__c (before insert, before update, before delete) {

                
    if(Trigger.IsInsert) {
        for (Account_Counselor__c ac : Trigger.new) {
        
        //Check that HUB exists
try {
    BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];
} catch(DmlException e) {
    System.debug('The following exception has occurred: ' + e.getMessage());
} catch(Exception e) {
    System.debug('The following exception has occurred: ' + e.getMessage());
} 
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                
        //Check that public group exists
try {
    Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
} catch(DmlException e) {
    System.debug('The following exception has occurred: ' + e.getMessage());
} catch(Exception e) {
    System.debug('The following exception has occurred: ' + e.getMessage());
} 
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put Names in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;
                string accID = ac.Account__c;

// and so on ....

 
Vishal_GuptaVishal_Gupta
Hi Matt,

Can you please check Validation rules on Account_Counselor__c  object.

Share the exact error message you are getting right now.

Thanks,
Vishal
MattMet86MattMet86
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountTeam caused an unexpected exception, contact your administrator: AccountTeam: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.AccountTeam: line 26, column 1

The error message is being generated as there is no Public Group named the same as the account. This is correct behavior and I do want an error but I want to tell the User via the UI why they can't add the Team Member. 
 
Vishal_GuptaVishal_Gupta
Hi Matt,

Please use the below line of codes  :

List<Group> lstGP = [SELECT Id from Group WHERE Name = :Acc.Name];
    if(lstGP.size() < 1)
    {
        //add your custoom error message
    }
    else
    {
        //Put IDs in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;   
                
            system.debug('UserID='+userId);
            system.debug('GroupID='+groupId);
            system.debug('AccountID='+ac.Account__c);     
            
    }

Please let me know if I can help you more.

Thanks,
Vishal
This was selected as the best answer
MattMet86MattMet86
That worked Vishal

Complete code with error handling for SOQL queries that return no records. 
trigger AccountTeam on Account_Counselor__c (before insert, before update, before delete) {


                
    if(Trigger.IsInsert) {
        for (Account_Counselor__c ac : Trigger.new) {

                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c LIMIT 1];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                
                
        //Prevent duplicates
        List<AccountTeamMember> TeamCheck = [SELECT Id FROM AccountTeamMember WHERE UserId = :BCS.User__c AND AccountId = :ac.Account__c];
            
            if(TeamCheck.size() > 0)
            {
                ac.Hub_Name__c.adderror('This Team Member is already on the account!');
            }
                else
                {
    
                //Check that public group exists
                List<Group> GroupCheck = [SELECT Id from Group WHERE Name = :Acc.Name];
                
                if(GroupCheck.size() < 1)
                {
                    ac.Account__c.adderror('There is no Public Group that matches this Account name!');
                }
                        else
                        {
                            Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
                        
                            //Put Names in variables
                            string userID = BCS.User__c;
                            string groupID = GP.Id;
                            string accID = ac.Account__c;
                         
                         //Check that a User lookup exists on Hub record.
                         if(userId == Null)
                         {
                            ac.Hub_Name__c.adderror('There is no User linked to this Hub!');
                         }
                         else
                         {  
                                system.debug('UserID='+userId);
                                system.debug('GroupID='+groupId);
                                system.debug('AccountID='+ac.Account__c);     
                    
                                    //Add user to AccountTeam
                                    AccountTeamMember Team    = new AccountTeamMember();
                                    Team.AccountId            = ac.Account__c;
                                    Team.TeamMemberRole       = ac.Role__c;
                                    Team.UserId               = userID;
                                    insert Team;
                                    
                                    //Call AddAccountShare Method
                                    BCI_Helper_Methods.AddAccountShare(userID,accID);
                                    
                                    //Call Future method
                                    BCI_Helper_Methods.AddMember(userID,groupID);
                    }
            }
        }
        }
        }
        
        if(Trigger.IsUpdate) {
        for (Account_Counselor__c ac : Trigger.new) {
                
                //Convert IDs
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put IDs in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;   
                string accID = ac.Account__c;
                                
            system.debug('UserID='+userId);
            system.debug('AccountID='+ac.Account__c);   
                
                //Overwrite Account Team
                AccountTeamMember Team    = new AccountTeamMember();
                Team.AccountId            = ac.Account__c;
                Team.TeamMemberRole       = ac.Role__c;
                Team.UserId               = userID;
                insert Team;

        }
        }
        
        if(Trigger.IsDelete) {
         for (Account_Counselor__c ac : Trigger.old) {
                
                //Convert IDs
                BCS__c BCS = [SELECT User__c, Name FROM BCS__c WHERE id = :ac.Hub_Name__c];
                Account Acc = [SELECT Name FROM Account WHERE Id = :ac.Account__c];
                Group GP = [SELECT Id from Group WHERE Name = :Acc.Name];
            
                //Put IDs in variables
                string userID = BCS.User__c;
                string groupID = GP.Id;   
                
            system.debug('UserID='+userId);
            system.debug('GroupID='+groupId);
            system.debug('AccountID='+ac.Account__c);     
            
             //Delete AccountTeam
                List<AccountTeamMember> delTeam = [SELECT Id From AccountTeamMember WHERE AccountId = :ac.Account__c AND UserId = :userId];
                system.debug('delteam='+delTeam);
                delete delTeam;

            //Delete PublicGroup
                BCI_Helper_Methods.RemoveMember(userID,groupID);
        }
    }
 }