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
ShrinivasShrinivas 

Need to update check box field using trigger

Hi Team,

Trying to updating once of the check box field from Case.

Created one trigger like When Case got closed creating the new case with certain fields,Attachements and Email from the old case to new case.

Now i have created one check box field from case object i am tryint o updating the field = true when new case got created.

Please help me on this from where i need to add the conditon from my trigger .

Thanks in Advance 
Best Answer chosen by Shrinivas
Abhishek BansalAbhishek Bansal
Hi Albert,

I have updated your code to update the checkbox from old case. Please find the update code below: Code is eclosed between the comment tags - Code Added by Abhishek: Start/End
trigger CaseCloningFromOldtoNew on EmailMessage (after insert) {
    
    Set<Id> caseIds = new Set<Id>();   
    List<Case> caseList = new List<Case>();
    List<CaseComment> caseCommentList = new List<CaseComment>();
    List<Case> emailMessageToCases;
    Map<Id,List<Attachment>> caseToAttachmentMap = new Map<Id,List<Attachment>>();
    Map<Id,List<EmailMessage>> caseToEmailMessageMap = new Map<Id,List<EmailMessage>>();
    List<Attachment> newAttachments = new List<Attachment>();
    List<EmailMessage> newMessages = new List<EmailMessage>();
    Map<String,Case> mapOldCase = new Map<String,Case>();
    List<Case> oldCasesToUpdate = new List<Case>();
    Id StandardSupportRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Standard Support Closed').getRecordTypeId();
    Id FullfillmentSupportRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Fulfillment Support Closed').getRecordTypeId();
    
    for(EmailMessage emailMessageRecord : Trigger.new)
    {
        if(emailMessageRecord.ParentId!=null && String.valueOf(emailMessageRecord.ParentId).startsWith('500'))            
        {
            caseIds.add(emailMessageRecord.ParentId);
        }
    }      
    if(caseIds!=null && !caseIds.isEmpty()){
        System.debug('****************'+CaseIDs);
        emailMessageToCases = new List<Case>([Select Id,AccountId,BusinessHoursId,CaseNumber,ClosedDate,ContactEmail,ContactId,OldCaseId__c,Old_Case_Number__c,
                                              Description,IsClosed,Origin,Priority,RecordTypeId,New_Case_Created__c,
                                              Status,Subject,Business_Group__c,Business_Sub_Group__c,OwnerId, Owner.Name,Case_Description__c,
                                              Case_Type__c,CurrencyIsoCode from Case where Id IN : caseIds
                                              and IsClosed=true and (recordTypeId =: system.label.Case_Standard_Closed_Record_Type 
                                                                     or recordTypeId =: system.label.Case_Comm_Support_CCS_SW_Closed_Record_Type or 
                                                                     recordTypeId =: system.label.Case_Fullfillment_Support_Closed_Recordtype)]);
        
        System.debug('###############'+emailMessageToCases);        
        List<Attachment> attachments = new List<Attachment>([Select Id,Name,Body,ParentId,OwnerId from attachment where ParentId IN : caseIds]);
        List<EmailMessage> emailMessages = new List<EmailMessage>([Select Id,BccAddress,CcAddress,EmailTemplateId,FromAddress,
                                                                   FromName,Headers,HtmlBody,ParentId,Status,Subject,TextBody,
                                                                   ToAddress from EmailMessage where ParentId IN : caseIds]);
        
        if(attachments!=null && !attachments.isEmpty())
        {
            for(Attachment attachmentRecd : attachments)
            {
                if(caseToAttachmentMap.containsKey(attachmentRecd.ParentId))
                {
                    List<Attachment> attachmentRecords = caseToAttachmentMap.get(attachmentRecd.ParentId);
                    attachmentRecords.add(attachmentRecd);
                    caseToAttachmentMap.put(attachmentRecd.ParentId,attachmentRecords);
                }
                else
                    caseToAttachmentMap.put(attachmentRecd.ParentId,new list<Attachment>{attachmentRecd});
            }
        }        
        if(emailMessages!=null && !emailMessages.isEmpty())
        {
            for(EmailMessage emailMessageRecord:emailMessages)
            {
                if(caseToEmailMessageMap.containsKey(emailMessageRecord.ParentId))
                {
                    List<EmailMessage> emailMessageRecords = caseToEmailMessageMap.get(emailMessageRecord.ParentId);
                    emailMessageRecords.add(emailMessageRecord);
                    caseToEmailMessageMap.put(emailMessageRecord.ParentId,emailMessageRecords);
                }
                else
                    caseToEmailMessageMap.put(emailMessageRecord.ParentId,new list<EmailMessage>{emailMessageRecord});
            }
        }       
    }    
    if(emailMessageToCases!=null && !emailMessageToCases.isEmpty())
    {        
        for(Case caseRecord : emailMessageToCases)
        {
            System.debug('Recordtype' +caserecord.Recordtypeid);
            if(caseRecord.RecordTypeId == StandardSupportRecordTypeId)
            {
                Case openCase = caseRecord.clone(false);
                // openCase.recordTypeId = StandardSupportRecordTypeId;
                openCase.recordTypeId='012C0000000QV46';
                OpenCase.ownerid= caseRecord.Ownerid;           
                openCase.Status = 'New';
                openCase.OldCaseId__c = caseRecord.Id;
                openCase.Old_Case_Number__c = caseRecord.CaseNumber;
                openCase.Case_Description__c = caseRecord.Case_Description__c;
                caseList.add(openCase);                
            }
            else if(caseRecord.RecordTypeId == FullfillmentSupportRecordTypeId)
            {
                Case openCase = caseRecord.clone(false);
                // openCase.recordTypeId = FullfillmentSupportRecordTypeId;
                openCase.recordTypeId='012C0000000QV46';
                OpenCase.ownerid= caseRecord.Ownerid; 
                openCase.Status = 'New';
                openCase.OldCaseId__c = caseRecord.Id;
                openCase.Old_Case_Number__c = caseRecord.CaseNumber;
                openCase.Case_Description__c = caseRecord.Case_Description__c;
                OpenCase.New_Case_Created__c = True;
                Update CaseList;
                caseList.add(openCase);                
            }          

			//Code Added by Abhishek : Start
			caseRecord.Case_Update__c = true;
			//Code Added by Abhishek : End
        }
		//Code Added by Abhishek : Start
		update emailMessageToCases;
		//Code Added by Abhishek : End
    }   
	
			
    if(caseList!=null && !caseList.isEmpty()){
        system.debug('CaseList'+ caseList);        
        insert caseList; 
              
        for(Case caseOpenRecord : caseList)
        { 
            if(caseToAttachmentMap.containsKey(caseOpenRecord.OldCaseId__c))
            {
                for(Attachment caseAttachment : caseToAttachmentMap.get(caseOpenRecord.OldCaseId__c))
                {
                    Attachment newAttachment = caseAttachment.clone(false);
                    newAttachment.ParentId = caseOpenRecord.Id;
                    newAttachments.add(newAttachment);
                }
            }            
            if(caseToEmailMessageMap.containsKey(caseOpenRecord.OldCaseId__c))
            {
                for(EmailMessage emailMessageRecd : caseToEmailMessageMap.get(caseOpenRecord.OldCaseId__c))
                {
                    EmailMessage newEmailMessage = emailMessageRecd.clone(false);
                    newEmailMessage.ParentId = caseOpenRecord.Id;
                    newMessages.add(newEmailMessage);
                }
            }
        }
    }    
    if(newAttachments!=null && !newAttachments.isEmpty())
        insert newAttachments;    
    if(newMessages!=null && !newMessages.isEmpty())
        insert newMessages;  
}

Thanks,
Abhishek Bansal.

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Albert,

Greetings!

You can add the line where you are assigning the values for the New case fields from old case fields and before insert() method.

Please check the sample code in the below thread:

https://salesforce.stackexchange.com/questions/164666/trigger-to-create-a-new-case-whenever-there-is-a-new-email-tag-on-the-closed-ca

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Abhishek BansalAbhishek Bansal
Hi Albert,

I have updated your code to update the checkbox from old case. Please find the update code below: Code is eclosed between the comment tags - Code Added by Abhishek: Start/End
trigger CaseCloningFromOldtoNew on EmailMessage (after insert) {
    
    Set<Id> caseIds = new Set<Id>();   
    List<Case> caseList = new List<Case>();
    List<CaseComment> caseCommentList = new List<CaseComment>();
    List<Case> emailMessageToCases;
    Map<Id,List<Attachment>> caseToAttachmentMap = new Map<Id,List<Attachment>>();
    Map<Id,List<EmailMessage>> caseToEmailMessageMap = new Map<Id,List<EmailMessage>>();
    List<Attachment> newAttachments = new List<Attachment>();
    List<EmailMessage> newMessages = new List<EmailMessage>();
    Map<String,Case> mapOldCase = new Map<String,Case>();
    List<Case> oldCasesToUpdate = new List<Case>();
    Id StandardSupportRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Standard Support Closed').getRecordTypeId();
    Id FullfillmentSupportRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Fulfillment Support Closed').getRecordTypeId();
    
    for(EmailMessage emailMessageRecord : Trigger.new)
    {
        if(emailMessageRecord.ParentId!=null && String.valueOf(emailMessageRecord.ParentId).startsWith('500'))            
        {
            caseIds.add(emailMessageRecord.ParentId);
        }
    }      
    if(caseIds!=null && !caseIds.isEmpty()){
        System.debug('****************'+CaseIDs);
        emailMessageToCases = new List<Case>([Select Id,AccountId,BusinessHoursId,CaseNumber,ClosedDate,ContactEmail,ContactId,OldCaseId__c,Old_Case_Number__c,
                                              Description,IsClosed,Origin,Priority,RecordTypeId,New_Case_Created__c,
                                              Status,Subject,Business_Group__c,Business_Sub_Group__c,OwnerId, Owner.Name,Case_Description__c,
                                              Case_Type__c,CurrencyIsoCode from Case where Id IN : caseIds
                                              and IsClosed=true and (recordTypeId =: system.label.Case_Standard_Closed_Record_Type 
                                                                     or recordTypeId =: system.label.Case_Comm_Support_CCS_SW_Closed_Record_Type or 
                                                                     recordTypeId =: system.label.Case_Fullfillment_Support_Closed_Recordtype)]);
        
        System.debug('###############'+emailMessageToCases);        
        List<Attachment> attachments = new List<Attachment>([Select Id,Name,Body,ParentId,OwnerId from attachment where ParentId IN : caseIds]);
        List<EmailMessage> emailMessages = new List<EmailMessage>([Select Id,BccAddress,CcAddress,EmailTemplateId,FromAddress,
                                                                   FromName,Headers,HtmlBody,ParentId,Status,Subject,TextBody,
                                                                   ToAddress from EmailMessage where ParentId IN : caseIds]);
        
        if(attachments!=null && !attachments.isEmpty())
        {
            for(Attachment attachmentRecd : attachments)
            {
                if(caseToAttachmentMap.containsKey(attachmentRecd.ParentId))
                {
                    List<Attachment> attachmentRecords = caseToAttachmentMap.get(attachmentRecd.ParentId);
                    attachmentRecords.add(attachmentRecd);
                    caseToAttachmentMap.put(attachmentRecd.ParentId,attachmentRecords);
                }
                else
                    caseToAttachmentMap.put(attachmentRecd.ParentId,new list<Attachment>{attachmentRecd});
            }
        }        
        if(emailMessages!=null && !emailMessages.isEmpty())
        {
            for(EmailMessage emailMessageRecord:emailMessages)
            {
                if(caseToEmailMessageMap.containsKey(emailMessageRecord.ParentId))
                {
                    List<EmailMessage> emailMessageRecords = caseToEmailMessageMap.get(emailMessageRecord.ParentId);
                    emailMessageRecords.add(emailMessageRecord);
                    caseToEmailMessageMap.put(emailMessageRecord.ParentId,emailMessageRecords);
                }
                else
                    caseToEmailMessageMap.put(emailMessageRecord.ParentId,new list<EmailMessage>{emailMessageRecord});
            }
        }       
    }    
    if(emailMessageToCases!=null && !emailMessageToCases.isEmpty())
    {        
        for(Case caseRecord : emailMessageToCases)
        {
            System.debug('Recordtype' +caserecord.Recordtypeid);
            if(caseRecord.RecordTypeId == StandardSupportRecordTypeId)
            {
                Case openCase = caseRecord.clone(false);
                // openCase.recordTypeId = StandardSupportRecordTypeId;
                openCase.recordTypeId='012C0000000QV46';
                OpenCase.ownerid= caseRecord.Ownerid;           
                openCase.Status = 'New';
                openCase.OldCaseId__c = caseRecord.Id;
                openCase.Old_Case_Number__c = caseRecord.CaseNumber;
                openCase.Case_Description__c = caseRecord.Case_Description__c;
                caseList.add(openCase);                
            }
            else if(caseRecord.RecordTypeId == FullfillmentSupportRecordTypeId)
            {
                Case openCase = caseRecord.clone(false);
                // openCase.recordTypeId = FullfillmentSupportRecordTypeId;
                openCase.recordTypeId='012C0000000QV46';
                OpenCase.ownerid= caseRecord.Ownerid; 
                openCase.Status = 'New';
                openCase.OldCaseId__c = caseRecord.Id;
                openCase.Old_Case_Number__c = caseRecord.CaseNumber;
                openCase.Case_Description__c = caseRecord.Case_Description__c;
                OpenCase.New_Case_Created__c = True;
                Update CaseList;
                caseList.add(openCase);                
            }          

			//Code Added by Abhishek : Start
			caseRecord.Case_Update__c = true;
			//Code Added by Abhishek : End
        }
		//Code Added by Abhishek : Start
		update emailMessageToCases;
		//Code Added by Abhishek : End
    }   
	
			
    if(caseList!=null && !caseList.isEmpty()){
        system.debug('CaseList'+ caseList);        
        insert caseList; 
              
        for(Case caseOpenRecord : caseList)
        { 
            if(caseToAttachmentMap.containsKey(caseOpenRecord.OldCaseId__c))
            {
                for(Attachment caseAttachment : caseToAttachmentMap.get(caseOpenRecord.OldCaseId__c))
                {
                    Attachment newAttachment = caseAttachment.clone(false);
                    newAttachment.ParentId = caseOpenRecord.Id;
                    newAttachments.add(newAttachment);
                }
            }            
            if(caseToEmailMessageMap.containsKey(caseOpenRecord.OldCaseId__c))
            {
                for(EmailMessage emailMessageRecd : caseToEmailMessageMap.get(caseOpenRecord.OldCaseId__c))
                {
                    EmailMessage newEmailMessage = emailMessageRecd.clone(false);
                    newEmailMessage.ParentId = caseOpenRecord.Id;
                    newMessages.add(newEmailMessage);
                }
            }
        }
    }    
    if(newAttachments!=null && !newAttachments.isEmpty())
        insert newAttachments;    
    if(newMessages!=null && !newMessages.isEmpty())
        insert newMessages;  
}

Thanks,
Abhishek Bansal.
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Albert,

You can contact me on:
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790

Thanks,
Abhishek Bansal.
Abhishek BansalAbhishek Bansal
Please contact me on my gmail or skype. We can figure this out. Its already late in India so I am available for next 20 mins. Otherwise we can connect tomorrow.