You need to sign in to do that
Don't have an account?

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
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
- 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.
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
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
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
Regards,
- Harsha