-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standard field matching in apex code needs to account for variable data type #4
Comments
Another thing is that obviously, we are not the only app that has had similar requirements, so someone somewhere has already solved this problem and the approach should be: try to find an open-source library first, write our own parser only if really really necessary. Here are some example libraries we could use to generate an AST from apex https://www.npmjs.com/package/apex-parser Part of the scope of this issue is to do some R&D on the above and figure out which would could satisfy our needs. |
Also we can use the SymbolTable object to determine the type of a variable!
|
The MetadataComponentDependency Tooling API object currently supports custom fields, so it is possible to see if a custom field is used in apex code.
Standard fields support is not available, but this library provides "where is this used" information for standard fields in validation rules, workflow rules/updates, etc. This works pretty well because we use the field unique name to find references i.e
Lead.Industry
is the "id" reference in field updates that use this standard field.However, when it comes to apex code, it's very hard to tell if a standard field is actually being used.
What the library does at the moment is:
Lead.Industry
, we figure out the object name, in this caseLead
MetadataComponentDependency
to find all metadata that references this object. This works nicely because if an apex class uses theLead
object in any way, the API will find it.sfdc-soup/lib/sfdc_apis/metadata-types/StandardField.js
Line 156 in d784bc6
At the moment, all we do is we check if the body of the class contains any reference to the field name, i.e
Industry
. We do this by using a regular expressionsfdc-soup/lib/sfdc_apis/metadata-types/StandardField.js
Line 190 in d784bc6
This approach however is very naive because the apex class can use the word
industry
in hundreds of different contexts that are NOT the actual usage of theLead.Industry
field, such assThe only valid references should be
For this to work, we need some very robust/complex parsing logic. I'm open to using 3rd party libraries if that makes things easier. At a high level, the algorithm would have to be something like this:
lead
but notmyLead
lead
? if so, does it have the field name in the constructor i.eLead l = new Lead(Industry='cars')
industry
but notmyIndustry
we again need to ask the same questions? is this a comment, is it a variable? if it is a variable, does it belong to an object of thatlead
? If so, WE HAVE A MATCH!The text was updated successfully, but these errors were encountered: