To support the workflow of my Ubercart/Salesforce integation, it would be ideal if sf_prematch.module could handle cases in which there is no existing Contact record that matches the fields specified for prematching, but there is a Lead record that matches.

Preferably, then, although this might be best architected as a separate module, rather than part of sf_prematch, the Lead would automatically get converted to a Contact and the fields upserted (or however Salesforce API works around the upsert bug).

I don't know how difficult this would be, but I am presuming that the second part (lead conversion) would be more difficult, since I don't know how the actual SF API Toolkit handles this programmatically.

The first part (just finding the match) would just mean running a SOQL query against two "tables" instead of one, as far as I know. This would involve changing the sf_prematch_export() function in sf_prematch.main.inc to build out two separate $from_where_clause variables, one per table, and then concatenate to two separate queries, which would be run sequentially.

The Lead/Contact dual prematching would probably have to have a checkbox in the prematching configuration UI as well, since not everyone would want it. And it would only be available as an option when the primary fieldmapping was to a Contact record.

I may work on this myself, but probably not, since for now my boss is willing to live with Leads that never get converted into Contacts. I wanted to open this issue though since I thought others might be interested in this feature, and at least this description provides a roadmap for how it could be done.

Comments

EvanDonovan’s picture

Assigned: Unassigned » EvanDonovan

Now the upsert bug doesn't exist anymore, so I think this would just be a matter of calling the relevant function from the PHP Toolkit, after a matching LeadId had been found (at least if the checkbox had been checked for this). Looks like this code from 13.0 would work (http://wiki.developerforce.com/index.php/PHP_Toolkit_13.0_ConvertLead_Sa...(Enterprise)), but probably would want to clean it up a bit to conform to the module's coding standards.

I may have code for this shortly.

LetUsBePrecise’s picture

We also have this requirement. We thought converting contact on drupal side would be easier but how to do same on salesforce side, that is mystery for us. Hope someone has solution for this. Still searching for solution...........

rjacobs’s picture

HI,

I think that managing lead conversions could take on many forms, and it would certainly be useful if some flexible solution existed (potentially part of the suite).

I'm confronted with a scenario that is a bit different than what Evan described at the start of this thread, but one that I think may benefit from the same technical solution. We have leads that come in to Salesforce independent of any Drupal user data, while our Drupal user data needs to be directly exported as Salesforce contacts (once the individual is a Drupal user, we want them to be a full contact). The added twist is that whenever a contact is exported, we want to check if a Lead for that contact already exists, and if so, convert it (and then export to that converted contact). This way we don't have duplicates between Leads and Contacts. This is pretty close to what Evan outlined, just a slightly different context perhaps.

Anyway, my thought was to go about the lead conversion process completely separately from the contact export. If it's possible to run an autonomous prematch check, and possible conversion, first (before any contact export effort happen), then once the contact export kicks in, we already have a contact record to sync to.

Thanks to the link highlighted in #1 (http://wiki.developerforce.com/index.php/PHP_Toolkit_13.0_ConvertLead_Sa...(Enterprise)) I think I may have a working prototype that does this... at least as a custom module. I'll see if it does indeed work, and then share whatever I can.

rjacobs’s picture

I went ahead and bundled-up my code into an experimental module project:

http://drupal.org/sandbox/rjacobs/1240978

You can browse the code in the repository (http://drupalcode.org/sandbox/rjacobs/1240978.git), grab a snapshot to try out, etc.

Basically I'm using hook_salesforce_api_pre_export() to intervene just before a new contact is about to to be exported to salesforce. If configured, a function sf_convert_lead() kicks in and checks for a lead match (based on whatever prematch criteria you want to define within a user-to-lead fieldmap), and if it finds one it converts it to a contact, and returns this newly created contact Id to the export object (so that the contact export taking place will map to the new, now existing contact, instead of creating a new one).

I've not thought this through completely, and have only tested it minimally, but it has been somewhat promising. It would be great to see what others think.

Ryan

EvanDonovan’s picture

Assigned: EvanDonovan » Unassigned

Awesome, rjacobs - you wrote the code exactly as I was planning to do it, and satisfied the use case that I need as well. I am deploying the site today, so I won't use this immediately, but I will hopefully be able to test in a few weeks at least.

Also, I'm unassigning to myself, since I will not be focusing on this as much.

I think you are right maybe, that this should be a separate module, not part of sf_prematch.

rjacobs’s picture

Glad this may prove useful to others. If anyone can test it out, please let us know your results.

I'll surely be making updates during testing on my end, so be sure to grab the most recent iteration of the code.

EvanDonovan’s picture

I thought that this was very useful, but I needed to add in the ability to link up Leads to existing Contacts, so I have done so in #1341458: Determine how to handle linkage of Leads to existing Contacts.

I think this should eventually become a full project, but I think we'll need the lead conversion settings configurable in that case.

rjacobs’s picture

Thanks Evan. I think your suggestions make sense, thanks for opening an issue in the sandbox. I also added one for your second point (make lead conversion settings configurable): #1351002: Add configurable settings for lead conversion into the admin UI

kostajh’s picture

Status: Active » Closed (won't fix)