A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5000 below:

[apex] UnusedLocalVariable FP with binds in SOSL

Affects PMD Version:
version 7.1.0 and 7.0.0

Rule:
UnusedLocalVariable

Description:
The rule UnusedLocalVariable doesn't detect the binding of a variable on a String Query. See Salesforce's docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm

Code Sample demonstrating the issue:

  SObject record = this.getOldRecords()[0];
  **List<SObject> objectsToQuery = this.getOldRecords();**

  SObjectType sObjectType = record.getSObjectType();
  
  String[] relatedObjectData = RELATED_RECORD_DATA_BY_OBJECT.get(sObjectType).split('::');
  
  String relatedObjectName = relatedObjectData[0];
  String relatedFieldName = relatedObjectData[1];

  String query = 'SELECT ' + String.escapeSingleQuotes(relatedFieldName) + ' FROM ' + **String.escapeSingleQuotes(relatedObjectName) + ' WHERE ' + String.escapeSingleQuotes(relatedFieldName) + ' IN :objectsToQuery';**
  
  for(sObject relatedRecord : (Database.query(query))){
    this.withRelatedRecords.add((String)relatedRecord.get(relatedFieldName));
  }

Expected outcome:
The variable is used in the query.
In the line 2 List<SObject> objectsToQuery = this.getOldRecords(); and this variable is used in the line String.escapeSingleQuotes(relatedObjectName) + ' WHERE ' + String.escapeSingleQuotes(relatedFieldName) + ' IN :objectsToQuery';

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Thank you in advance!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4