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
Deepak ChouhanDeepak Chouhan 

Chckmarx Issue : Bulkify apex methods using collections In methods

Hi,
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
pconpcon
This is because after your size comparison, you are only operating on a single instance of the list and ignoring the rest.  I would modify your code to be the following:
 
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.isEmpty()) {
    for (Matches__c match: matches) {
        match.Interested_in_Dealing__c = true;
    }
} 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;
    matches.add(new_match);
}

if (!matches.isEmpty()) {
    try {
        upsert matches;
    } catch(Exception ex) {
        system.debug('Error occurred while perfoming DML Operation :::::'+ ex.getMessage());
    }
}

This will set Interested_in_Dealing__c to true for every match, and if there are no matches create a new one.  The upsert the entire list.

NOTE: This code has not been tested and may contain typographical or logical errors
NewBie09NewBie09
can you guys tell me what's wrong with my code
public static void mobiFormRecordDtlsUpdate(sObject tplan){
                
        try{
            sObject updateDtls = tplan;
            if(!string.ISBLANK(tplan.Id) && (tplan != null)) {
                update updateDtls;
            }else if (tplan!= null){ 
                insert updateDtls;
            }
           //getSearchObjectDtls();
        }catch(Exception ex){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error,ex.getMessage());
            ApexPages.addMessage(msg); 
}

Query: Bulkify Apex Methods Using Collections In Methods ... I have no idea what's wrong here
thanks in advance
pconpcon
@Dinesh

The problem isn't specifically this method.  What is probably happening is that youare calling this method from inside a loop
 
for (sObject foo : fooList) {
​    mobiFormRecordDtlsUpdate(foo);
}

I also don' t know why you have this method (or at least the the if statement).  You can say
 
public static void mobiFormRecordDtlsUpdate(sObject tplan) {
    if (tplan == null) {
        return;
    }

    try {
        upsert tplan;
    } catch (Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage());
    }
}

With that being said, I would recommend that you change this to take in a List and upsert that list instead of taking in a single sObject.  However, without seeing the method / trigger that calls this (and any higher level methods / triggers) I cannot say why checkmarx is flagging that code.