+ Start a Discussion
SFDC GuestSFDC Guest 

Bulkify code to perform dml operation on multple records

Hi All.

Below class is used to create feeditem when application log is created/ updated.It's working fine when I am inserting/updating single record but it's creating only one record when multiple multiple records are created. Please let me know how to make this class to support multiple records. Thanks.

public class ApplicationLogTriggerHandler{
    public void LogHandlerMethod(List<Application_Log__c> appLogs){
        List<SObject> ObjList = new List<SObject>();
        String ObjType ='';
        String ObjID='';
        String Description = '';
        List<String> ObjTypeList = new List<String>();
        String LinkToRecord = '';
        List<Application_Log__c> LogList = new List<Application_Log__c>([Select ID, ObjectType__c, Name, Link_To_Record__c, 
                                                                         Record_Id__c, Description__c from 
                                                                         Application_Log__c where ID IN : appLogs ]);
        System.debug('LogList--->' +LogList);
        for(Application_Log__c app : LogList){
            if(app.Id!=null){
                ObjType = app.ObjectType__c;
                ObjID = app.Record_Id__c;
                Description = app.Description__c;
                LinkToRecord  = app.Link_To_Record__c;
                ObjTypeList.add(ObjType);
            }
        }
        system.debug('---->ObjTypeList'+ObjTypeList);
        System.debug('LogList--->' +LogList);
        String Query='Select Id, Name, LastModifiedById from '+ ObjType + ' WHERE ID =:ObjID';
        ObjList =database.query(Query); 
        System.debug('ObjList'+ ObjList);
        List<FeedItem> feedList = new List<FeedItem>();
            for(SObject record : ObjList){
            record.get('LastModifiedById');
            record.get('Name');
            System.debug((String) record.get('LastModifiedById'));
            FeedItem post = new FeedItem();
            post.ParentId = String.valueOf(record.get('LastModifiedById')); //eg. Opportunity id, custom object id..
            post.Body = 'An error has been logged for the opportunity: '+ record.get('Name') +  ' with the details: ' + Description + '\n' + 'Record ID: ' + LinkToRecord  ;
            feedList.add(post);
        }
        Database.insert(feedList, false);
        System.debug('feedList ------->' +feedList);
    }
}
Mouhamed N'DIONGUEMouhamed N'DIONGUE
Hi SFDC Guest,
I think your problem here is coming from the fact that you're just querying 1 record on your objType Query (String Query).
Try this sample. Id didn't test it as I didn't want to create your data model, but tell me if it doesn't work. I'll implement it on my side.
public class ApplicationLogTriggerHandler{
    public void LogHandlerMethod(List<Application_Log__c> appLogs){
        List<SObject> ObjList = new List<SObject>();
        String ObjType ='';
        String ObjID='';
        String Description = '';
        List<String> ObjTypeList = new List<String>();
        String LinkToRecord = '';
        List<Application_Log__c> LogList = new List<Application_Log__c>([Select ID, ObjectType__c, Name, Link_To_Record__c, 
                                                                         Record_Id__c, Description__c from 
                                                                         Application_Log__c where ID IN : appLogs ]);
        System.debug('LogList--->' +LogList);
        //List of objIds
        String[] objIds = new String[]{};
        for(Application_Log__c app : LogList){
            if(app.Id!=null){
                ObjType = app.ObjectType__c;
                ObjID = app.Record_Id__c;
                Description = app.Description__c;
                LinkToRecord  = app.Link_To_Record__c;
                ObjTypeList.add(ObjType);
                objIds.add(ObjID);
            }
        }
        system.debug('---->ObjTypeList'+ObjTypeList);
        System.debug('LogList--->' +LogList);
        String Query='Select Id, Name, LastModifiedById from '+ ObjType + ' WHERE ID IN :objIds';//query on the list and not only on one record
        ObjList =database.query(Query); 
        System.debug('ObjList'+ ObjList);
        List<FeedItem> feedList = new List<FeedItem>();
            for(SObject record : ObjList){
            record.get('LastModifiedById');
            record.get('Name');
            System.debug((String) record.get('LastModifiedById'));
            FeedItem post = new FeedItem();
            post.ParentId = String.valueOf(record.get('LastModifiedById')); //eg. Opportunity id, custom object id..
            post.Body = 'An error has been logged for the opportunity: '+ record.get('Name') +  ' with the details: ' + Description + '\n' + 'Record ID: ' + LinkToRecord  ;
            feedList.add(post);
        }
        Database.insert(feedList, false);
        System.debug('feedList ------->' +feedList);
    }
}

Best regards

Mouhamed​
SFDC GuestSFDC Guest
Hi Mouhamed, it's not working for bulk records.
Mouhamed N'DIONGUEMouhamed N'DIONGUE
SFDC Guest, I'll check on my side by implementing your data model and will make the test. Can you just provide me some records of your Application_Log__c records (just a picture of your csv records). I'll keep you updated.
Regards
SFDC GuestSFDC Guest
Hi Mouhamed, here is the data.


Application Log Name
Test 1
Test 2
Test 3


Object Type
Account 
Contact
Opportunity


Record Id
Account record Id - 15 digit/18 digit
Contact record Id - 15 digit/18 digit
Opportunity record Id - 15 digit/18 digit