2

I try to do my dynamic filtering on the form based on an object values, entered by a user using apex:inputField. The object stays in memory and never gets inserted to the DB.

When I try to assign some default value to the non-writeable field, like CreatedDate, in the object (which never gets inserted), it throws exception "Field CreatedDate is not editable".

Why this exception is thrown at this level and not when the object gets inserted? It there a way to overcome this, a way to shift this check on db-access level, so to say?

Illustration code, this is not the real code, it is just easier to explain this way:


public Map<String, String> filters = new Map<String, String>(); // suppose we have this map of "filterField" and Default filter values filled.
public String sobjtype = 'MyCustomObj__c'; // This is a type of objects shown on the page, dynamic.
public SObject filterObj = Schema.getGlobalDescribe().get(sobjtype).newSObject();
//Now assign the filterObj default values
for (String key : filters.keySet()) {
// THIS LINE THROWS AN ERROR ON NON-EDITABLE FIELD, LIKE CREATEDDATE.
   filterObj.put(key, filters.get(key)); // But I do not try to insert it to the DB, simple assignment.
}

Then in Visual Force I use apex:repeat on filters to create input fields with all the default for VF input abilities, like date pickers, reference pickers and so on using the filterObj.

  • I experienced something similar recently with a page that uploads attachments, if the attachment was bigger than the limit we have allocated, it would throw an exception before entering the "commit" part of the transaction, it happened when the object was being loaded in memory. I am also interested on the explanation for this. On the other hand, the solution for your issue is simple, simply don't try to set the created date as it's an audit field. – PepeFloyd Jun 6 '14 at 12:05
  • I am not sure if that is the same. I want to make users be able to filter by the fields which a user defines. Also I do not even know the object type, so I use SObject.put(). Which throws me the error. Yes, the easy way is to exclude the object's non-editable fields from filter fields set. But I am just curious if there is a solution/work-around to this). – Volodymyr B Jun 6 '14 at 12:17
  • 2
    The problem is that apex:inputField binds the value the users enters with the actual field in the object, you won't be able to do it that way. That value will have to be stored somewhere else. It would be good if you post your code is now as is a bit vague and not very clear how you are trying to accomplish that. – PepeFloyd Jun 6 '14 at 12:22
  • See my changed description to the question. – Volodymyr B Jun 6 '14 at 13:13
1

I think this could work:

for (String key : filters.keySet()) {
// THIS LINE THROWS AN ERROR ON NON-EDITABLE FIELD, LIKE CREATEDDATE.
   if(filters.get(key).getDescribe().isUpdateable()) <!--Only update values if the user is allowed to do it.
      filterObj.put(key, filters.get(key)); // But I do not try to insert it to the DB, simple assignment.
}
| improve this answer | |
  • 1
    If you use many fields, you'll run into describe limits. I personally just use a try catch inside the for loop to catch assignment errors. – sfdcfox Jun 8 '14 at 2:57
  • It seems that there is no way to allow a user to change these not updateable fields on an object in memory. I am only left to use this "filters.get(key).getDescribe().isUpdateable()" to just disallow a user to modify it with apex:inputField ((. – Volodymyr B Jun 10 '14 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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