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
Matt TindallMatt Tindall 

AggregateResult = NULL then show 0?

Hello,

 

I am trying to put an IF statement into my Class that determines if an Aggregate Result is NULL then place a 0 in its place.

Iv tried a few different ideas and just can't get it to work.

 

Public Integer getShowJanuarySales() {
    if (ShowJanuarySales > 0){
        return ShowJanuarySales;
    } else {
        return 1;
    }
 

 

Iv got the following statement that works fine as long as a record exists and I know that it changes the value for me. But if I replace the "> 0" with "== null" I just get an error on my page saying it can't reference a null object.

Anyone got any ideas?

Matt

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVaSeAlVa
public class JanuarySalesCalculator{
    
    public integer ShowJanuarySales=0;
    
    public JanuarySalesCalculator(){
    	List<AggregateResult> January_Sales = [SELECT SUM (Sales__c) FROM Stats_Diary__c WHERE Date__c = THIS_YEAR and CALENDAR_MONTH(Date__c) = 1 and Staff__c LIKE 'Matt']; 
        decimal JanuarySalesTEMP = January_Sales[0].get('expr0')==null?0:(decimal)January_Sales[0].get('expr0');
        ShowJanuarySales = JanuarySalesTEMP.intvalue();
    }

	Public Integer getShowJanuarySales() {
 	    return ShowJanuarySales;
    }    
}

All Answers

Sonam_SFDCSonam_SFDC

Hi Matt,

 

I came across a similar issue once - have you tried the following:

if (ShowJanuarySales != NULL){
return ShowJanuarySales;
} else {
return 1;
}

Also, make sure you initialize the variable with a default value: ShowJanuarySales

AmulAmul
put following line in your Apex class constructor

ShowJanuarySales=0;


Vinita_SFDCVinita_SFDC

Hello Matt,

 

Aggregate result is always a number, so compare it with a number.

Matt TindallMatt Tindall

Thanks for the replies but its still not working.

I have created a micro concept to show what I am trying to do.

 

CLASS

public class JanuarySalesCalculator{
    
    public integer ShowJanuarySales=0;
    
    public JanuarySalesCalculator(){
    	List<AggregateResult> January_Sales = [SELECT SUM (Sales__c) FROM Stats_Diary__c WHERE Date__c = THIS_YEAR and CALENDAR_MONTH(Date__c) = 1 and Staff__c LIKE 'Matt']; 
        decimal JanuarySalesTEMP = (decimal)January_Sales[0].get('expr0');
        ShowJanuarySales = JanuarySalesTEMP.intvalue();
    }

	Public Integer getShowJanuarySales() {
    	if (ShowJanuarySales != NULL){
    		return 0;
    	} else {
    		return ShowJanuarySales;
    	}
    }    
}

 VF PAGE

<apex:page controller="JanuarySalesCalculator">
    
    <p>{!ShowJanuarySales}</p>
    
</apex:page>

 ERROR

Attempt to de-reference a null object 
An unexpected error has occurred. Your development organization has been notified.

 If a record exists and even contains a 0 everything is fine. But sometime the record wont exist so I need to work out a solution.

Matt

SeAlVaSeAlVa
public class JanuarySalesCalculator{
    
    public integer ShowJanuarySales=0;
    
    public JanuarySalesCalculator(){
    	List<AggregateResult> January_Sales = [SELECT SUM (Sales__c) FROM Stats_Diary__c WHERE Date__c = THIS_YEAR and CALENDAR_MONTH(Date__c) = 1 and Staff__c LIKE 'Matt']; 
        decimal JanuarySalesTEMP = January_Sales[0].get('expr0')==null?0:(decimal)January_Sales[0].get('expr0');
        ShowJanuarySales = JanuarySalesTEMP.intvalue();
    }

	Public Integer getShowJanuarySales() {
 	    return ShowJanuarySales;
    }    
}
This was selected as the best answer
Matt TindallMatt Tindall

Thats worked!!

Thanks,

Matt