You need to sign in to do that
Don't have an account?
James George 00700
Is it possible to do System.Debug dynamic SOQL statements
Hi Friends,
Is it possible or easy way to do System.Debug dynamic SOQL statements, want to know what will be the internal parameters build in the query before executing
For eg:
Is it possible or easy way to do System.Debug dynamic SOQL statements, want to know what will be the internal parameters build in the query before executing
For eg:
trigger AccountDeletion on Account (before delete) { // Prevent the deletion of accounts if they have related opportunities. for (Account a : [SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity) AND Id IN :Trigger.old]) { Trigger.oldMap.get(a.Id).addError( 'Cannot delete account with related opportunities.'); } }
Can somebody provide a sample code to have a debug log in the For loop, so I can know what are the parameters get passed.
or is there any way the SOQL query to be passed to a string so I can see what's going on.
Thanks,
James George
Here's how I would write that trigger that you specified above. In triggers, it's important to bulkify your trigger to avoid hitting governor limits, so I put the SOQL query before the loop. Regarding your question about dynamic SOQL, Here is the developer forum page: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm?search_text=soql
In a nutshell, you construct your query as a string (which you can easily display in a debug statement) and then use Datebase.query(yourQueryString) to execute.
Here's a very simple example:
All Answers
I suspect your query is answered in the below blogs,
https://developer.salesforce.com/forums/?id=9060G000000BfOQQA0
https://success.salesforce.com/answers?id=90630000000hdphAAA
https://webkul.com/blog/execute-a-dynamically-created-query-in-apex/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks.
Here's how I would write that trigger that you specified above. In triggers, it's important to bulkify your trigger to avoid hitting governor limits, so I put the SOQL query before the loop. Regarding your question about dynamic SOQL, Here is the developer forum page: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm?search_text=soql
In a nutshell, you construct your query as a string (which you can easily display in a debug statement) and then use Datebase.query(yourQueryString) to execute.
Here's a very simple example:
Thanks for the detailed reply and hint about hitting the governer limit.
Is there any documentation/reference about writing the complex SOQL which spans more than 2 SELECT statements in a single SOQL.
Thanks
James George
Here is the SOQL and SOSL reference guide. Introduction to SOQL and SOSL (https://developer.salesforce.com/docs/atlas.en-us.224.0.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_sosl_intro.htm)
Here are a few of my most common queries that would span multiple objects.
Thanks for providing the link and the sample SOQL.
Thanks again
James George
Hi Derrik,
Is it possible to substitute real values into a complex SOQL request like below: