function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sol2012Sol2012 

SOQL on an Opportunity extension does not retrieve all fields

Hello,

I am having an issue with a controller extension, where I am able to get a record ID and then make a SOQL query... but the query does not load all selected fields...

I have tried both getting these fields both from the controller and from a SOQL query, to no attempt...

Am I missing something? Is it just a bug?

Thanks in advance for any help!

Apex code (two options with alternatives and results. Current option "active": get opportunity from SOQL query):
public with sharing class OpportunityQualityControllerExtension {
	
	private Opportunity controllerOpportunity;
	private Opportunity opp;
	private Id recordId;
	
	// The extension constructor initializes the private member
	// variable acct by using the getRecord method from the standard
	// controller.
	public OpportunityQualityControllerExtension(ApexPages.StandardController stdController) {
		
		// Option 1: Use controller
		/*
		stdController.reset();	// Should not be needed, but there is no difference
		stdController.addFields(new List<String>{'HiddenName__c', 'KeyDecisionMaker_id__c'});	// Add extra fields
		this.controllerOpportunity = (Opportunity)stdController.getRecord();	// Get record
		opp = this.controllerOpportunity; // Assign Opportunity to a new variable to validate it below
		*/
		// Alternative options: use recordId to perform a query (all 3 alternative methods return the same ID, the right one)
		//recordId = controllerOpportunity.Id;	// Alternative 1: Get Id from Opportunity ID
		//recordId = stdController.getId();		// Alternative 2: Get Id directly from Standard Controller
		recordId = ApexPages.currentPage().getParameters().get('id');	// Alternative 3: Get Id from page parameters
		opp = [SELECT Id, Name, HiddenName__c, KeyDecisionMaker_id__c, StageName FROM Opportunity WHERE Id = :recordId];
		
		// Validate options above...
		if (opp == null) {
			System.debug('===> NULL Opportunity!!!');
		} else {
			System.debug('===> Opportunity value: ' + opp);
		}
		
		// Option 1 result (Using controller)
		//	===> Opportunity value: Opportunity:{RecordTypeId=012D0000000JzBSIA0, CurrencyIsoCode=EUR, Id=006L0000006E7qFIAS}
		
		// Option 2 result (Using query)
		//	===> Opportunity value: Opportunity:{Name=XXXX, StageName=Potential, RecordTypeId=012D0000000JzBSIA0, CurrencyIsoCode=EUR, HiddenName__c=XXXX, Id=006L0000006E7qFIAS}
	}
}
Visualforce code (Irrelevant: it is used inline in a Standard opportunity page layout):
 
<apex:page standardController="Opportunity" extensions="OpportunityQualityControllerExtension" tabStyle="Opportunity" >
	<apex:pagemessages />
	In...
</apex:page>






 
Best Answer chosen by Sol2012
Sol2012Sol2012
For anyone in the same situation:

When you query a field with a NULL value, that field is not retrieved, thus it does not show up in a debug message.

Fields are retrieved when they contain information.

@Vivek, CLK: Thanks for your cooperation!

All Answers

Vivek_PatelVivek_Patel
Hi Sol2012,

controller.getRecord() methods gives you all the field which are included on the vf page not all the fields available on the object, if you want other fields, you will have to query them inside the exstension.

Regards,
Vivek Patel.

Please mark this as best answer if this solves you problem.
Sol2012Sol2012
Hi Vivek,

Many thanks for your answer.

In the posted code, you can see that the "controller.getRecord()" option is commented. The option which is "running" is actually getting the record ID from the page parameters and then performing a SOQL query as you suggest.

What puzzles me is that this query is not returning the expected fields either! (See "Option 2 results" in the lower part of the posted code)

Thanks for your cooperation, though!
Vivek_PatelVivek_Patel
Hi Sol2012,

I tried similar example and all the fields which I queried were retrieved

May be salesforce support can help you to identify if it is a bug from their side.

Regards,
Vivek Patel.
CLKCLK
check whether all those field made visible in FLS?
Sol2012Sol2012
@Vivek
Thank you again... I am able to get at least a text field... but certainly not the other (Lookup field in the code above against Contact, which is not on a Child-Parent relationship with Opportunity...)

@CLK
Many thanks for your answer, although those fields are universally editable in my org!
 
Sol2012Sol2012
For anyone in the same situation:

When you query a field with a NULL value, that field is not retrieved, thus it does not show up in a debug message.

Fields are retrieved when they contain information.

@Vivek, CLK: Thanks for your cooperation!
This was selected as the best answer