1

Basically I have this code right now

$createResponse = $client->create(array((object) $Billing_Obj), 'Lead');

foreach ($createResponse as $lead) {
    $Lead_Id = $lead->getId();
}

and I'd like to change it to something like this

$createResponse = $client->upsert( 'Company', array((object) $Billing_Obj), 'Lead');

foreach ($createResponse as $lead) {
    $Lead_Id = $lead->getId();
}

any ideas on how to make that work? I get an error back saying

PHP Fatal error:  Uncaught SoapFault exception: [sf:INVALID_FIELD] INVALID_FIELD: Field name provided, Company does not match an External ID, Salesforce Id, or indexed field for Lead

I'm using the Phpforce SOAP toolkit, thanks

0

Upsert is possible but your "Company" field should be an external id field. However, Company is not a external Id field so you may consider making an another external id field called "Record_Key__c". Use combination of fields to make this field unique, like - Company + Some field 1 + Some field 2. Prepare a csv file following this and update Record_Key__c field using dataloader with this combination data. Let's go through the code below:

List<Database.Upsertresult> ur =  new List<Database.Upsertresult>();
//sObjectlist would be list of sObjects
//The second parameter is a reference to the external Id field, which is written as objectname.fieldname.
ur = Database.upsert(sObjectList,ObjectName__c.Record_Key__c,false);

I hope this helps.

0

The upsert operation is intended for two kinds of keys: the standard Id field and any custom field flagged as External Id.

But you need to understand the behavior of upsert. From the API documentation:

  • If the external ID is not matched, then a new record is created.
  • If the external ID is matched once, then the existing record is updated.
  • If the external ID is matched multiple times, then an error is reported.
  • When batch updating multiple records where the external ID is the same for two or more records in your batch call, those records will be marked as errors in the UpsertResult file. The records will be neither created or updated.

Even if you could set Lead.Company to be an external Id (which you can't, incidentally), you wouldn't want to, I don't think. What if you get two leads, two people, but the same company?

Upsert should really only be used when you're expecting Uniqueness. Or even better, if you're enforcing it on the field with the Unique flag.

2
  • I actually want it to only have one record per company, meaning if a new person comes up from the same company, it should update the company's information with whatever info that person entered as they are now the buyer for that company. So in this case I do want Company to be unique. Is there any way to do so?
    – zacbrac
    Jun 2 '15 at 16:43
  • That's a funny way to use lead. I think you will cause yourself headaches. And you loose data (the person). Leads can be converted and appear to be gone...Curious why this approach? If you're married to it, then you'll have to implement "upsert" manually using some kind of workaround.
    – pchittum
    Jun 2 '15 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.