function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott M - SFDC FanScott M - SFDC Fan 

Need a pattern for inserting records then associating them

Hi,

 

I have a class that saves two case records per asset correctly. However, now the specification changed and the two cases must also relate to each other too, so when the user sees one case, they'll also see the related case in a related list. The original insert for the two cases per asset uses this code.

 

        if(!tmp_newCaseList.isEmpty())

        {

            Database.SaveResult[] tmp_saveResList = new Database.SaveResult[]{};

           

            try

            {

                tmp_saveResList = Database.insert(tmp_newCaseList, false);

            }

            catch(Exception e)

            {

                throw e;

            }

           

            if(tmp_saveResList != null && !tmp_saveResList.isEmpty())

            {

                for(Integer i = 0; i < tmp_saveResList.size(); i++)

                {

                    Database.SaveResult tmp_saveRes = tmp_saveResList.get(i);

                   

                    if(!tmp_saveRes.isSuccess())

                    {

                        String tmp_serialNo = tmp_newCaseList.get(i).Asset.SerialNumber;

                       

                        if(!a_failedNumbers.contains(tmp_serialNo))

                        {

                            a_failedNumbersMsg += tmp_serialNo + ', ';

                           

                            a_failedNumbers.add(tmp_serialNo);

                        }

                    }

                }

            }

           

            if(a_failedNumbersMsg != '')

            {

                a_failedNumbersMsg = a_failedNumbersMsg.substring(0, a_failedNumbersMsg.length() - 2);

            }

        }

       

        return Page.BulletinCaseResult;

    }

 

tmp_newCaseList is the list of cases.

 

Is there a relatively easy way to get the ID from one case of a certain asset and insert it into the other case created for that same asset using the tmp_saveResList to then update those cases?

 

Scott

Avidev9Avidev9

Well when you do a insert call, once the call is successfull the IDs gets populated in the sobject

 

   if (!tmp_newCaseList.isEmpty()) {
       Database.SaveResult[] tmp_saveResList = new Database.SaveResult[] {};
       //try catch not required because you are using database.insert
       tmp_saveResList = Database.insert(tmp_newCaseList, false);
       //at this every successfull record will have ID populated you can use that to populate the lookup by iterating through the record make changes accordingly
       if (tmp_saveResList != null && !tmp_saveResList.isEmpty()) {
           for (Integer i = 0; i < tmp_saveResList.size(); i++) {
               Database.SaveResult tmp_saveRes = tmp_saveResList.get(i);

               if (!tmp_saveRes.isSuccess()) {
                   String tmp_serialNo = tmp_newCaseList.get(i).Asset.SerialNumber;

                   if (!a_failedNumbers.contains(tmp_serialNo)) {
                       a_failedNumbersMsg += tmp_serialNo + ', ';

                       a_failedNumbers.add(tmp_serialNo);
                   }
               }
           }
       }

       if (a_failedNumbersMsg != '') {
           a_failedNumbersMsg = a_failedNumbersMsg.substring(0, a_failedNumbersMsg.length() - 2);
       }
   }

   return Page.BulletinCaseResult;
   }

 

Scott M - SFDC FanScott M - SFDC Fan

Thanks Avi.

 

I basically new that and also started working on the iteration. But, for example, can I iterate simply on tmp_saveResList, make the needed associations and then update with tmp_saveResList?

 

Like with

 

tmp_updateResList = Database.update(tmp_saveResList, false);

 

Scott

Avidev9Avidev9
Exactly you got it. Iterate through the "tmp_newCaseList" populate the lookup and then do an update.
Scott M - SFDC FanScott M - SFDC Fan

Hmm...then are the new IDs in the temp_newCaseList too? I though they were only in the tmp_saveResList? Or do I have to find a way to compare the two lists and then make the association? What if some of the inserts failed? I only need to associate cases that were succesfully inserted.

 

Scott

 

Avidev9Avidev9
Yes you are in correct path.
after insert Ids get populated in the "tmp_newCaseList" aswell