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
SofitDevelopmentSofitDevelopment 

How to use the escapeSingleQuotes method?

I'm trying to use this method in a dynamic SOQL query. Follow the sample code:

 

public static String getRowById(String sobjName, id id){
	String query = 'select ';            
	Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(sobjName).getDescribe().fields.getMap();
	for(String f : objectFields.keySet()){
		query += f;
		query += ',';
	}
	
	query = query.substring(0,query.length()-1);
	query += ' from '+ sobjName;
	query += ' where id = \'' + String.escapeSingleQuotes(id) + '\'';
	query += ' limit 1';
	
	return query;
}

 

The problem is that this is still considered as a SOQL Injection vulnerability.

Am I missing something here? I also tried to use it as a parameter like this:

 

getRowById('SFT_Brand__c',String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('id')));

 

Parth_SevakParth_Sevak

try this.

 

query += '\''+String.escapeSingleQuotes(Id)+'\'';