We have an app that pulls data from our rails server and loads it into SalesForce via batch jobs. The object that we load data into MyObj has 2 lookup fields for relating it to Contacts and Leads. The problem is that rails knows nothing about Contacts or Leads therefore it cannot set these lookup fields. The only relationship between MyObj and a Contact/Lead is an email address.

The problem that arises is that we end up having to query all Contacts and Leads, grab the ID and then set the lookup field in MyObj. When we ran checkmarx on this, it complained about "Queries with no Where clause or no LIMIT clause".

So the question is, how can we relate objects, without knowing the IDs of SalesForce objects and do this without exceeding the governor limits?

PS. External IDs are not an option since we do not have a relationship to Contacts/Leads in the first place.

  • Is pulling your Salesforce data down to a local server an option? Then you wouldn't need to worry about limits.
    – Mike Chale
    Commented Jul 18, 2013 at 17:46
  • Yes that is an option, how would it work? Commented Jul 18, 2013 at 17:54
  • You could process the data locally, including "populating" the lookups. This would make your import step simpler because the heavy lifting was already complete.
    – Mike Chale
    Commented Jul 18, 2013 at 18:47
  • Yes the thing is our app is currently deployed on Orgs that have upwards of 4k contacts, therefore this option would complicate things a bit. Is there an alternative that can be done on SF directly? Commented Jul 18, 2013 at 19:04
  • Couldn't you just use a trigger on MyObj and have that trigger populate the lookups in a before insert event? You'd just have to have them install some small app that contains the trigger.
    – sfdcfox
    Commented Jul 18, 2013 at 21:57

1 Answer 1


This is a pretty normal scenario, and an email address does pretty well for this purposes. Tricky part is figuring out what you're going to do when the email address matches multiple contacts and/or leads.

Regardless of how you implement this, you'll end up needing to query all contacts and leads with an email address matching the data you pull from your rail server and use that to set the lookup on your object.

Assuming you're processing everything in one massive call and need to have one massive call to get the data down a before trigger is an easy option. Triggers batch processing into sets of 200 records (at the moment at least). You can query the related contacts and leads there and update them.

  • This is almost exactly what i had to do. I queryied all Leads/Contacts by a set of emails (WHERE Email IN:= listOfEmails). I then created a map between Emails and ID so i can easily access them. Finally I manually populated the Lookup fields with the IDs. Commented Jul 20, 2013 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .