+ Start a Discussion

Dynamic SOQL/DML

I need help on dynamically assigning data to a query result so I can do a DML update. I want to use Batch Apex to overcome the 10k governor limit.  So for example, if I have a table that a user can supply a query of their own that I would never know to code for in advance how can I iterate over the results and detect the actual field type so I can determine how to update that field.  Let's say the query is "SELECT Firstname, Lastname, Email FROM Contact".  
My code would need to dynamically, detected that the two fields are string type and one is email type and dynamically, update the fields with data that I will have predefined based on the field type.  So this code could never hard code in FOR LOOP c.Email = 'some value' because I don't know that they would supply EMAIL in their query, I have to detect that it is email type and the code would have to be able to update the field whether it is actually named email or a custom field named myemail__c with 'some value'.
Does Salesforce even let you get to that level to be truly dynamic in your code base.  I see some rough examples, that don't seem to get me to what I am describing or I am just not understanding it.
PriyaPriya (Salesforce Developers) 

The developer community recommends providing any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this while posting a question.


Salesforce Support

I figured it out.  From the batch apex examples that use Scope for the variable names (sObject), I just did scope.get(variablename);  Where variablename could be equal to any object name, for example.