+ Start a Discussion
Dhaval PanchalDhaval Panchal 

Strange Issue with Bulk Upsert: INVALID_CROSS_REFERENCE_KEY

Hello,

I am facing strange issue with Bulk API (insert/upsert).
below is the error:
   Error in upsert (Account) record:
   Error Message - Record Type ID: this ID value isn't valid for the user:
   Status Code - INVALID_CROSS_REFERENCE_KEY
   Fields: RecordTypeId

But when I use SOAP API it works perfact this issue only occurs when I use Bulk API.

Any Idea?

Thanks,
Dhaval Panchal
Ashish_SFDCAshish_SFDC
Hi Dhaval, 

Can you post your code and the point out the error line. 

Regards,
Ashish
Dhaval PanchalDhaval Panchal
below is the code:


List<saveResult> upsert(String objectName, List<mySObject> lstObj, out String responseFromServer, bulkLogin binding, String externalId)
        {
            responseFromServer = "";
            List<saveResult> lstResult = new List<saveResult>();
            try
            {
                String jobId = "";
                String batchId = "";
                String batchStatus = "";
                String resultId = "";
                String result = "";
                serverLog = "Creating job for operation : " + common.OPERATION_UPSERT;
                if (CreateJob(ref responseFromServer, ref jobId, common.OPERATION_UPSERT, objectName, binding, externalId) == true)
                {
                    serverLog = "Job created successfully with job id: " + jobId;
                    StringBuilder upsertXML = xmlTemplate.getObjectXML(lstObj);
                   
                    if (upsertXML.Length>0)
                    {
                        serverLog = "Creating batch...";
                        if (CreateBatch(jobId, ref responseFromServer, ref batchId, upsertXML, binding) == true)
                        {
                            serverLog = "Batch created successfully with job id: " + batchId;
                            Boolean statusCompleted = false;
                            serverLog = "Checking status of batch.";
                            do
                            {
                                Thread.Sleep(waitingTime(lstObj.Count));
                                statusCompleted = checkBatchStatus(batchId, jobId, ref responseFromServer, ref batchStatus, binding);
                                serverLog = "BATCH STATUS:" + batchStatus;
                            } while (statusCompleted == false);

                            if (statusCompleted == true && batchStatus.ToUpper() == common.BATCHSTATUS_COMPLETED.ToUpper())
                            {
                                serverLog = "Retrieving result.";
                                if (getSaveResult(batchId, jobId, ref responseFromServer, ref lstResult, binding) == true)
                                {
                                }
                                else
                                {
                                }
                            }
                            else
                            {
                                String error = "ERROR FOUND\n";
                                error += "BATCH STATUS :" + batchStatus;
                                error += "\r\nRESPONSE :\r\n" + responseFromServer;
                                error += "\r\n\r\nXML sent to server \r\n" + upsertXML;
                                serverLog = error;
                            }
                        }
                        else
                        {
                            String error = "";
                            error = "Failed to create batch.";
                            error += "\r\nRESPONSE :\r\n" + responseFromServer;
                            serverLog = error;
                        }
                    }

                    if (closeJob(jobId, ref responseFromServer, binding) == true)
                    {
                        //MessageBox.Show("Job closed successfully.");
                        serverLog = "Job closed successfully.";
                    }
                    else
                    {
                        //MessageBox.Show(responseFromServer);
                        serverLog = "Error in closing job\n" + responseFromServer;
                    }
                }
                else
                {
                    String error = "";
                    error = "Failed to create job.";
                    error += "\nRESPONSE :\n" + responseFromServer;
                    serverLog = error;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, "upsert(String, List<mySObject>, out String, bulkLogin, String)", responseFromServer);
                responseFromServer += ex.Message;
            }
            return lstResult;
        }
        public Boolean getSaveResult(String batchId, String jobId, ref String responseFromServer, ref List<saveResult> lstResult, bulkLogin binding)
        {
            try
            {
                responseFromServer = "";
                String url = "";
                url = binding.session.hostUrl + "/services/async/" + apiVersion + "/job/" + jobId + "/batch/" + batchId + "/result";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Headers.Add("X-SFDC-Session", binding.session.sessionId);
                request.KeepAlive = false;
                request.Host = binding.session.host;
                request.UserAgent = ".NET Framework Test Client";
                request.Accept = "application/xml";
                request.Method = WebRequestMethods.Http.Get;

                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream datastream = response.GetResponseStream();


                    if (HttpStatusCode.OK == response.StatusCode || HttpStatusCode.Created == response.StatusCode)
                    {
                        Stream dataStream = response.GetResponseStream();
                        StreamReader reader = new StreamReader(datastream);
                        responseFromServer = reader.ReadToEnd();
                    }
                    List<String> resultIds = new List<String>();
                    if (common.NVL(responseFromServer) != "")
                    {
                        XmlDocument responseXml = new XmlDocument();
                        responseXml.LoadXml(responseFromServer);
                        lstResult = parseSaveResultXML(responseXml);
                        //resultId = xmlTemplate.getValueFromXML(responseXml, "id");
                    }
                    response.Close();
                    if (lstResult != null && lstResult.Count>0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (WebException e)
                {
                    using (WebResponse response = e.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        responseFromServer = string.Format("Error code: {0}  ", httpResponse.StatusCode);
                        using (Stream data = response.GetResponseStream())
                        {
                            responseFromServer += new StreamReader(data).ReadToEnd();
                        }
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                responseFromServer += ex.Message;
                return false;
            }
        }
Ashish_SFDCAshish_SFDC
Hi Dhaval,

This issue comes up when we are referencing a deleted SObject or record. 

Can you point on which line of thsi code you are getting error. 

Regards,
Ashish