SOQL Query – How to Replace a SOQL Query with a String querysoqlstring Any idea how to convert a query into a string? Best Answer Instead of for (List<AggregateResult> agg: query) you should have for (List<AggregateResult> agg: Database.query(query)). You can find additional detail regarding Dynamic SOQL here. Best regards! Related Solutions[SalesForce] How to insert a string into another string Lets say you have the following strings. String1 = 'myString/myPattern/someString' String2 = 'newString' You can append your new string just before the required pattern like this. String newString = String1.substring(0, String1.indexOf('/myPattern')) + String2 + String1.substring(String1.indexOf('/myPattern'), String1.length()); But make sure your pattern is not repeated in the String1, because you might end up replacing just the first occurrence. Apex SOQL – Formatting DateTime Object into String for SOQL Query DateTime dt = DateTime.now(); String formattedDt = dt.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''); Update: Another option is to use SOQL dynamic binding, no formatting required. DateTime dt = DateTime.now(); String query = 'SELECT Id FROM User WHERE SystemModstamp > :dt ' List<SObject> results = Database.query(query); Related TopicApex – How to Convert a Number into a Formatted StringSOQL – How to Convert String to Query in Dynamic SOQLApex SOQL – How to Replace a SOQL Query with Dynamic QueryApex SOQL – How to Retrieve a String from a Query
Best Answer
Instead of
for (List<AggregateResult> agg: query)
you should havefor (List<AggregateResult> agg: Database.query(query))
.You can find additional detail regarding Dynamic SOQL here.
Best regards!