Is it possible to grab the external Ids of the records that failed in database.insert










3















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?










share|improve this question

















  • 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















3















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?










share|improve this question

















  • 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













3












3








3








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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










1 Answer
1






active

oldest

votes


















6














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;





share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    6














    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;





    share|improve this answer



























      6














      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;





      share|improve this answer

























        6












        6








        6







        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;





        share|improve this answer













        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;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 27 '18 at 22:08









        sfdcfoxsfdcfox

        259k12204447




        259k12204447



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ