0

i having some problem in salesforce checkmarx report. this generated one waring : Bulkify_Apex_Methods_Using_Collections_In_Methods in particular code

        interested_prop = String.escapeSingleQuotes(interested_prop);            
        String qry = 'select ' + sObjectUtility.sObjectFields('srex__Matches__c') +' Id from Matches__c';
        string whereString = ' where Interested_in_Dealing__c = false';
        whereString       += ' AND ('+ sObjName + ' =: propid ';
        whereString       += ' OR '+ lookup2 +' =: propid )';
        whereString       += ' AND ('+ sObjName + ' =: interested_prop ';
        whereString       += ' OR '+ lookup2 +' =: interested_prop )';


        list<Matches__c> matches = Database.query(String.escapeSingleQuotes(qry)+String.escapeSingleQuotes(whereString));

        if(matches.size() > 0){
            matches[0].Interested_in_Dealing__c = true;
            try{
                update matches;
            }catch(Exception ex){
                system.debug('Error occurred while perfoming DML Operation :::::'+ ex.getMessage());
            }
        }else{
            Matches__c new_match = new Matches__c();
            new_match.put(sObjName, prop_id);
            new_match.put(lookup2 , interested_prop);
            new_match.Interested_in_Dealing__c = true;
            try{
                List<Matches__c> matchlst = new List<Matches__c>();
                matchlst.add(new_match);
                insert matchlst;
            }catch(Exception ex){
                system.debug('Error occurred while perfoming DML Operation :::::'+ ex.getMessage());
            }
        }

pls help me out.

1
  • can u post ur complete code.. is this inside a method which is being called in a loop ? Dec 9, 2014 at 3:30

1 Answer 1

2

Per Salesforce documentation regarding for loop soql formats

  1. The single sObject format executes the for loop's once per sObject record. Consequently, it is easy to understand and use, but is grossly inefficient if you want to use data manipulation language (DML) statements within the for loop body. Each DML statement ends up processing only one sObject at a time.
  2. The sObject list format executes the for loop's once per list of 200 sObjects. Consequently, it is a little more difficult to understand and use, but is the optimal choice if you need to use DML statements within the for loop body. Each DML statement can bulk process a list of sObjects at a time.

You could try #2. Example below:

for(List<Matches__c> listOfMatches : [Select Id, Name from Matches__c]){
  for(Integer i=0; i<listOfMatches.size(); i++){
    //data manipulation logic
  }
  update listOfMatches;
}

I have done something like mentioned above to get rid of this warning from Checkmarx.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .