0

I'm not able to get past what looks like a simple query in Apex and SOQL. I'm trying to query on an Id and I've tried many things. The query is built up dynamically using strings and the final part in this format works fine when hardcoding and Id:

String queryPt2 = ' From Contact Where Id = \'1035U00000ckdinQAX\'';

When using this final segment, everything works fine, but this is processing a single Id, which is of course not what we want, so we're trying this:

String queryPt2 = ' From Contact Where Id = :conId';

Where "conId" is passed through an Apex method. When using this technique, the query doesn't catch and returns null.

I have tried passing the Id as and Id and as a String, passing it as a String and converting to an Id, passing as an Id and converting it to a String and nothing has so far worked except hard-coding it in the format above. I'm thinking that it's some kind of formatting issue and the query itself seems to work because hard coding an Id returns the correct data.

Any thoughts would be appreciated!

2
  • 1
    It'd probably help to see more context for your situation here. As is, there's not much to go on, and there's nothing I can pick out as amiss in your description.
    – Derek F
    Mar 7, 2019 at 22:17
  • Is this assumption correct that you are returning only queryPt2 from the method and then concatenating the returned query to make a complete query and making a Database.query call?
    – Jayant Das
    Mar 7, 2019 at 22:38

4 Answers 4

2

This is based on the assumption that the below string is returned from your apex method, something as:

private String myFromQuery(Id conId) {
    String queryPt2 = ' From Contact Where Id = :conId';
    return queryPt2;
}

And then somewhere in your code, you are utilizing it as:

Id myId = 'xxxx';
myQuery += myFromQuery(myId); // call the method to construct the FROM clause
Database.query(myQuery);

If the above is the correct assumption, then you don't need to even pass any argument to the method which returns the FROM clause. Change your method to a no-args method and utilize it as below.

So your updated code looks like:

private String myFromQuery() {
    return ' From Contact Where Id = :conId';
}

And then use it as:

Id conId = 'xxxx'; // just declare the variable named as conId right here
myQuery += myFromQuery(); // call it now

Database.query(myQuery); // conId is now available to be queried here

Why?

Because when myQuery is being executed, condId has to be a variable available within the scope.

1

The variable conId must be within the current scope in order for this to work.

Example buildQuery

String buildQuery(Id conId) {
  return 'SELECT Id FROM Contact WHERE Id = :conId';
}

Failure

void someMethod(Case record) {
  String query = buildQuery(record.ContactId);
  Contact[] records = Database.query(query);
}

Success

void someMethod(Case record) {
  Id conId = record.ContactId;
  String query = buildQuery(conId);
  Contact[] records = Database.query(query);
}

Always make sure that the variable name accessible to the method's scope. To get a general idea of what "scope" is, you can read more about it in the Java documentation (the semantics are slightly different in Apex, but close enough for purposes of this answer).

0
0

To make this work you would need build the string.

String queryPt2 = ' From Contact Where Id = \'' + conId + '\'';

Or if its a list.

//Update with sfdcfox's suggestions
queryPt2 += '(\'' + String.join(conId,'\',\'')+'\')'
1
  • '(\'' + String.join(conId,'\',\'')+'\')' doesn't require the substringbeforelast method or a manual loop. Also, your code is missing the appropriate quotes.
    – sfdcfox
    Mar 7, 2019 at 22:23
0

Further debugging revealed that this was a JavaScript scope issue further up the chain at the calling controller. The code as posted above actually works fine now. Thanks for your responses, everyone!

You must log in to answer this question.

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