2

Overview - i recently was assigned to take over a project which was started by an outside consultant. I am unfamiliar with RestResources which is what this project was built on. MY ISSUE:I need to get this deployed but want to ensure i am not missing any components which are necessary for it to function. Currently i pretty much only know i need this class and the custom setting created.

This project is essentially doing an auto-deduplication function on cases created via inbound emails within a certain time frame based on having the same subject line (including the RE:'s from replies within that time frame)

The code i modified was in this class (posted below) and i was able to get the desired result, i just am unsure what else is triggering the code to run when the record is viewed.

@RestResource(urlMapping='/find_duplicate_cases')
global without sharing class akaFindDuplicateCasesREST{
    public static final String DUPLICATE_STATUS = 'Closed/Duplicate';
    public static final String RESOLUTION_NOTES = 'Closed Automatically - system identified this case as a duplicate; refer to Parent Case';



    @HttpPost
    global static void doPost(String recordId) {
        if (isCaseId(recordId)) {
            Id caseId = Id.valueOf(recordId);
            List<Case> originalCase = [
                    SELECT Id, 
                            MS_Current_Department__c, 
                            MS_Source_Queue__c,
                            Subject,
                            CreatedDate
                    FROM Case
                    WHERE Id = :caseId
                    AND MS_Source_Queue__c != NULL
                    AND Subject != NULL
            ];
            //add if custom setting checkbox = false or program != best buy or red wing, do nothing
            String parsedSubject = '%' + originalCase[0].Subject.toLowerCase().replace('re: ', '').replace('fwd: ', '').replace('fw: ', '');
            if (originalCase.isEmpty() == false && originalCase[0].MS_Current_Department__c != 'Financial Controls') {
                Datetime startingTime = originalCase[0].CreatedDate.addMinutes(-1 * akaDuplicateCaseHandlerSettingMgr.MINUTES_BEFORE_CREATEDDATE);
                Datetime endingTime = originalCase[0].CreatedDate.addMinutes(akaDuplicateCaseHandlerSettingMgr.MINUTES_AFTER_CREATEDDATE);


                List<Case> duplicateCases = [
                        SELECT Id
                        FROM Case
                        WHERE CreatedDate >= :startingTime
                        AND CreatedDate <= :endingTime
                        AND Subject like :parsedSubject
                        //AND MS_Source_Queue__c != :originalCase[0].MS_Source_Queue__c
                        AND Id!=:originalCase[0].Id
                        AND Status = 'New' order by CreatedDate desc
                ];

                set<Id> duplicateCasesIdSet = new set<Id>();
                for(Case case0 : duplicateCases) {
                    duplicateCasesIdSet.add(case0.id);
                }

                if(duplicateCases.isEmpty() == false){
                    List<Account> duplicateCaseAccounts = [SELECT Id FROM Account WHERE Id=:akaDuplicateCaseHandlerSettingMgr.DUPLICATE_CASE_ACCOUNT_ID];
                    List<Contact> caseOriginatorContacts = [SELECT Id FROM Contact WHERE Id=:akaDuplicateCaseHandlerSettingMgr.CASE_ORIGINATOR_CONTACT_ID];
                    Account dupCaseAcct = duplicateCaseAccounts.isEmpty() == false ? duplicateCaseAccounts[0] : null;
                    Contact caseOriginatorCont = caseOriginatorContacts.isEmpty() == false ? caseOriginatorContacts[0] : null;

                    updateDuplicateCases(originalCase[0], duplicateCases, dupCaseAcct, caseOriginatorCont);
                }
                list<EmailMessage> emailMessagesToInsert = new list<EmailMessage>();
                set<Id> emailMessageIdSet = new set<Id>();
                boolean findAttachments = false;
                for(EmailMessage em : [select id, Subject, FromAddress, ToAddress, HtmlBody, TextBody, HasAttachment from EmailMessage where ParentId IN: duplicateCasesIdSet]) {
                    emailMessageIdSet.add(em.id);
                    if(em.HasAttachment) {
                        system.debug('Here findAttachments = true');
                        findAttachments = true;
                        //set boolean to query and clone attachments/reparent
                    }
                    EmailMessage em0 = new EmailMessage();
                    em0.ParentId = duplicateCases[0].id;
                    em0.Subject = em.Subject;
                    em0.HtmlBody = em.HtmlBody;
                    em0.TextBody = em.TextBody;
                    em0.FromAddress = em.FromAddress;
                    em0.ToAddress = em.ToAddress;
                    emailMessagesToInsert.add(em0);
                }
                if(emailMessagesToInsert.isempty() == false) {
                    insert emailMessagestoInsert;
                }


                List<Attachment> attachmentList = new list<Attachment>();

                if(findAttachments) {
                    system.debug('Here2 - findAttachments = true');
                    for(Attachment attach : [SELECT Id, body, ContentType, Name FROM Attachment WHERE parentId IN: emailMessageIdSet]) {
                        Attachment newAttach = attach.clone();
                        newAttach.ParentId = originalCase[0].id;
                        attachmentList.add(newAttach);
                    }

                }
                if(attachmentList.isEmpty() == false) {
                   insert attachmentList; 
                }
            }
        }
    }


    @TestVisible
    private static void updateDuplicateCases(Case originalCase, List<Case> duplicateCases, Account duplicateCaseAccount, Contact caseOriginatorContact){
        if(originalCase != null && originalCase.Id != null && duplicateCases != null && duplicateCases.isEmpty() == false) {
            for (Case duplicateCase : duplicateCases) {
                duplicateCase.ParentId = originalCase.Id;
                duplicateCase.Status = DUPLICATE_STATUS;
                duplicateCase.MS_Resolution_Notes__c = RESOLUTION_NOTES;
                if(duplicateCaseAccount != null){
                    duplicateCase.AccountId = duplicateCaseAccount.Id;
                }
                if(caseOriginatorContact != null){
                    duplicateCase.ContactId = caseOriginatorContact.Id;
                }
            }

            Database.update(duplicateCases);

        }


    }


    @TestVisible
    private static Boolean isCaseId(String strId) {
        Boolean valToReturn = false;
        try {
            Id parsedId = Id.valueOf(strId);
            valToReturn = parsedId != null && parsedId.getSobjectType() == Case.getSObjectType();
        } catch (Exception e) {
            valToReturn = false;
        }


        return valToReturn;
    }




}
  • Hi - can you try to limit the code sample a bit, or speak out more of your concerns? The community will be able to answer you better – Brian Miller May 5 '19 at 20:08
  • Code does not run when a record is viewed inside a standard page. We’re missing valuable context here. – Sebastian Kessel May 5 '19 at 20:51
  • Realistically, there's no real way to find out without checking documentation (hopefully your organization has some?!). It could be called from a Visualforce page, a button, some other website, etc. – sfdcfox May 5 '19 at 23:56
  • Unfortunately there is no documentation provided from the consultant on this new feature. I have been looking through the audit trail and i have seen a couple other classes that have been modified/created around the same time. I can add those above in my question but it will be pretty lengthy. – Logie C May 6 '19 at 13:43
  • Update - i believe it is being triggered by an email service that i just found. I am unfamiliar with these so i will begin additional research now. – Logie C May 6 '19 at 13:58
0

it was being called via an 'invisible' lightning component which would trigger the code whenever someone first opens and views a case. I only found the component by hovering over the entire page in the lightning app builder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.