Search code examples
ruby-on-railssalesforceomniauthdatabasedotcom-gem

Salesforce error - "invalid cross reference id"


I am developing a Rails app using OmniAuth, OmniAuth-salesforce and this gem: https://github.com/heroku/databasedotcom

I am hardcoding the "client_id" and "client_secret" into my app. Then for each user that authenticates with Saleforce, I am capturing their oauth token, user id, and instance url. With all this, I am creating leads on their behalf.

Everything works if the user is from the same account where I got the "client_id" and "client_secret". However, if I authenticate with a user from another Salesforce instance, I get an "invalid cross reference id" error.

I want my app to be submitting leads for users from many different Salesforce intances. Is this not possible?

Here is my full code:

client = Databasedotcom::Client.new :client_id => SALESFORCE_CLIENT_ID, :client_secret => SALESFORCE_CLIENT_SECRET
client.authenticate :token => user.salesforce_token, :instance_url => user.salesforce_instance_url
client.materialize("Lead")

lead = Lead.new(:FirstName => first_name, :LastName => last_name, :Email => email, 
                :Phone => phone, :OwnerId => user.salesforce_id, :IsConverted => false,
                :IsUnreadByOwner => true, :Company => contact_company)
lead.save

Thanks for any advice!


Solution

  • The error will be coming from the user.salesforce_id reference you're setting for ownerId, and leads me to think that you are somehow mixing the data up, i.e. you're sending user1 but to the user2's session. I haven't used the gem you mention, but from the code, i can't see how the lead instance is associated with the particular client instance. How does lead.save know to use the client instance you just created ?

    Looking more at how materialize("Lead") works, it seems like the Lead.new is always going to create a lead that associated with the first client instance you created, so you'd be trying to send all the leads the first user that you authenticate.