+ Start a Discussion
GaneeeshGaneeesh 

Case merge Error............ in trigger and developer console...

Can anybody help me i am merging cases, those are not merging. If i am going to merg the case records it shows an error can anybody help to rectify this Error the Error is" Error: Compile Error: Specified type SOBJECT:Case cannot be merged at line 9 column 1 "

and this is my code...........................................

 

Case masterCase = [SELECT Id, casenumber,Accountid,Contactid,origin FROM Case WHERE Accountid = '0019000000jiDIX' AND Contactid = '0039000000iOKkH' AND origin = 'Phone' LIMIT 1]; System.debug('************master*****************'+mastercase); Case mergeCase = [SELECT Id, casenumber,Accountid,Contactid,origin FROM Case WHERE Accountid = '0019000000jiDIX' AND Contactid = '0039000000iOKkH' AND origin = 'Email' LIMIT 1]; System.debug('************merge*****************'+mergecase);

try {    

merge masterCase mergeCase;

}

catch (DmlException e) {    

// Process exception here

}

digamber.prasaddigamber.prasad

Hi,

 

Could you please validate if both the queries return a record or one of them is null?

 

Happy to help you!

GaneeeshGaneeesh

hi digambar i need Trigger and apex class for merge the Case object records. So can u Please send me that code please...?

GaneeeshGaneeesh

yes both are retrived a record..

Ashish_SFDCAshish_SFDC

Hi Ganesh, 

 

See the code for merging case records, 

 

 public void MergeCases(Case dupCase, Id masterCaseId) {
 
        // 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;
 
        // Duplicate all emails and attachments
        List<EmailMessage> EmailsToBeCreated = new List<EmailMessage>();
        for (EmailMessage em : [SELECT FromName, FromAddress, ToAddress, ccAddress, bccAddress, Subject, TextBody,
                                       HtmlBody, MessageDate, Status, Incoming, Headers, ActivityId, HasAttachment
                                FROM EmailMessage WHERE ParentId = :dupCase.Id AND IsDeleted=false]) {
 
            string mergeNotice = 'This email was recreated when it\'s parent case was merged. ';
            
            // if the email has an attachment, recreate all the attachments with the parentid set to the case id
            if (em.HasAttachment) {
                for (Attachment att : [SELECT Name, IsPrivate, Description, Body
                                       FROM Attachment WHERE ParentId = :em.Id AND IsDeleted=false]) {
                    AttachmentsToBeCreated.add(new Attachment(ParentId = masterCaseId,
                                                              Name = att.Name,
                                                              IsPrivate = att.IsPrivate,
                                                              Body = att.Body,
                                                              Description = 'This attachment was originally attached to a merged email titled ' +
                                                                            em.Subject + '.\r\r' +
                                                                            ((att.Description != null) ? att.Description : '')
                                                             ));
                }
                mergeNotice += 'The original email attachments are now attached to the parent case.';
            }
            // now that the attachments are handled, add the actual email
            EmailsToBeCreated.add(new EmailMessage(ParentId = masterCaseId,
                                                   FromName = em.FromName,
                                                   FromAddress = em.FromAddress,
                                                   ToAddress = em.toAddress,
                                                   ccAddress = em.ccAddress,
                                                   bccAddress = em.bccAddress,
                                                   Subject = em.subject,
                                                   TextBody = ((em.TextBody != null) ? mergeNotice + '\r\rOriginal body:\r\r' + em.TextBody : null),
                                                   HtmlBody = ((em.HtmlBody != null) ? mergeNotice + '
 
Original body:
 
' + em.HtmlBody : null),
                                                   MessageDate = em.MessageDate,
                                                   Status = em.Status,
                                                   Incoming = em.Incoming,
                                                   Headers = em.Headers,
                                                   ActivityId = em.ActivityId
                                                  ));
        }
        if (!EmailsToBeCreated.IsEmpty())
            insert EmailsToBeCreated;
 
        // 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;
    }

 http://blog.crmscience.com/2012/05/merging-cases-in-salesforce.html

 

Regards,

Ashish

 

 

Ashish_SFDCAshish_SFDC

Or Try this App from the App exchange, 

 

https://appexchange.salesforce.com/listingDetail?listingId=a0N30000005vPKWEA2

 

Regards,

Ashish