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
Radhe Shyam.Radhe Shyam. 

bulk insert in trigger

Hi Team,

I am trying to create a list in Trigger and adding the values in this list by iterating for loop.
After adding value, i am trying to insert this list out side for.

Things are fine if we are testing.

But when we are scanning our code by CheckMarx, it is saying that 'Need to be bulkify using collection'. 

Am i getting this error because there is limit of 10000 record to insert in trigger?
If yes, then how to proceed with this?

Please Help.

Radhe S




 
Best Answer chosen by Radhe Shyam.
Abdul KhatriAbdul Khatri
Hi Radhe,

I guess when you are iterating the loop and adding values in the list, it is crossing 10000 limit. You cannot have more than 10000 values in the List to perform any DML operations in one transaction. You have few options here
It's really hard to advise without knowing your whole situation.
 

All Answers

Dushyant SonwarDushyant Sonwar
Hi Radhe Shyam ,

I think you are using helper/handler class in trigger and your handler/helper methods are not bulkified.
Example :
public String getAccounts(account acc){
return '';
}

to
public String getAccounts(list<account> lstAccount){
return '';
}
you need to bulkify your methods by changing argument of single object to list.

Thanks,
 
Radhe Shyam.Radhe Shyam.
This is already bulky fied Warm Regards Radhe Shyam
Dushyant SonwarDushyant Sonwar
Could you post your code with trigger and helper?
Dushyant SonwarDushyant Sonwar
Checkmarx may highlight some false positives . If you are sure that , you have bulkified your apex method as above. Then you can report your false positives at sourcescanner@salesforce.com
Thanks,
Kamal ThakurKamal Thakur
Hi Radhe, Please provide some of your code. 
 
Abdul KhatriAbdul Khatri
Hi Radhe,

I guess when you are iterating the loop and adding values in the list, it is crossing 10000 limit. You cannot have more than 10000 values in the List to perform any DML operations in one transaction. You have few options here
It's really hard to advise without knowing your whole situation.
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Radhe,

Can you please post your code here so we can help you
Radhe Shyam.Radhe Shyam.
Map < id, List < Account >> rule_wise_records = new Map < id, List < Account >> ();
Map < id, List < String >> rule_wise_Users = new Map < id, List < String >> ();

//Filling these maps in  class. Now trying to get records and processing 

//Get the values from Both Maps and Process the Records
Set < String > SetRecordWithId = new Set < String > ();

list < Account > AccRecords = new list < Account > ();
list < string > UserIDs = new list < string > ();
for (id rule: rule_wise_records.keySet()) //rule_wise_records : this map contain Some RuleId as key and List of Accounts as Value
{
    if (rule_wise_records.containsKey(rule)) {
        AccRecords = (list < Account > ) rule_wise_records.get(rule);
        for (Account sobj: AccRecords) {
            SetRecordWithId.add(sobj.id);
        }
    }
}


for (String eachRec: SetRecordWithId) {
    string parentId;
    parentId = String.valueof(eachRec.substringAfter('@')); //.substring(0,15);
    UserIDs = rule_wise_Users.get(String.valueof(eachRec.substringBefore('@'))); //rule_wise_Users : contains some rule as key and List of users as value
    for (String sId: UserIDs) {
        EntitySubscription e = new EntitySubscription();
        e.ParentId = id.valueof(parentId); //parentId
        e.SubscriberId = id.valueof(sId); //sId
        ES_ListToinsert.add(e);
        System.debug('I am in Maps ES_ListToinsert ' + ES_ListToinsert);
    }

}
//System.debug('Each Rule Wise single Record '+json.serialize(rulewise_record));
if (ES_ListToinsert != null && ES_ListToinsert.size() > 0 && ES_ListToinsert.size() < 9999) { //Tried ES_ListToinsert.size()<9999 - but no result
    if (!Schema.sObjectType.EntitySubscription.fields.ParentId.isCreateable() && !Schema.sObjectType.EntitySubscription.fields.SubscriberId.isCreateable()) {
        //Do nothing
    } else {
        insert ES_ListToinsert;
        //I tried Database.Insert as well, but same result.
    }
}

 
Radhe Shyam.Radhe Shyam.
Hi Amit, Abdul, Kamal,Dushyant,

This is the error i am getting from checkmarx. on Insert the list of records.
User-added image
Radhe Shyam.Radhe Shyam.
@Abdul, I think you are right here...I should go with batch.
Abdul KhatriAbdul Khatri
Hey Radhe,

I see couple of issues with the code and it has a lot of room for improvements. some mentioned here 

User-added image

Anyhow I would suggest to go with batch if you are expecting to working on the bulk updates greater than 10000.

There is also another following trick to handle collection less than 49999 using SOQL. It is called SOQL loop to load lists of 200 items at a time but it work with SOQL only. See if you can use this in your situation.
for(List<Account>  accounts : [SELECT Id, Name FROM Account])
{
    for(Account account: accounts) {
        
        //perform operation here
        
        //The system automatically rturns the results in lists of 200 records, maintaining a query locator 
        //so that the operation still counts as a single query
    }
}

I hope this help.
 
Abdul KhatriAbdul Khatri
Hi, Please let me know if you need any specific help. 

Was above suggestion helpful?
Radhe Shyam.Radhe Shyam.
Hi Abdul, all that @, the combination of two id was required.

By the way, issue was not the for inside for which i was thinking. Issue was insert stmnt which i placed here in trigger. I passed this list for insertion in batch and inserted there. It works. chkmarx error eliminated.

Thank you man for your active co-operation.

Radhe S