You need to sign in to do that
Don't have an account?
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);
}
}
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);
}
}
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.
Best regards
Mouhamed
Regards
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