1

a lot of you might find my question really basic but I am beginner. I am writing a visualforce page and here is my code.

public class CustomControllerExtension1 {
    public Account accountOfInterest{get;set;}
    public contact contactOfInterest{get;set;}
    public CustomControllerExtension1(ApexPages.StandardController controller) {
        accountOfInterest = (account)(controller.getRecord());
        contactOfInterest = [select name, id from contact where id='00341000003a3gV'];

    }

    public void SaveCustom() {
      if(accountOfInterest.monthly__c<10000) {
           upsert accountOfInterest;

          } else { 
                    accountOfInterest.monthly__c=10000; 
                    accountOfInterest.message__c='Salary is more than permitted!';

                 }

    }
}

When I am trying to save the code its asking me to query Message object. When I queried separately as in "select message_c from account" it gives me error saying an another for the query of the same object had been found and doesn't let me run it. Please help. If needed I will provide more information and screenshots. Thank you.

2
  • can you post the exact error message, may be someone can take a look at it and help
    – Rao
    Oct 5, 2016 at 21:15
  • Have changed my answer - hopefully it will solve the problem. Oct 5, 2016 at 22:35

1 Answer 1

0

In your page, add the field that you want to reference, but don't render it.

<apex:outputText rendered="false" value="{!Account.message__c}"/>

Also, I can see another error. You are assigning the result of the SOQL query to a single sObject when you should be assigning it to a list. An alternative is to use a LIMIT 1 clause, but that causes errors if a record is not found.

Given that, change your code to this:

Contact[] contacts = [select name, id from contact where id='00341000003a3gV']; 
//note, you should probably not be hardcoding an id here
if (!contacts.isEmpty()){
    contactOfInterest = contacts[0]; 
}

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.