Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to troubleshoot the 'Dependent class is invalid and needs > recompilation' exception?

Asked by Clare Matthews May 4, 2023 2.3K views 2 answers
Share

About this question

I create a trigger on User that only inserts/updates another table when there are changes/insertions into the Department field that belong to User. It's working fine except sometimes I receive this exception in my email:


Apex script unhandled trigger exception by user/organisation: 0053000000xxx/00DQ0000003Lxxx Source organisation: 00D00000000hxxx (null) MyUpdateTrigger: execution of BeforeUpdate

caused by: line 55, column 24: Dependent class is invalid and needs recompilation: MyHelperClass: line 28, column 30: No such column 'Department' on entity 'User'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.


I check the line number: it points to an empty line.
The Department api name has no __c at the end.
I don't receive an email on this error when I do a manual test. I just received an email occasionally, meaning someone is firing the trigger.
Any ideas about the meaning of that error? How to troubleshoot this type of error?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 9, 2024


The 'Dependent class is invalid and needs recompilation' exception is common in database systems like Oracle when a dependent object, such as a stored procedure or view, references another object that has changed. Here’s a detailed guide to troubleshoot and resolve this issue:

Steps to Troubleshoot and Resolve

Identify Invalid Objects:

Query the USER_OBJECTS or ALL_OBJECTS dictionary views to find invalid objects.

  SELECT object_name, object_typeFROM user_objectsWHERE status = 'INVALID';Check Dependencies:

Use the USER_DEPENDENCIES or ALL_DEPENDENCIES views to find the dependencies of the invalid objects.

  SELECT name, type, referenced_name, referenced_typeFROM user_dependenciesWHERE name IN (SELECT object_name FROM user_objects WHERE status = 'INVALID');Recompile Invalid Objects:

Recompile the invalid objects using the ALTER statement.

  -- For a specific procedureALTER PROCEDURE procedure_name COMPILE;-- For a specific packageALTER PACKAGE package_name COMPILE;-- For all invalid objects
  BEGIN    FOR rec IN (SELECT object_name, object_type FROM user_objects WHERE status = 'INVALID') LOOP        EXECUTE IMMEDIATE 'ALTER ' || rec.object_type || ' ' || rec.object_name || ' COMPILE';    END LOOP;END;

Check Compilation Errors:

If recompilation fails, check for compilation errors in the USER_ERRORS view.

  SELECT name, type, line, position, textFROM user_errorsWHERE name IN (SELECT object_name FROM user_objects WHERE status = 'INVALID');Resolve the Underlying Issues:

Address any issues identified from the compilation errors. This might involve correcting syntax errors, resolving missing references, or fixing logic errors in the dependent objects.

Recompile Again:

After resolving the errors, recompile the objects again.

Automate Recompilation (Optional):

If there are many invalid objects, you can use a script to recompile them automatically.

  BEGIN    FOR rec IN (SELECT object_name, object_type FROM user_objects WHERE status = 'INVALID') LOOP        EXECUTE IMMEDIATE 'ALTER ' || rec.object_type || ' ' || rec.object_name || ' COMPILE';    END LOOP;END;Example WorkflowIdentify Invalid Objects:
  SELECT object_name, object_typeFROM user_objectsWHERE status = 'INVALID';Output:
  markdownOBJECT_NAME     OBJECT_TYPE---------------------------MY_PROCEDURE    PROCEDUREMY_PACKAGE      PACKAGECheck Dependencies:
  SELECT name, type, referenced_name, referenced_typeFROM user_dependenciesWHERE name IN ('MY_PROCEDURE', 'MY_PACKAGE');

Output:

  NAME           TYPE       REFERENCED_NAME   REFERENCED_TYPE------------------------------------------------------------MY_PROCEDURE   PROCEDURE  MY_TABLE          TABLEMY_PACKAGE     PACKAGE    MY_VIEW           VIEW
Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.