Neat Salesforce URLFOR Trick

December 28th, 2010

I was working on an app the other day and had written a custom screen allowing the user to add/edit multiple records on the same page. So in the process I wrote a Visualforce page that Overrides the standard New and Edit functions and redirects the user to this new screen. Here’s my code that I thought should have worked perfectly.

1
2
3
<apex:page standardController=”MyCustomObject__c”
  action={!URLFOR($Page.AddEditMultipleRecords,null,
  [id=MyCustomObject__c.SomeRelatedObject__c])}/>

The code above worked great for the New function but I was receiving the following error for the Edit function:

SObject row was retrieved via SOQL without querying the requested field: MyCustomObject__c.SomeRelatedObject__c

This is actually a well-known “problem” when using a standard controller. The standard controller automatically constructs your SOQL query for you based on the fields that are referenced in your page. So simply including the field on the page, fixes the error.

1
2
3
4
5
<apex:page standardController=”MyCustomObject__c”
  action={!URLFOR($Page.AddEditMultipleRecords,null,
  [id=MyCustomObject__c.SomeRelatedObject__c])}>
  {!MyCustomObject__c.SomeRelatedObject__c}
</apex:page>
VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Neat Salesforce URLFOR Trick, 10.0 out of 10 based on 1 rating

Categories: Salesforce, Visualforce

Leave a comment

Comments Feed5 Comments

  1. Abhinav Gupta

    Interesting post !
    Seems you included wrong field in the page i.e. {!Purchase_Order_Opportunity__c.Purchase_Order__c}

    I believe it should be MyCustomObject__c.SomeRelatedObject__c

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. Jeff Douglas

    Thanks @Abhinav!! I didn’t cleanse my code as well I would have liked to.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  3. Alex

    Hi, this is definately most usefull, I’m trying to save the Visualforce page in my sandbox but get this error :

    Save error: is required and must be the outermost tag in the markup at line 1 column 1

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  4. Alex

    sorry

    Save error: is required and must be the outermost tag in the markup at line 1 column 1

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  5. Alex

    sorry again the error is on the page tag which is definately the outermost but eventually Eclipse returns the error.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

http://blog.jeffdouglas.com / Neat Salesforce URLFOR Trick