-1

I'm very new to Salesforce Development and I am struggling to figure this out. So i want to create 2 VF pages and 2 controllers and I want to create new account and insert that account when I'm pressing the Save button, then i want to redirect to second visualforce page and on the second visualforce page i want to fetch the id from the account which is just created and outputfield the value, the name of account. I just started creating the first visualforce page and controller. I dont know if i'm doing this right.

<apex:page controller="screen1">
   <apex:form>
       <apex:pageBlock>
           <apex:pageBlockSection>
               <apex:inputField value="{!account.name}"/>
               <apex:commandButton value="Save" action="{!save}"/>
           </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>
</apex:page>

Controller:

public class screen1 {

    public Account account;

    public Account getAccount()
    {
      if(account == null)
      {
         account = new account();
      }
      return account;
    }

     public PageReference save()
     {
       getAccount();
       if(account != null)
       {
           insert account;
       }
       PageReference pageRef = new PageReference('/apex/screen2');
       pageRef.setRedirect(true);
       return PageReference;
     }}

have an error here:

Error: Compile Error: DML requires SObject or SObject list type: Account at line 20 column 12.

What im doing wrong here ?

  • 2
    Apex is a case-insensitive language. Don't name your variables things like account or contact; it can confuse the compiler (and you). It thinks you're trying to insert Account, the class, rather than account, the Sobject variable. – David Reed Mar 16 '18 at 12:50
  • Someone defined a custom class named Account didn't they. – Adrian Larson Mar 16 '18 at 14:36
1

I wasn't able to reproduce DML requires SObject or SObject list type error.

But on line 23 you are returning PageReference object type. You need to return previously initialized object variable.

PageReference pageRef = new PageReference('/apex/screen2');
pageRef.setRedirect(true);
return pageRef;
1

This sort of compile error is almost universally caused by naming conflict. You could introduce such an error into any org by simply defining the following class:

public class Account { }

If some nefarious or inexperienced developer did so in your org, you can work around the naming conflict by using a more verbose, explicit reference to the type.

Schema.Account record = new Schema.Account();
...
insert record;

Others have mentioned a few other issues you will run into once you resolve this one, but that's the crux of what you directly asked about.

1

You have two changes to work on here.

  1. Apex is a case-insensitive language. Don't name your variables things like account or contact; it can confuse the compiler (and you). It thinks you're trying to insert Account, the class, rather than account, the Sobject variable. You should change the name of your instance variable.
  2. You need to communicate the Id of the object you would like to act upon to your other Visualforce page. Since your pages won't share a view state, you need to use URL parameters to do so.

If you're using a standard controller and a controller extension for your second page, or if you accept the Id of the Account to display in a URL parameter called Id (which is a common pattern), you'd need to add that parameter to your PageReference like this:

PageReference pageRef = new PageReference('/apex/screen2');
pageRef.setRedirect(true);
pageRef.getParameters().put('id', myAccount.Id); // Assuming your variable is called myAccount now.
return pageRef;

You'll probably want to include all of this in your if (myAccount != null) block to avoid NullPointerException, and return null if otherwise.

Additionally, I just want to note that you do not have to redirect to a second page simply to render information about an Account you have just created. Visualforce offers a rich set of functionality for re-rendering and conditionally rendering sections of your page. Trailhead can help you get up to speed on how to dynamically alter your content based on the data in your controller.

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.