Can I use SOQL to update fields? - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Umair IlyasUmair Ilyas 

Can I use SOQL to update fields?

Hi All,

I built a report that shows me a list of contacts based on a certain set of requirements. For example, my report only shows contacts who have attended a training, taken a survey, or opened an email. The contacts in this report should be on a specific "level" - which is another picklist field in Salesforce. The contacts in this report should be on level "A"

The problem I'm having is that some contacts on this list are NOT on level A, and I need a way to mass update those members that fall on my report. 

My question is this: Is it possible to create a SOQL query to pull the Contacts that I need, and then to update all of the Contacts to level A? 

To add further, I'm looking to mass update the level field based on Contact information across various custom objects. These objects may or may not have a relationship Contacts. However, I would still like data in those objects to update fields in the Contacts object.

Any thoughts?
Best Answer chosen by Umair Ilyas
Jeff MayJeff May
If you want this work to be done automatically when a record is updated, you want to learn about triggers.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm

  http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm


All Answers

Anu MoorthyAnu Moorthy
Answer to : Is it possible to create a SOQL query to pull the Contacts that you need, and then to update all of the Contacts to level A?
Yes.  You can run a SQL using Jitterbit and pull all the contacts that should be in "Level A".  I am not sure what filter you will use to gather just those records.  Let's assume, you have this list ready.  You can add "A" to the level field and upsert these records back into SF.

Second Question:  Can you give a more specific example for this?  The part that confuses me is - "may not have a relationship with contacts".  
Jayvin AroraJayvin Arora
For the first part of your question, You want to do a tool called data loader to update records. Or, if you're in Apex, you want to use the update DML action


Here's what I would recommend

1- Do your query to pul the contacts. You'll need your contact ID and the field that needs to be updated.
2-  In excel update the records as needed
3- download and install dataloader. In Salesfroce go to Setup --> Dataloader
4- map the fields in teh excel document to the Salesforce fields.
5- perform the update. After the update you'll get a success and error log. look at these to see if there have been any issues with your update.


Jeff MayJeff May
There are a number of data manipulation tools you can use.  See choices in the Salesforce UI under Admin Setup->Data Management.  You might also consider a tool such as the free dataloader.io (just type dataloader.io in a browser).  It lets you export the records you want by creating a SOQL statement. Then you can modify the CSV file and run a data update job.
Phillip SouthernPhillip Southern
Umair, to answer the question from a developer point of view...yes that is very possible.  In the developer console you can for the query in the SOQL editor to confirm it grabs what you need.  After that you can run code in the Execute Anon section to pull the SOQL through a Loop, make changes and then add it to a LIST to perform one update action on it. 

All that said, from a high level admin/professional approach: perferred method would be the ETL tools that are being mentioned in the post.
Umair IlyasUmair Ilyas
Hey All,

These are some great responses, and it's definitely helping me out. I've used DataLoader to mass update records before, and now I use a tool called Enabler for Excel (its a godsend!). 

I'm understanding that SOQL can help me get the data I need, and it can also provide the field that I need to update. I understand I can update that field manually via DataLoader, Enabler, etc; however, I am looking for some sort of automation that will take care of this whenever Contact records are edited or saved. I'm curious of Jayvin's response above when referring to DML. Can a query be "fed" into some sort of automation that will not require me to manually update my fields? If so, can the DML be part of the query code? 

I'm going to play around with the execute Anon suggestion that Phillip presented, especially since I'd like to use as few tools as possible (I'd rather do as much as I can using a SOQL)
Jeff MayJeff May
If you want this work to be done automatically when a record is updated, you want to learn about triggers.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm

  http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm


This was selected as the best answer
MarijaMarija (Salesforce) 
Hello Umair,

I see our community has been helpful, please take a moment to let us know if this answers your question by selecting the post as "Best Answer" to help others in the community with similar questions, or let us know if we can be of further assistance.
Umair IlyasUmair Ilyas
Hi Jeff,

I think Apex would be the answer for what I need. I guess it's time to learn some Apex!
Akeel WaniAkeel Wani
I currently have 103 progress notes in the session object, where I need to update the  picklist field “treatment type”  from EPC to HC. I am wondering if we have a way to do this quickly.
 
I cannot use a dataloader upload as the session needs to be open, in order to update this but in this case all sessions have been closed and i need to do it for all the closed sessions. 
Chinmaya RathChinmaya Rath
Best thing here is to go for Triggers.
As you have mentioned, you need to automate this task... triggers should help you to acieve this.