-
Notifications
You must be signed in to change notification settings - Fork 558
Fields that are polymorphic fail due to first type being used. #137
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
Changes from all commits
260cfe1
9f7dede
c78614a
6fa7ff2
83508ff
823b772
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,7 +84,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr | |
| private Schema.ChildRelationship relationship; | ||
| private Map<Schema.ChildRelationship, fflib_QueryFactory> subselectQueryMap; | ||
|
|
||
| private String getFieldPath(String fieldName){ | ||
| private String getFieldPath(String fieldName, Schema.sObjectType relatedSObjectType){ | ||
| if(!fieldName.contains('.')){ //single field | ||
| Schema.SObjectField token = fflib_SObjectDescribe.getDescribe(table).getField(fieldName.toLowerCase()); | ||
| if(token == null) | ||
|
|
@@ -108,7 +108,22 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr | |
| } | ||
|
|
||
| if(token != null && i.hasNext() && tokenDescribe.getSOAPType() == Schema.SOAPType.ID){ | ||
| lastSObjectType = tokenDescribe.getReferenceTo()[0]; //if it's polymorphic doesn't matter which one we get | ||
| if(relatedSObjectType == null) { | ||
| lastSObjectType = tokenDescribe.getReferenceTo()[0]; //if it's polymorphic get the first one - user did not specify the one to use. | ||
| }else{ | ||
| List<Schema.sObjectType> relatedObjs = tokenDescribe.getReferenceTo(); //if it's polymorphic, it matters which one we use - i.e. Lead.Owner is GROUP|USER and each has different fields. | ||
| if(relatedObjs.size() > 1) { | ||
| String relatedSObjectName = fflib_SObjectDescribe.getDescribe(relatedSObjectType).getDescribe().getName(); | ||
| for(Schema.sObjectType sot : relatedObjs) { | ||
| if(fflib_SObjectDescribe.getDescribe(sot).getDescribe().getName() == relatedSObjectName){ | ||
| lastSObjectType = sot; | ||
| break; | ||
| } | ||
| } | ||
| }else{ | ||
| lastSObjectType = relatedOBjs[0]; //only one type returned, use it. | ||
| } | ||
| } | ||
| fieldPath.add(tokenDescribe.getRelationshipName()); | ||
| }else if(token != null && !i.hasNext()){ | ||
| fieldPath.add(tokenDescribe.getName()); | ||
|
|
@@ -122,6 +137,10 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr | |
|
|
||
| return String.join(fieldPath,'.'); | ||
| } | ||
|
|
||
| private String getFieldPath(String fieldName) { | ||
| return this.getFieldPath(fieldName, null); | ||
| } | ||
|
|
||
| @TestVisible | ||
| private static String getFieldTokenPath(Schema.SObjectField field){ | ||
|
|
@@ -197,16 +216,27 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr | |
| this.sortSelectFields = doSort; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Selects a single field from the SObject specified in {@link #table}. | ||
| * Selecting fields is idempotent, if this field is already selected calling this method will have no additional impact. | ||
| * @param fieldName the API name of the field to add to the query's SELECT clause. | ||
| **/ | ||
| public fflib_QueryFactory selectField(String fieldName){ | ||
| fields.add( getFieldPath(fieldName) ); | ||
| fields.add( getFieldPath(fieldName, null) ); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Selects a single field from the SObject specified in {@link #table}. | ||
| * Selecting fields is idempotent, if this field is already selected calling this method will have no additional impact. | ||
| * @param fieldName the API name of the field to add to the query's SELECT clause. | ||
| * @param relatedSObjectType the related sObjectType to resolve polymorphic object fields. | ||
| **/ | ||
| public fflib_QueryFactory selectField(String fieldName, Schema.sOBjectType relatedObjectType){ | ||
| fields.add( getFieldPath(fieldName, relatedObjectType) ); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Selects a field, avoiding the possible ambiguitiy of String API names. | ||
| * @see #selectField(String) | ||
|
|
@@ -760,6 +790,18 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr | |
| this.setMessage( 'Invalid field \''+fieldName+'\' for object \''+objectType+'\'' ); | ||
| } | ||
| } | ||
|
|
||
| public class InvalidRelationshipException extends Exception | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find anywhere else in this pull request where this exception is referenced. Perhaps it should be thrown if the passed in object type is not in the reference to list.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one!! @squattingdog could you check this one?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @squattingdog once you check this one we will merge it quickly. |
||
| { | ||
| private String fieldName; | ||
| private Schema.SObjectType objectType; | ||
| public InvalidRelationshipException(String fieldname, Schema.SObjectType objectType) { | ||
| this.fieldname = fieldname; | ||
| this.objectType = objectType; | ||
| this.setMessage( 'Not able to find related sObjectType for object \''+objectType+'\' denoted by field \''+fieldname+'\'' ); | ||
| } | ||
| } | ||
|
|
||
| public class InvalidFieldSetException extends Exception{} | ||
| public class NonReferenceFieldException extends Exception{} | ||
| public class InvalidSubqueryRelationshipException extends Exception{} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of interest why are we comparing the names and not just the sobject type objects?