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
  • 1
    Another possibility would be to try to detect the condition prior to the DML operation. Commented Jan 13, 2014 at 13:39

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .