2

Feels like I'm not having a good day at all with platform :-) ... another day and another issue with platform dynamic sObjects :(

So I'm trying to create an sObject record dynamically from a VF page. In that there is a Lookup field to be included in the form dynamically. Now, usually lookup field value can be selected by using the lookup button to search and select a specific record OR by simply typing the name of the record. Platform selects a valid record or alert user that record entered by name do not exist or not found.

So now I'm seeing with dynamic sObjects it is thrown an untrappable error:

SObject row does not allow errors

To test it, simply copy my code below and in the Account Lookup field enter a value 12345xyz (or simply a value that you're sure do not exist in your org as an Account). And press Save button, you get this error, and everything on the form is lost even if you have placed try/catch in your code.

VF Page:

<apex:page controller="MyTestDynamicSObject">
    <apex:form id="theForm">
        <apex:pageBlock mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!saveRecord}" value="Save" reRender="theForm" status="loading" />
                <apex:actionStatus id="status" startText="(loading...)"></apex:actionStatus>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!sObjRecord['LastName']}" required="false" /><br />
                <apex:inputField value="{!sObjRecord['AccountId']}" required="false" /><br /><br />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

APEX Class:

public class MyTestDynamicSObject {
    public MyTestDynamicSObject() {
        sObjRecord      = (sObject) Schema.getGlobalDescribe().get('Contact').newSObject(null, true);
        sObjRecord.put('LastName', 'Test name');
    }

    public  sObject     sObjRecord  { get;set; }

    public void saveRecord() {
        try {
            insert sObjRecord;
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }
}
3
  • so what's the point of using the generic sobject in this case, why not just have it be a contact? Nov 3, 2018 at 8:57
  • 1
    I only simplified my objective and this query with sample reproducible code. My code is lot complex than this, it has to support any sObject in the org and form is generated dynamically with dynamic number of fields.
    – VarunC
    Nov 3, 2018 at 8:59
  • got an answer for you, but honestly, you might be better off focusing on a DSL that generates the forms you want for each dynamic object. To see a great implementation of this check out passage tech's rollup helper package. It's not a UI, but a trigger, but they dynamically generate a trigger specific to an object on demand. Nov 3, 2018 at 9:00

1 Answer 1

0

So it appears that for generic SObjects, if you submit a lookup without first clicking the lookup icon, and the string doesn't match a parent object you'll get the apparently uncatchable "SObject row does not allow errors"

Option 1 - Add some instructions that tell the user to click the magnifying glass before clicking save and ensure they've found a particular contact.

Option 2 - Ditch the generic version and just create a individual pages for each object. I.e. have a setup page where you define the object and the fields, then you click a button and build and deploy a visualforce page that uses concrete sobjects instead of generics.

Option 3 - Just build a page for every permutation of objects since if it's a concrete sobject you'll get a nice "Account Name: No Matches found" error and the field will get highlighted.

Option 4 - Add your own error handling - update the save button to run some javascript that first querieis to see if the account name is valid, and if it isn't display a useful error message before you get to the uncatchable error.

1
  • I've found following Known Issue for this issue. Sadly, it is not going to be fixed. But I am able to get past the error if I disable loadDefaults.
    – VarunC
    Nov 4, 2018 at 4:20

You must log in to answer this question.

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