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
Shuhbam SinhaShuhbam Sinha 

how to call method from another method multiple times

Hello,
I have one method where i am inserting 10 records at a single time . Now I want after all the record is inserted then another method should be fired for all the inserted Ids at that time only . I dnot want to use for loop as the another method is having soql and some dml operations. please suggest another way. Thanks in advance.
// below I am inserting the records

   if(!recordUpdate.isEmpty()){
                for (Case multipleRecords : recordUpdate){
                	//updateChecklistRecords.putall(multipleRecords);      
                	lstUpdateChecklistRecords.add(multipleRecords)  ; 
                }
            }
            if(lstUpdateChecklistRecords.size()>0){
                insert lstUpdateChecklistRecords; 
            }


// calling the another method

             for(Case insertedRecord : lstUpdateChecklistRecords){
             	System.debug('case>>>'+insertedRecord.id);
                 AccountClass.bankload(insertedRecord.Id) // another method.
        	} 

 
Ajay choudharyAjay choudhary
Hi Shubam,

Here's a suggestion based on your requirements:
  1. Collect All Inserted IDs: After you insert the lstUpdateChecklistRecords, all the inserted records will have their Ids populated. You can collect all these Ids in a Set.
  2. Modify Your Method: Instead of having your bankload method accept a single Id, modify it to accept a Set of Ids or List of Ids. Adjust the method to work in a bulkified manner, meaning it can process multiple records efficiently without hitting governor limits.

 Sample code

// Insert records
if(!recordUpdate.isEmpty()){
    insert lstUpdateChecklistRecords; 
}

// Collect all inserted Ids
Set<Id> insertedIds = new Set<Id>();
for(Case insertedRecord : lstUpdateChecklistRecords){
    insertedIds.add(insertedRecord.Id);
}

// Call the modified method
AccountClass.bankload(insertedIds);




public static void bankload(Set<Id> caseIds) {
    // Adjust your SOQL to fetch records based on the provided caseIds
    List<Case> cases = [SELECT ... FROM Case WHERE Id IN :caseIds];

    // Process the cases and perform your DML operations in a bulkified way
    ...
}
Hope this helps,
Thanks