You need to sign in to do that
Don't have an account?
MattMet86
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.......
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
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
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
FYI, I am a new developer so this is probably something simple.
Thx
Can you please check Validation rules on Account_Counselor__c object.
Share the exact error message you are getting right now.
Thanks,
Vishal
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.
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
Complete code with error handling for SOQL queries that return no records.