0

Original code was

Set<Id> crConIds = new Set<Id>();


if (!crConIds.isEmpty()){ crConIds = new Map<Id, Account>(
   [SELECT Id, Name, ContactInfo__c FROM Account WHERE Id IN :crConIds]);
}

Modified Code to move query to own method.

if (!crConIds.isEmpty()){ 
   Map<Id, Account> account_m = getAccountFromAccountId();
}

public static Map<Id, Account> getAccountFromAccountId(){
String acctQuery = 'SELECT Id, Name, ContactInfo__c FROM Account WHERE Id IN 
:crConIds'

Map<Id, Account> accts= new Map<Id, Account>((List<Account>)Database.query(acctQuery));
return accts;

Test Class method that was previously passing now fails

Account acct= new Account(
   Name = 'Test Account'
);
insert acct;

Change_order__c cr = new Change_order__c(
  Name = 'Test CR',
  Type__c = 'Bug',
  Account = acct.id
)

Error:

unexpected token: Id
Class.ChangeOrderTriggerHandler.getAccountFromAccountId: references this line of code `Map<Id, Account> accts= new Map<Id, Account>((List<Account>)Database.query(acctQuery));`
Class.ChangeOrderTriggerHandler.bulkBefore : references this line of code Map<Id, Account> account_m = getAccountFromAccountId();

Questions

  1. Is the updated class logic correct? The way I am referencing the new method?
  2. Is the issue caused by using Database.query?
  3. Do I need to update the Test method dues to the query being moved to its own method?

1 Answer 1

0

There are a number of problems here, and it's not clear which are genuine problems in your code and which are related to how you included the code in your question.

The original code you show would not compile. Acount is misspelled.

The new code you show appears to have multiple issues, but again, it's not clear which one is to blame for this error.

public static Map<Id, Account> getAccountFromAccountId() {
    String acctQuery = 'SELECT Id, Name, ContactInfo__c FROM Account WHERE Id IN :crConIds';

    Map<Id, Account> accts= new Map<Id, Account>((List<Account>)Database.query(acctQuery));
    return accts;
}

crConIds is not in scope in the method getAccountFromAccountId() unless it is an instance variable in the class containing this method. If it's not, you'll need to pass that variable as a parameter.

Your code as included shows a line break inside the text of the dynamic query and a missing semicolon. Both are syntax errors that would appear at the time of saving, not at the time of running a test.

You've also needlessly switched to a dynamic query here. A static query will work just fine and is a more appropriate pattern as it provides compile-time checking.

2
  • crConIds is declared at Class level. Added above. I think I tried using a static query but it would not deploy to the org. So I should use something like this? Map<Id, Account> acctResult = new Map<Id, Account>([SELECT Id, Name, ContactInfo__c FROM Account WHERE Id IN :crConIds]); Also, what should the Database query look like for future reference. Thank you
    – Phuc
    May 20, 2022 at 14:20
  • Your query itself is not incorrect.
    – David Reed
    May 20, 2022 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .