4

My use case is to create a more specific error message when user tries to create a chattergroup(in the background by apex).

error message is for when user has reached the limit of 100 chattergroups he/she can join.Currently i am simply checking against error message to identify this.

Not sure whether its a good way to do this,because if sfdc changes message this doesnt work.is there any thing like an error code /or something more reliable to check this?

Code is below

How i am doing is in a before insert trigger(of a custom record), i do

 try{
                //create chatter groups 
                Database.Saveresult[] results = Database.insert(chatterGroups2Create, false);

                for (Integer i = 0; i < results.size(); i++)
                {
                    if (!results[i].isSuccess())
                    {
                        if ( results[i].getErrors()[0].getMessage() == 'Maximum per user collaboration group limit reached.')
                        {
                            newBets[i].addError('more specific error message');
                        }
                        else
                        {
                            newBets[i].addError(results[i].getErrors([0].getMessage());
                        }
                    }
                    else
                    {
                        //do something else
                    }
                }

            } catch (Exception e)
            {
                throw e;
            }
  • 1
    Another possibility would be to try to detect the condition prior to the DML operation. – Peter Knolle Jan 13 '14 at 13:39
1

Can you dump the value of results[i].getErrors()[0]to verify what all it contains. I am pretty sure it has an error message literal similar To LIMIT_EXCEEDED but I can't find the actual value/ link where it's documented. As a matter of good programming practice, I believe rather than doing a string comparison against the error message, you should check against the error code that you are receiving.

This way, even if Salesforce changes the error code to error message text mapping (which I doubt), you would still be safe as you'd be using the code and not the error message.

  • this is the dump ,Insert failed. First exception on row 0; first error: LIMIT_EXCEEDED, Maximum per user collaboration group limit reached.: []. – sfdc99999 Jan 13 '14 at 14:07
  • Its same as limit_exceeded --so don't think will be good enough to differentiate from possible limit exceptions – sfdc99999 Jan 13 '14 at 14:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.