12

I am getting this error with the code below. I've looked into the other cases on here. In my case I am referring to a relationship name (__r) which I am assuming is pulling correctly. It's a field which is a lookup to User :

Agreement_CSH_to_be_completed_by__r

Here is the code which is giving me problems :

public void doManagedClients() {
        addMsg('Starting processing of Managed Clients.');
        Integer startSize = CSH_Data.size();

        String SOQL = 'Select Id, OwnerId, Owner.UserName, Account__c, RecordTypeId, Name, Agreement_Client_Status_History__c ' +
            'From Contract_Terms__c Where ' +
            '   ( recordtypeid = \''+ ((String)A_N_Rectype).substring(0,15) + '\' ) and ' + 


            '   Agreement_Client_Status_History__c = \'Monthly\' ' +
            'order by createddate desc nulls first' ;
system.debug('#####333#### ');     

        addMsg(DEBUG,'Managed Query is: ' + SOQL + '\n');
        for (Contract_Terms__c a: Database.Query(SOQL)) {
            if (!a.Agreement_CSH_to_be_completed_by__r.isActive) {
                totalInactiveOwners++;

                addMsg('Account: ' + a.Name +  ', has an inactive owner.  Username: ' + a.Agreement_CSH_to_be_completed_by__r.isActive + '.');
            } else {            
                totalManagedCSHs++;
                addMsg(DEBUG,'Creating a Managed CSH for Account: ' + a);
                CSH_Data.add(new CSH_and_Share_Pair(a, createClientStatusHistory(a)));
                needflush(false);
                if (error) break;
            }
        }
        needflush(true);
        addMsg('End of Managed Clients.');      
    }

Is it just because I don't have a.Agreement_CSH_to_be_completed_by__r included in the SOQL ? I thought I got different errors for that, so I'm a little confused.

Thank you for your help.

15

You need to add all fields that you want access to to your SOQL query, this includes relationship fields. Even if you query for

Agreement_CSH_to_be_completed_by__c

You need to add the fields that you want to access in your query such as

Agreement_CSH_to_be_completed_by__r.isActive

If you update your SOQL, this should work fine

EDIT in response to Comment

try this in execute anonymous. This should return an errror

Contract_Terms__c term = [Select Id, OwnerId, Owner.UserName, Account__c, RecordTypeId, Name, Agreement_Client_Status_History__c From Contract_Terms__c limit 1];
system.debug('User is active: ' + term.Agreement_CSH_to_be_completed_by__r.isActive);

If you then add it to your SOQL like below, and re run it, it should work fine

Contract_Terms__c term = [Select Id, OwnerId, Owner.UserName, Account__c, RecordTypeId, Name, Agreement_Client_Status_History__c, Agreement_CSH_to_be_completed_by__c, Agreement_CSH_to_be_completed_by__r.isActive From Contract_Terms__c limit 1];
    system.debug('User is active: ' + term.Agreement_CSH_to_be_completed_by__r.isActive);
  • ok, thanks @SF_Ninja. I wasn't sure if that was it. I'm having problems troubleshooting this because I can't run it in Execute Anonymous and get a log produced, so I'm only getting a log when the schedule runs. I've set up myself for deb logging. Should I be doing something different with the Execute Anonymous ? Thanks again. – Zoom_v Nov 5 '13 at 18:12
  • you should be able to run a similar query in Execute anonymous, and then try to output the value of the field in a system.debug call. If you try the query with the field in it, it should work, and then try to the same query and debug statement without querying for the field, you will get an error – Chris Duncombe Nov 5 '13 at 18:14
  • Edited the answer to try to help show you how to test with execute anonymous. – Chris Duncombe Nov 5 '13 at 18:23
  • No worries, glad to help – Chris Duncombe Nov 5 '13 at 18:32

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.