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
ccoltccolt 

Update via APEX with external Data

Hi,

 

I want to update my Contacts with APEX.

I call a WebService and recive a set of contacts (XML). After parsing this information in Objects I want to update this contacts.

 

Working code:

 

... definition of customdata arrays and counter

sObject[] output = new Contact[]{};

for(Integer i = 0; i < counter; i++)
{
sObject con = new Contact();

con = [SELECT id FROM Contact WHERE id = :a];

con.put('customfield1__c', customdata1[i]);
con.put('customfield2__c', customdata2[i]);
con.put('customfiled3__c', customdata3[i]);

output.add(con);
}
update output;

 

this code works fine, but after execute ... SQL limit 100 reached.

I know I can do it in a Batch APEX, but I want to do this on a buttonevent.

 

Why this code fails ?

 

... definition of customdata arrays and counter

sObject[] output = new Contact[]{};

for(Integer i = 0; i < counter; i++)
{
sObject con = new Contact();

con.put('id', customid[i]);

con.put('customfield1__c', customdata1[i]);
con.put('customfield2__c', customdata2[i]);
con.put('customfiled3__c', customdata3[i]);

output.add(con);
}
update output;

 

customid is a normal contact id, that I transfer from my salesforce to my other system.

 

Error Message: System.SObjectException: Field id is not editable

 

why the same schema works, when I usw the WebService API, but not the APEX ?

 

thanks for help

 

so long

 

youcyouc

the query [SELECT id FROM Contact WHERE id = :a] is inside a for loop, so it will be executed for your {counter} times. Apparently it hits the limitation

ccoltccolt

the script reach the limitation becouse of 1000 records ;) and 100 SQL per job is allow

 

counter = 1000;

youcyouc

I am not sure what you want to achieve with setting the id. But ID is salesforce standard field, read only and automatically generated. You can't edit ID, both through web service and apex.

 

The working code has nothing to do with editing ID. It is just a query, reading the contact.  

 

ccoltccolt

Not exactly.

 

example code from documentation:

 

 

public void updateAccountSample()
{
    Account updateAccount = new Account();
    updateAccount.Id = "001D000000Ivban";
    updateAccount.Name = "New Account Name from Update Sample";
    SaveResult[] saveResults = binding.update(new sObject[] { updateAccount });
}

 

 

 

here the ID is set for update.

Why it works with the WebServices API but not with APEX ? I do the same with APEX.

I don´t understand why I must SELECT the Contact with APEX (to set the ID in the Object) and with WebServices I can write the ID in the Fiel.

 

The reason why I'm doing this, is an Import of Updates for my Contacts from a ThirdParty-Software.

youcyouc

Okay, so now i understand your question.

 

Here updateAccount.Id = "001D000000Ivban"; is give an id to account, so the api knows which record needs to be updated. It doesn't change the id value at all. That's how you update a record through web service.

 

And from functional point, it is exactly the same way as in apex, choose the right record, and then do the update job.

ccoltccolt

Ah okay.

 

Thats not so cool when I import 10K.

In a Batch job thats 102 Batches with 99 SQL ea (update is a SQL too ;( )

 

And on event I must build a merge table after SELECT all record with [SELECT .... WHERE id = :varid or id = :varid2 ...] and can do a maximum of 50k records per event.

 

I'm right ?

youcyouc

Err, depends on where you write the query.

 

batch apex can retrive 50 million records.

 

batch apex: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

 

a good artical about entry point determin governor limits: http://th3silverlining.com/2009/07/01/how-to-avoid-avoid-governor-limits-part-1-of-n/

ccoltccolt

yes, but i can put only 1000 objects in the updaterequest, so this border stays and i can do only 100 SQL.

50 SELECT, 50 Updates per function or batch. (and in batchmode X iterations)

I know the batchmode, but i want a live event ;) so the max SOQL retrive is 10000 and by the way, I get the input over HTTP request. (I know, 1MB limit ;) )

 

this limits are such a cute thing ;)