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
ethan huntethan hunt 

check for blank id field

Hi,

Can the below code be optimised. I need the query to filter all the not null id's , so that i can remove the variable strOpportunityRecord.

public Opportunity getOpportunityRecord(String Queriablefields, id sourceOpportunityId) {       
        String strOpportunityRecord;
        Opportunity sourceOpportunityRecord = new Opportunity();
        try {
            strOpportunityRecord =  'SELECT ' + Queriablefields +'Name From Opportunity where id =: sourceOpportunityId';
            if (strOpportunityRecord != null) {
                sourceOpportunityRecord = Database.query(strOpportunityRecord) ;
            }
           return sourceOpportunityRecord;
        }
        catch(QueryException ex) {
            system.debug(ex);
            return null;
        }
    }

Regards
gm_sfdc_powerdegm_sfdc_powerde
I see multiple problems with your query logic. 
- You should be doing the null check on sourceOpportunityId and not strOpportunityRecord. 
- No need to instantiante sourceOpportunityRecord.  
- Collect the result of Database.query in a list object, and then check the size of hte list. It's a bad idea to catch QueryException here.

Hope that helps.

harsha__charsha__c
"strOpportunityRecord" can be removed from your snippet without any changes

After removing the snippet becomes as below:


try {
            sourceOpportunityRecord = Database.query(
'SELECT ' + Queriablefields +'Name From Opportunity where id =: sourceOpportunityId') ;
            
return sourceOpportunityRecord;
       }
       catch(QueryException ex)
       {
            system.debug(ex);
            return null;
        }

The above snippet returns the Opportunity record, if the Query returns any record else it goes to the catch block and returns null.

Regards,
- Harsha
Rahul SharmaRahul Sharma

Hi Sidhartha,

Just  thought to provide a solution as explained by gm_sfdc_powerde comment, 

/////////////////////////////////////////////////////Code Start///////////////////////////////////////////////////////
public Opportunity getOpportunityRecord(String Queriablefields, id sourceOpportunityId) {      
    String strOpportunityRecord;
   
    // return null or empty list if the source Opportunity Id is null
    if(String.isEmpty(sourceOpportunityId)) {
        return null;
    }
   
    try {
        // Query all the records in a list is best practice to avoid null pointer exception
        List<Opportunity> lstOpportunity = Database.query('SELECT ' +
            Queriablefields +'Name From Opportunity where id =: sourceOpportunityId');
     
        // Return list if there are records, or else return null or empty list
        if(!lstOpportunity.isEmpty()) {
            return lstOpportunity;
        } else {
            return null;
        }
    }
    catch(QueryException ex) {
        system.debug(ex);
        return null;
    }
}

/////////////////////////////////////////////////////Code End///////////////////////////////////////////////////////

Thanks,
Rahul

ethan huntethan hunt
Hi Rahul/Harsha/gm_sfdc_powerde,

Thanks for helping me out.
So what will be the better option, to create a list or like how harsha has given a point. My objective is to reduce number of varibles and follow the best practice.

Regards
Sidhartha
harsha__charsha__c
Hey, your query can never return more than one record because you are matching with an Id (where id =: sourceOpportunityId), which is UNIQUE. So no worries. You do not need an extra list there.

Regards,
- Harsha