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

how can i call this class from Apex trigger..?

i have written a class for Case merge functionality , But the problem is when ever i call this class from trigger it shows error .... can anybody help me how can i call this class from trigger....? and this this is my class please send me how can i call this class from trigger..


public with sharing class casemerge{

public void mergecases(Case dupcase,Id mastercaseid){

System.debug('*************dupcase records**************'+dupcase);

//--------Duplicate all attachments

 List<Attachment> AttachmentsToBeCreated = new List<Attachment>();  

for (Attachment a : [SELECT Name, IsPrivate, Description, Body FROM Attachment WHERE ParentId = :dupCase.Id AND IsDeleted=false]){

 AttachmentsToBeCreated.add(new Attachment(ParentId = masterCaseId,Name = a.Name,IsPrivate = a.IsPrivate,Description = a.Description,Body = a.Body));        


//-------Duplicate all comments

 List<CaseComment> CommentsToBeCreated = new List<CaseComment>();

 for (CaseComment cc : [SELECT IsPublished, CreatedDate, CommentBody FROM CaseComment WHERE ParentId = :dupCase.Id AND IsDeleted=false]) {

 CommentsToBeCreated.add(new CaseComment(ParentId = masterCaseId,IsPublished = cc.IsPublished,CommentBody = 'This case comment was originally created on ' + cc.CreatedDate +' and was merged into this case.\r\r' + cc.CommentBody));        


 if (!CommentsToBeCreated.IsEmpty())

 insert CommentsToBeCreated;

//-------Change the whatid for all tasks

List<Task> allTasks = [SELECT Id, WhatId FROM Task WHERE WhatId = :dupCase.Id AND IsDeleted=false];

if (!allTasks.isEmpty()) {    

for (integer i = 0; i < allTasks.size(); i++)    

allTasks[i].WhatId = masterCaseId;    

update allTasks;        


//-----Change the whatid for all Events

List<Event> allEvents = [SELECT Id, WhatId FROM Event WHERE WhatId = :dupCase.Id AND IsDeleted=false];

if (!allEvents.isEmpty()) {

for (integer i = 0; i < allEvents.size(); i++)

allEvents[i].WhatId = masterCaseId;            

update allEvents;        


// duplicate all the attachments (of the case and related emails)

if (!AttachmentsToBeCreated.IsEmpty())

insert AttachmentsToBeCreated;  

// delete the original case and all it's related items

delete dupCase;






What is the error that you are getting? Create an instance of that class and invoke it.


the error is "System.SObjectException: DML statment cannot operate on or trigger.old: Class.casemerge.mergecases: line 38, column 1"


And my trigger is ...................


trigger trgoncase2 on Case (after insert) {
System.debug('************this is new case****************'+cs);
list<Case> lstcs=[select id,casenumber from Case where accountid =: cs.accountid AND contactid =: cs.contactid AND casenumber !=: cs.casenumber];
for(Case c:lstcs){
System.debug('**********existing case*************'+c);
Casemerge cm=new Casemerge();


how can i call that class from this trigger can you please tell me...........




Apex does not allow you to perform DML on the records being triggered through the trigger itself. You can use @future method to solve this. Once your trigger is complete, this class will be called


how can i use @future can you please show me once..? is this mention boefore the method rignt..?


If i use @future annotation it show an error at a method parameter  and this is my method "public static void mergerecords(Case dupcase,id masterrecordid)"   in this method it shows error like "un supported parameter type sobject: case".  i got this error how can i rectify this error.....? can u please explain me.......




Find  details about future annotation here


then how can i calll that method from trigger ........... please tell me...

Satyendra RawatSatyendra Rawat




it is also throws this type of  Error "Error: Compile Error: Unsupported parameter type LIST<Case> at line 3 column 20"