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
ProphytProphyt 

Updating Multiple Contacts with matching Email from a single Id,Email

SimonF (or anyone :) ),
You recently helped me getting contact updates working.  Im wondering what changes would be required to make it update multiple contacts at once if I pass it a value.

qr = binding.query("select Id,Email from Contact where Email = " + thisEmail + "");

binding.Url = lr.serverUrl;
binding.SessionHeaderValue.sessionId = lr.sessionId;

What would be required to update every contact returned in that query?

Single updates are working flawlessly (different code structure), even multiple iterations of single updates, but I want to accomplish passing the email address of the current contact in my DataTable and then update every contact in salesforce with a matching email address (which i dont have in my DataTable.  Anyone have any tips? Thank you in advance.

If anyone has a block of code that updates multiple contacts I could probably convert that to do what I need if you are willing to share.
DevAngelDevAngel
All of the api calls are batch oriented.  This means that you do your query and then loop through all the results setting the new value.  Then, once they have been updated with the new value, send them all back using a single update call.
ProphytProphyt

In case anyone else is looking for Code:

      DataTable data = this.dataGridView1.DataSource as DataTable;
      DateTime now = DateTime.Now;
      foreach(DataRow row in data.Rows)
      {
        StringBuilder errors = new StringBuilder();
        string thisEmail = row["ContactEmail"].ToString();

        binding.Url = lr.serverUrl;
        binding.SessionHeaderValue.sessionId = lr.sessionId;

        sforce.QueryResult qr2 = null;
        qr2 = binding.query("Select Id,Email from Contact where Email ='" + thisEmail + "'");
        for(int z = 0; z < qr2.records.Length; z++)
        {
          sforce.Contact con = (sforce.Contact)qr2.records[z];
          con.Last_Updated__c = now;
          con.Last_Updated__cSpecified = true;
        }
        sforce.SaveResult[] srs = binding.update(qr2.records);
        if(!srs.success)
        {
          errors.AppendFormat(
              "Error updating ContactUID [{0}]: {1} {2}",
              con.Id,
              srs.errors[0].statusCode,
              srs.errors[0].message);
          continue;
        }
      }
      if(errors.Length > 0)
      {
        lblDebug.Text = errors.ToString();
      }
      else
      {
        lblDebug.Text = "Finished!";
}
 

 

It is probably far from 'right' but it works for my purpose for now.  Thanks DevAngel for the tips!

ProphytProphyt

Edit: Forgot to move the StringBuilder errors = new StringBuilder(); out of the loop before posting.

ProphytProphyt
I redid the code above so that it has unlimited record capability, and while the update process is pretty fast, the code never returns to the application for it to continue processing.  Changing the above to use List<object> and build an array that stores the values then updating the results off the object works much better.