Is it possible to grab the external Ids of the records that failed in database.insert
We are trying to insert some records into database by using Database.SaveResult. It is dynamically typed Sobjects as in the following code:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
However, I won't be able to get the Id of the records which failed. So it would be really hard to find out which records are actually failing. Is there a possible way to find out the external Ids of the related records when it failed inserting?
apex dml
|
show 2 more comments
We are trying to insert some records into database by using Database.SaveResult. It is dynamically typed Sobjects as in the following code:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
However, I won't be able to get the Id of the records which failed. So it would be really hard to find out which records are actually failing. Is there a possible way to find out the external Ids of the related records when it failed inserting?
apex dml
1
I think you have come across a situation I was in few years back. Don't think there's a way to do it now. Had posted an idea around this for almost a similar use case.
– Jayant Das
Aug 27 '18 at 21:38
@JayantDas Thank you for your response and I am with you based on the value of Database.SaveResult. However, I am wondering how is dataloader / Jitterbit able to link the error message to the relevant record? There should an approach though...
– Lance Shi
Aug 27 '18 at 21:59
1
There is no ID available as the record was never created. The List you are inserting will be always in the same order as the resultset, So you could do something by iterating over the resultset.
– Narendra Nimmana
Aug 27 '18 at 22:05
@JayantDas I actually crafted a solution that works for your use case from that idea. You just build a second list to associate the records to, taking advantage of the fact that the DML result order is the same as the original DML list order. Hold on a sec, I'll copy some code for you.
– sfdcfox
Aug 27 '18 at 22:10
Thanks @sfdcfox. I couldn’t actually recollect what we had done that time (yeah that’s true :)). But now recollect it was similar to what you have it here (and that’s why hesitated to write an answer). Actually realized having an api would have really helped here instead of additional logic.
– Jayant Das
Aug 27 '18 at 22:31
|
show 2 more comments
We are trying to insert some records into database by using Database.SaveResult. It is dynamically typed Sobjects as in the following code:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
However, I won't be able to get the Id of the records which failed. So it would be really hard to find out which records are actually failing. Is there a possible way to find out the external Ids of the related records when it failed inserting?
apex dml
We are trying to insert some records into database by using Database.SaveResult. It is dynamically typed Sobjects as in the following code:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
However, I won't be able to get the Id of the records which failed. So it would be really hard to find out which records are actually failing. Is there a possible way to find out the external Ids of the related records when it failed inserting?
apex dml
apex dml
asked Aug 27 '18 at 21:32
Lance ShiLance Shi
7,48933076
7,48933076
1
I think you have come across a situation I was in few years back. Don't think there's a way to do it now. Had posted an idea around this for almost a similar use case.
– Jayant Das
Aug 27 '18 at 21:38
@JayantDas Thank you for your response and I am with you based on the value of Database.SaveResult. However, I am wondering how is dataloader / Jitterbit able to link the error message to the relevant record? There should an approach though...
– Lance Shi
Aug 27 '18 at 21:59
1
There is no ID available as the record was never created. The List you are inserting will be always in the same order as the resultset, So you could do something by iterating over the resultset.
– Narendra Nimmana
Aug 27 '18 at 22:05
@JayantDas I actually crafted a solution that works for your use case from that idea. You just build a second list to associate the records to, taking advantage of the fact that the DML result order is the same as the original DML list order. Hold on a sec, I'll copy some code for you.
– sfdcfox
Aug 27 '18 at 22:10
Thanks @sfdcfox. I couldn’t actually recollect what we had done that time (yeah that’s true :)). But now recollect it was similar to what you have it here (and that’s why hesitated to write an answer). Actually realized having an api would have really helped here instead of additional logic.
– Jayant Das
Aug 27 '18 at 22:31
|
show 2 more comments
1
I think you have come across a situation I was in few years back. Don't think there's a way to do it now. Had posted an idea around this for almost a similar use case.
– Jayant Das
Aug 27 '18 at 21:38
@JayantDas Thank you for your response and I am with you based on the value of Database.SaveResult. However, I am wondering how is dataloader / Jitterbit able to link the error message to the relevant record? There should an approach though...
– Lance Shi
Aug 27 '18 at 21:59
1
There is no ID available as the record was never created. The List you are inserting will be always in the same order as the resultset, So you could do something by iterating over the resultset.
– Narendra Nimmana
Aug 27 '18 at 22:05
@JayantDas I actually crafted a solution that works for your use case from that idea. You just build a second list to associate the records to, taking advantage of the fact that the DML result order is the same as the original DML list order. Hold on a sec, I'll copy some code for you.
– sfdcfox
Aug 27 '18 at 22:10
Thanks @sfdcfox. I couldn’t actually recollect what we had done that time (yeah that’s true :)). But now recollect it was similar to what you have it here (and that’s why hesitated to write an answer). Actually realized having an api would have really helped here instead of additional logic.
– Jayant Das
Aug 27 '18 at 22:31
1
1
I think you have come across a situation I was in few years back. Don't think there's a way to do it now. Had posted an idea around this for almost a similar use case.
– Jayant Das
Aug 27 '18 at 21:38
I think you have come across a situation I was in few years back. Don't think there's a way to do it now. Had posted an idea around this for almost a similar use case.
– Jayant Das
Aug 27 '18 at 21:38
@JayantDas Thank you for your response and I am with you based on the value of Database.SaveResult. However, I am wondering how is dataloader / Jitterbit able to link the error message to the relevant record? There should an approach though...
– Lance Shi
Aug 27 '18 at 21:59
@JayantDas Thank you for your response and I am with you based on the value of Database.SaveResult. However, I am wondering how is dataloader / Jitterbit able to link the error message to the relevant record? There should an approach though...
– Lance Shi
Aug 27 '18 at 21:59
1
1
There is no ID available as the record was never created. The List you are inserting will be always in the same order as the resultset, So you could do something by iterating over the resultset.
– Narendra Nimmana
Aug 27 '18 at 22:05
There is no ID available as the record was never created. The List you are inserting will be always in the same order as the resultset, So you could do something by iterating over the resultset.
– Narendra Nimmana
Aug 27 '18 at 22:05
@JayantDas I actually crafted a solution that works for your use case from that idea. You just build a second list to associate the records to, taking advantage of the fact that the DML result order is the same as the original DML list order. Hold on a sec, I'll copy some code for you.
– sfdcfox
Aug 27 '18 at 22:10
@JayantDas I actually crafted a solution that works for your use case from that idea. You just build a second list to associate the records to, taking advantage of the fact that the DML result order is the same as the original DML list order. Hold on a sec, I'll copy some code for you.
– sfdcfox
Aug 27 '18 at 22:10
Thanks @sfdcfox. I couldn’t actually recollect what we had done that time (yeah that’s true :)). But now recollect it was similar to what you have it here (and that’s why hesitated to write an answer). Actually realized having an api would have really helped here instead of additional logic.
– Jayant Das
Aug 27 '18 at 22:31
Thanks @sfdcfox. I couldn’t actually recollect what we had done that time (yeah that’s true :)). But now recollect it was similar to what you have it here (and that’s why hesitated to write an answer). Actually realized having an api would have really helped here instead of additional logic.
– Jayant Das
Aug 27 '18 at 22:31
|
show 2 more comments
1 Answer
1
active
oldest
votes
The order of the Database.SaveResult items will be the same order as the original DML list. As such, you can figure out the external Id values simply by looping over the list:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
Set<String> failedIds = new Set<String>();
for(Integer i = 0, s = insertResultList.size(); i < s; i++)
if(!insertResultList[i].isSuccess())
failedIds.add(castInsertList[i].get(extIdField));
return failedIds;
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230278%2fis-it-possible-to-grab-the-external-ids-of-the-records-that-failed-in-database-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The order of the Database.SaveResult items will be the same order as the original DML list. As such, you can figure out the external Id values simply by looping over the list:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
Set<String> failedIds = new Set<String>();
for(Integer i = 0, s = insertResultList.size(); i < s; i++)
if(!insertResultList[i].isSuccess())
failedIds.add(castInsertList[i].get(extIdField));
return failedIds;
add a comment |
The order of the Database.SaveResult items will be the same order as the original DML list. As such, you can figure out the external Id values simply by looping over the list:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
Set<String> failedIds = new Set<String>();
for(Integer i = 0, s = insertResultList.size(); i < s; i++)
if(!insertResultList[i].isSuccess())
failedIds.add(castInsertList[i].get(extIdField));
return failedIds;
add a comment |
The order of the Database.SaveResult items will be the same order as the original DML list. As such, you can figure out the external Id values simply by looping over the list:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
Set<String> failedIds = new Set<String>();
for(Integer i = 0, s = insertResultList.size(); i < s; i++)
if(!insertResultList[i].isSuccess())
failedIds.add(castInsertList[i].get(extIdField));
return failedIds;
The order of the Database.SaveResult items will be the same order as the original DML list. As such, you can figure out the external Id values simply by looping over the list:
List<SObject> castInsertRecords = (List<SObject>)Type.forName(listType).newInstance();
castInsertRecords.addAll(dataToInsertList);
Database.SaveResult insertResultList = Database.insert(castInsertRecords, false);
Set<String> failedIds = new Set<String>();
for(Integer i = 0, s = insertResultList.size(); i < s; i++)
if(!insertResultList[i].isSuccess())
failedIds.add(castInsertList[i].get(extIdField));
return failedIds;
answered Aug 27 '18 at 22:08
sfdcfoxsfdcfox
259k12204447
259k12204447
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230278%2fis-it-possible-to-grab-the-external-ids-of-the-records-that-failed-in-database-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown


1
I think you have come across a situation I was in few years back. Don't think there's a way to do it now. Had posted an idea around this for almost a similar use case.
– Jayant Das
Aug 27 '18 at 21:38
@JayantDas Thank you for your response and I am with you based on the value of Database.SaveResult. However, I am wondering how is dataloader / Jitterbit able to link the error message to the relevant record? There should an approach though...
– Lance Shi
Aug 27 '18 at 21:59
1
There is no ID available as the record was never created. The List you are inserting will be always in the same order as the resultset, So you could do something by iterating over the resultset.
– Narendra Nimmana
Aug 27 '18 at 22:05
@JayantDas I actually crafted a solution that works for your use case from that idea. You just build a second list to associate the records to, taking advantage of the fact that the DML result order is the same as the original DML list order. Hold on a sec, I'll copy some code for you.
– sfdcfox
Aug 27 '18 at 22:10
Thanks @sfdcfox. I couldn’t actually recollect what we had done that time (yeah that’s true :)). But now recollect it was similar to what you have it here (and that’s why hesitated to write an answer). Actually realized having an api would have really helped here instead of additional logic.
– Jayant Das
Aug 27 '18 at 22:31