3

I have been asked to manage a large salesforce organization and I am getting a strange error. I simply need to create a Visualforce Page and a Controller right now. I have been banging my head against the wall trying to figure this out. Everytime I attempt to query for a Contact, Account, or User object I am returned a massive amount of null pointer exceptions that follow a "MyClass.QueryException: Script-thrown exception". If I do not attempt to query for a custom object the code saves fine with no issue but adding a Query for one of those 3 objects does not work.

I tried adding in conditions to check for Query Limits but this still throwed the error

Here is the bit of relevant code that seems to be causing the issue:

@TestVisible
private Contact retreiveContact(String contactId){
    try{

        if(!Test.isRunningTest()){

            /////// This query for the Program custom object was simply added to see if any query 
            /////// would work and this one saves fine.
            //Program__c p = [SELECT Id FROM Program__c limit 1];
            //Contact c = new Contact();
            //Account a = [SELECT Id FROM Account limit 1];
            //Contact c = [SELECT Id FROM Contact limit 1];
            //Contact contact = [SELECT Id,AccountId FROM Contact c WHERE c.Id = :contactId];
            //return contact;

            /// The code in this method is the exact same as the code two lines above this line.
            return MyOtherClass.queryContact(contactId);

        }
        return null;
    }
    catch(QueryException qe){
        system.debug(qe.getMessage());
        return null;
    }
    catch(Exception e){
        system.debug(e.getMessage());
        return null;
    }
}

Whenever I attempt to uncomment out the Contact query the code breaks but will save fine with the Program__c query.

The Controller and Visualforce page that I am creating have not even been added to any test classes yet so I am confused as to why an arbitrary and irrelevant query should have any effect on whether other test values have null values. I have already created the Visualforce page and linked this as the controller for that visualforce page.

Any help on this would be greatly appreciated - I am more than a little frustrated with this.

Edit 1:

Here is the method that is actually returning the QueryException: Script-thrown error.

public Contact queryContact(String currentContactID){
    try{
        system.debug('Current Contact ID: ' + currentContactID);
        Schema.DescribeSObjectResult describe = Contact.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fields = describe.fields.getMap();
        String SOQL = 'Select '; 
        for (String fieldName : fields.keySet()){
            System.debug('fieldName: ' + fieldName);
            SOQL += fieldName + ',';  
        } 
        SOQL = SOQL.substring(0,SOQL.length()-1);
        SOQL += ' FROM Contact WHERE ID = :currentContactID LIMIT 1';        
        return Database.query(SOQL);
    }catch(ListException listEx){
        system.debug('queryContact listException: ' + listEx);
        throw new QueryException();
    }catch(Exception ex){
        system.debug('queryContact Exception: ' + ex);
        throw new QueryException();
    }
}

This method is successfully tested and operates fine in many other locations in the organization but it does not work when I add a query to the contact object inside of this new class I am making.

Edit 2:

I found something else interesting that I think is worth pointing out. I tried saving this exact same code in an entirely separate class and it saves just fine. I have no clue why I am unable to save this set of code inside of that particular class. I am starting to lean towards @crop1645 statement of something with a corrupted APEX Cache and there is an actual error inside of this when I try to save something that queries for one of those objects. I honestly have no idea though, if anyone has gone through something like this or has any ideas - I would love to hear from them.

My next attempt will be to create a new class entirely and see if I still get this error. I will report back what happens in case anyone sees this in the future.

Edit3:

I forgot to mention that event excluding this code from running when tests are running still throws this error.

Edit 4

Okay so now the strangest thing is happening. I created another new class to place this code into and I received the same error. I moved this code into an existing class and the code saves just fine. I then call this code placed in the other class within the method I have been trying to save and it saves! I have updated the code above to reflect this. I am absolutely baffled by this but at least this will allow me to complete my project and make my deadline and then come back and investigate what is going on. If anyone has any insight please let me know. Thanks.

1

I would guess that your test code is expecting that retrieveContact "(i before e, except after c)" to always return a Contact record and the input to this method is not a valid Contact Id

This could be because the tests do not set up Contact records or the ones that are set up are not the one's referenced in the testmethod

It could be because the testmethods are passing null or the ID of some other sObject to this method

@dphil is correct that the queryexception will be caught and return null to the caller.

  • Excuse my spelling error (ebmarassing) this code is not being called/tested anywhere yet. I haven't called this method from another place within this class yet because I have been unable to add the code for querying for a contact and successfully save it. – Chris Dec 2 '14 at 19:40
  • 1) what code is invoking retrieveContact() ? sounds like from many places per OP 2) Do you have an org wide sharing model of private and the running user doesn't have access to these records (like Contact) ? – cropredy Dec 2 '14 at 19:47
  • retrieveContact has not been successfully saved with a query inside of it and so I haven't made a call to it from anywhere else (no test,class,or trigger). Entirely unrelated classes start popping up with null pointer errors. Now it is interesting to point out that the top error "MyClass.QueryException: Script-Thrown error" is erroring out on a "queryContact" method (I am going to add this code to my OP above) in an entirely unrelated class. I added an exception catch around this method inside of the test coverage calling it but that only hid the error and kept the null pointers around. – Chris Dec 2 '14 at 19:54
  • As far as a sharing model - well yes we do and this page will be for Community users but none of that has really been applied to anything yet as I am just now starting out trying to make it. The test coverage does run as particular users and portal users in various scenarios but this code is not even being called in test coverage - I don't know if it is possible for this code to have been executed under a user that other test coverage has been setup to run as. I don't think that it is though. – Chris Dec 2 '14 at 20:10
  • Chris - I have seen something like this once before wherein SFDC's cache of your APEX has become corrupted and the code you see in your IDE doesn't seem to match what happens at execution time. (For me it was a VF controller). The problem went away after 12 hours salesforce.stackexchange.com/questions/17657/… – cropredy Dec 3 '14 at 1:28
0

Your query is likely returning either 0 or more than a single record. Either of these cases should cause your code to become invalid. They should be falling into your Exceptions anyways though. In order to fix your query you should be doing something like:

List<Contact> contacts = [SELECT AccountId FROM Contact WHERE Id = :contactId LIMIT 1];
if (!contacts.isEmpty())
  return contacts[0];
return null;
  • Well I should only be getting one record back since I am querying against the Salesforce surrogate ID field but I have added limit 1 into my query (this is an older version of the code) and still it does not run. The method retreiveContacts is not even being called anywhere in test code but adding this causes existing, successful test code to fail. – Chris Dec 2 '14 at 19:35
  • @Chris True, I guess the limit 1 is redundant, but the important part is the line after that. Don't use just the LIMIT 1. Use everything I gave you. The only other thing I can think of is that you have a compile time error that you aren't fixing. – dphil Dec 2 '14 at 19:36
  • It should not make a difference though because a QueryException should be thrown if either a) there are more than 1 records returning or b) there are no records returning. Both situtations should be caught by my QueryException catch and then return null. – Chris Dec 2 '14 at 19:38
  • @Chris What code does it show when you go there with the web browser when you have the Contact portion uncommented? Does it show the correct updated code or does it show the old code? – dphil Dec 2 '14 at 19:40
  • I think you are asking me to look at the Apex Class through salesforce.com in the web browser. I looked and the code appears to be the same as what is inside of eclipse and MavensMate – Chris Dec 2 '14 at 19:43

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.