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
billkatbillkat 

Update - how to update records on a custom object

I'm trying to build a statement to update some records. I'm having trouble understanding the method and syntaxt. Trying to do this via PHP, forming statements based on this page http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_update.htm

 

In a nutshell I don't understand what the first term "Contact" in this line from the docs is:

Contact c = [select account.name from contact
             where lastName = 'Carter' limit 1];

 

I'm trying to build a statement like this:

 

$qry = "XXX cbp = [SELECT Supplier_Code__c FROM MyObj__c WHERE ManufacturerName__c = 'Fish Corp'];";
$qry .= "cbp.Supplier_Code__c = 'FISHCO';";
$qry .= "UPDATE cbp;";

 

But lost as to what/where the "XXX" term is or where to find it re my custom object...

Hope this makes sense

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
EtienneCoutantEtienneCoutant

So then you can pass the Objects one by one in the update call. There is not limitation like in APEX.

 

 

  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient = $mySforceConnection->createConnection('partner.wsdl.xml');
  $mylogin = $mySforceConnection->login($SalesforceUsername,$SalesforcePassword);

  $query = 'SELECT Supplier_Code__c FROM MyObj__c WHERE ManufacturerName__c = \'Fish Corp\'';
  $options = new QueryOptions(200);
  $response = $mySforceConnection->query(($query),$options);
  !$done = false;

  if ($response->size > 0) {
    while (!$done) {
      foreach ($response->records as $record) {
	 $myObj = new SObject($record);
	 $myObj->fields-> Supplier_Code__c = 'FISHCO';
	 $results = $mySforceConnection->update(array($myObj));
      }

      if ($response->done != true) {
        echo "***** Get Next Chunk *****\n";

        try {
          $response = $mySforceConnection->queryMore($response->queryLocator);
        } catch (Exception $e) {

          echo $e->faultstring;
        }
      } else {
        $done = true;
      }
    }
  }

 

 

Good luck!

Etienne

All Answers

EtienneCoutantEtienneCoutant

Hi,

 

I don't think you can query that way in php.

I would try something like:

 

 

  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient = $mySforceConnection->createConnection('partner.wsdl.xml');
  $mylogin = $mySforceConnection->login($SalesforceUsername,$SalesforcePassword);

  $query = 'SELECT Supplier_Code__c FROM MyObj__c WHERE ManufacturerName__c = \'Fish Corp'\';
  $options = new QueryOptions(2000);
  $response = $mySforceConnection->query(($query),$options);
  !$done = false;

  if ($response->size > 0) {
    while (!$done) {
      foreach ($response->records as $record) {
        $myObj = new SObject($record);
        $myObj->fields-> Supplier_Code__c = 'FISHCO';
      }
      $results = $conn->update($response->records);
    }
  }

 

 

billkatbillkat

Thanks Etienne. I'm getting

EXCEEDED_ID_LIMIT: record limit reached. cannot submit more than 200 records into this cal

 

but looks like the right track, thanks

EtienneCoutantEtienneCoutant

Hmmm.

That is probably because update does not accept more than 200 records at a time.

Can you try changing the line 

$options = new QueryOptions(2000);

to

 

$options = new QueryOptions(200);
billkatbillkat

I did try that, but no change. The update() line causes the error, I guess the $response object needs chunking up somehow.

I confess I'm somewhat out of my depth but will experiment more tomorrow... cheers.

EtienneCoutantEtienneCoutant

So then you can pass the Objects one by one in the update call. There is not limitation like in APEX.

 

 

  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient = $mySforceConnection->createConnection('partner.wsdl.xml');
  $mylogin = $mySforceConnection->login($SalesforceUsername,$SalesforcePassword);

  $query = 'SELECT Supplier_Code__c FROM MyObj__c WHERE ManufacturerName__c = \'Fish Corp\'';
  $options = new QueryOptions(200);
  $response = $mySforceConnection->query(($query),$options);
  !$done = false;

  if ($response->size > 0) {
    while (!$done) {
      foreach ($response->records as $record) {
	 $myObj = new SObject($record);
	 $myObj->fields-> Supplier_Code__c = 'FISHCO';
	 $results = $mySforceConnection->update(array($myObj));
      }

      if ($response->done != true) {
        echo "***** Get Next Chunk *****\n";

        try {
          $response = $mySforceConnection->queryMore($response->queryLocator);
        } catch (Exception $e) {

          echo $e->faultstring;
        }
      } else {
        $done = true;
      }
    }
  }

 

 

Good luck!

Etienne

This was selected as the best answer
billkatbillkat

Thanks so much Etienne, I owe you a beer...

 

Had to make a slight mod. After dumping $result I found it demanded an ID. Bit of searching round the web and modded to this:

 

Add Id to query

$query  = "SELECT Id, Product_Name__c ... "

 

Add Id to the new object:

 

foreach ($response->records as $record) {

   $myObj = new SObject($record);
   $myObj->Id = $record->Id;

 

Works like a charm... cheers!