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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












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 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 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 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 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 at 22:31
















up vote
3
down vote

favorite












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 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 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 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 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 at 22:31












up vote
3
down vote

favorite









up vote
3
down vote

favorite











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?









share|improve this question











share|improve this question




share|improve this question










asked Aug 27 at 21:32









Lance Shi

7,19522568




7,19522568







  • 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 at 22:31










1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted










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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote



    accepted










    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
























      up vote
      6
      down vote



      accepted










      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






















        up vote
        6
        down vote



        accepted







        up vote
        6
        down vote



        accepted






        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 at 22:08









        sfdcfox

        226k10171385




        226k10171385



























             

            draft saved


            draft discarded















































             


            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery