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
FrankFFrankF 

Error Updating a custom field in the Lead object (C#)

I need to set a Lookup field's value in Salesforce.com's Lead object and am using the code below.

The Qualifying_Campaign__c field is a custom lookup field related to the Campaign object and the value I'm setting is the correct Campaign Id for the campaign which we want to set. A describesObject call to look at the Lead object reveals that the Qualifying_Campaign__c is indeed updateable.

The code snippet comes from a function called by an Outbound Message from SFDC on Lead creation for certain types of Leads.

The Exception I get when I call the update function  is as follows and I can't quite understand why it's happening...

Error message from catch (Exception ex)
A duplicate value was specified for field 'Id' in object 'Lead', duplicate value '00Q7000000OXbm5EAD' prior value '00Q7000000OXbm5EAD'

heh?!
Other troubleshooting showed that my code works when updating the OwnerID in the same way. The error only occurs when updating the Qualifying_Campaign__c field.
 
Thanks in advance,
Frank.
 
Code Snippet
QueryResult qrLead = RunQuery("select Id, Qualifying_Campaign__c from Lead where Id='" + leadid + "'");
sObject lead = null;
if (qrLead != null && qrLead.size > 0)
{
 lead = qrLead.records[0];
 //Update to new username
 //ld.Id = leadid;
 //lead.Any[1].InnerText = uid;
 lead.Any[1].InnerText = "70170000000KheRAAS";
 
 SaveResult[] saveResult = null; ;
 try
 {
  saveResult = _sfdcService.update(new sObject[] { lead });
 }
 catch (Exception ex)
 { }
 if (saveResult[0].success)
  res = 1;
 else {
  _log.Error(String.Format("Failure in UpdateLead: {0}", saveResult[0].errors[0].message));
  _log.Error("Lead ID : " + leadid + "      RSM : " + rsm);
 }
}
else
 res= 0;
 
SuperfellSuperfell
because you're round tripping the query results (always a bad practice IMO), you're sending the Id field twice, once in the base sobject, and once as a field in the Any, hence the error.
In fact in your case, the query is a waste of time, you can skip it altogether and just construct the sobject, set the Id field to the lead Id, and set a single element in the Any array for the lookup field.
FrankFFrankF
Simon,

Thanks very much for your response. I've resolved the problem using your advice.

I'm just curious, even though it is a round trip, why it would have worked for OwnerId but not for Qualifying_Campaign__c.

Thanks again,
Frank.