function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam BengtsonAdam Bengtson 

How to create dynamic soql update query in Apex

I need help creating a dynamic query for a global newsletter unsubscribe (20 newsletters or more that can change at any given time).  I don't want to have to constantly update so many fields in the apex code every time we create new newsletters.

Here is the only code I could get working:
global static boolean unsubscribe(string user_id, string newsletterField) {
        Boolean valid = true;
         
        contacts = [SELECT field1, field2, field3
        FROM Contact
        WHERE Id = :user_id];
        
        if(contacts.size() < 1) {
            valid = false;
        } else {
            if(newsletterField == 'field1'){
                contacts[0].field1 = false;
            } else if(newsletterField == 'field2') {
                contacts[0].field2 = false;
            } else if(newsletterField == 'field3') {
                contacts[0].field3 = false;
            } 
        
            update contacts[0];
        }
            
        return valid;
    }
Basically a huge list of if/else statements and its making me crazy that I can't seem to find a way to make dynamic query where I can set the field to be updated a variable like contacts[0].myVar = false instead of hard coding every field.

Maybe I am approaching this issue completely wrong.  Some advice would be great.

Thanks!
 
Best Answer chosen by Adam Bengtson
Jerome RussJerome Russ

It looks like you know the field you need to update to false (newsLetterField that you pass in).

Instead of doing a hardcoded query, you can do:
contacts = Database.query('Select Id,' + newLetterField + ' FROM Contact WHERE Id = ' + user_id);

You could then do:
contacts[0].put(newLetterField,false); 
instead of your nested If statement with all of the fields.

Here are some more in depth references for the concepts I mentioned:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_put

Hopefully that helps!

All Answers

Jerome RussJerome Russ

It looks like you know the field you need to update to false (newsLetterField that you pass in).

Instead of doing a hardcoded query, you can do:
contacts = Database.query('Select Id,' + newLetterField + ' FROM Contact WHERE Id = ' + user_id);

You could then do:
contacts[0].put(newLetterField,false); 
instead of your nested If statement with all of the fields.

Here are some more in depth references for the concepts I mentioned:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_put

Hopefully that helps!
This was selected as the best answer
Sudipta DebSudipta Deb
Hi Adam -

If you know the field, fetch only that field using dynamic query. Here is the updated version of the your method -
 
public Boolean unsubscribe(string user_id, string newsletterField) {
	String query = 'SELECT ' +  newsletterField + ' FROM Contact WHERE Id = ' + user_id;
   	Contact[] myContacts = Database.query(query);
   	
   	if(myContacts.size() < 1){
   		return false;
   	}else{
   		myContacts[0].put(newsletterField,false);
   		Database.update(myContacts[0]);
   		return true;
  	}
}

Please let me know if that helps you. Thanks.
Adam BengtsonAdam Bengtson
Thanks to you both, this is exactly what I needed.