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
Ian Wright 3Ian Wright 3 

Create a case comment when another object field is not Null

Hi,

I'm new to Apex and am looking to create an auto-generated approval note on the case attached to the custom object Event_Resolution__c.  Below is what I have and I keep getting line 5, loop variable must be of Type Question.  I assume it means it thinks Case is not defined or related to the event resolution.  The case is entered in a lookup field case__c but it says that variable doesn't exist.  

Any help on how to make this possible?  Basically if anyone on the event resolutions team approves a credit or point value, I want a case comment to automatically populate on the case.
User-added image
Best Answer chosen by Ian Wright 3
Ravi Dutt SharmaRavi Dutt Sharma
Hi Ian,

I have updated the code to check condition for Question record type. I am hoping that Question record type is present on Event Resolution object. Let me know if it is otherwise. Thanks.

New utility class that you need to create first:
 
public class ObjectUtil {

    public static String getObjectRecordTypeId(SObjectType sObjectType, String recordTypeName){
        //Generate a map of tokens for all the Record Types for the desired object
        Map recordTypeInfo = sObjectType.getDescribe().getRecordTypeInfosByName();

        if(!recordTypeInfo.containsKey(recordTypeName))
            throw new RecordTypeException('Record type "'+ recordTypeName +'" does not exist.');

        //Retrieve the record type id by name
        return recordTypeInfo.get(recordTypeName).getRecordTypeId();
    }

    public class RecordTypeException extends Exception{}
}

Updated trigger code that makes use of this utility class:
 
trigger AutoApprovals on Event_Resolution__c (after insert){

    List<CaseComment> newComment = new List<CaseComment>();
    
	// Get record type id for record type named Question present on Event_Resolution__c object
	String questionRecordTypeId = ObjectUtil.getObjectRecordTypeId(Event_Resolution__c, Question);
	
    for(Event_Resolution__c eventRes: Trigger.new){
		// Added additional check for record type
        if(eventRes.Credits_Approved__c != null && eventRes.RecordTypeId.equals(questionRecordTypeId)){
            CaseComment caseComm = new CaseComment();
            caseComm.ParentId = eventRes.Case__c;
            caseComm.CommentBody = 'QRF agent approved ' + '$' + eventRes.Credits_Approved__c;
            newComment.add(caseComm);
        }
		/ Added additional check for record type
        if(eventRes.Points_Approved__c != null && eventRes.RecordTypeId.equals(questionRecordTypeId)){
            CaseComment caseComm = new CaseComment();
            caseComm.ParentId = eventRes.Case__c;
            caseComm.CommentBody = 'QRF agent approved ' + eventRes.Points_Approved__c + 'Point/s';
            newComment.add(caseComm);
    }
    
    Database.insert(newComment, false);
	}
}

 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi Ian,

Your trigger should be on below lines. I have given the sample code, you can extend it further easily. Let me know if you face any issues. Thanks.
 
trigger AutoApprovals on Event_Resolution__c (after insert){

	List<CaseComment> newComment = new List<CaseComment>();
	
	for(Event_Resolution__c eventRes: Trigger.new){
		if(eventRes.Credits_Approved__c != null){
			CaseComment caseComm = new CaseComment();
			caseComm.ParentId = eventRes.Case__c;
			newComment.add(caseComm);
		}
	}
	
	Database.insert(newComment, false);
}

 
Ian Wright 3Ian Wright 3
Hey Ravi,

This helps a bunch thank you.  After extending the code to include everything I needed I now get the error that "Compile Error: Invalid type: Event_Resolution__c.RecordType.Question at line 5 column 9" where if I just leave it as Event_Resolution__c it says that the loop must be of type Question.  

I need to make sure the trigger is specific to the record type Question and not just the overall object Event Resolution.  Any idea what I'm missing?  I can probably just have it be for all records on the object but would prefer it be specific to the record type for more accurate usage by employees.  
 
trigger AutoApprovals on Event_Resolution__c (after insert){

    List<CaseComment> newComment = new List<CaseComment>();
    
    for(Event_Resolution__c eventRes: Trigger.new){
        if(eventRes.Credits_Approved__c != null){
            CaseComment caseComm = new CaseComment();
            caseComm.ParentId = eventRes.Case__c;
            caseComm.CommentBody = 'QRF agent approved ' + '$' + eventRes.Credits_Approved__c;
            newComment.add(caseComm);
        }
        if(eventRes.Points_Approved__c != null){
            CaseComment caseComm = new CaseComment();
            caseComm.ParentId = eventRes.Case__c;
            caseComm.CommentBody = 'QRF agent approved ' + eventRes.Points_Approved__c + 'Point/s';
            newComment.add(caseComm);
    }
    
    Database.insert(newComment, false);
}
}

 
Ravi Dutt SharmaRavi Dutt Sharma
Hi Ian,

I have updated the code to check condition for Question record type. I am hoping that Question record type is present on Event Resolution object. Let me know if it is otherwise. Thanks.

New utility class that you need to create first:
 
public class ObjectUtil {

    public static String getObjectRecordTypeId(SObjectType sObjectType, String recordTypeName){
        //Generate a map of tokens for all the Record Types for the desired object
        Map recordTypeInfo = sObjectType.getDescribe().getRecordTypeInfosByName();

        if(!recordTypeInfo.containsKey(recordTypeName))
            throw new RecordTypeException('Record type "'+ recordTypeName +'" does not exist.');

        //Retrieve the record type id by name
        return recordTypeInfo.get(recordTypeName).getRecordTypeId();
    }

    public class RecordTypeException extends Exception{}
}

Updated trigger code that makes use of this utility class:
 
trigger AutoApprovals on Event_Resolution__c (after insert){

    List<CaseComment> newComment = new List<CaseComment>();
    
	// Get record type id for record type named Question present on Event_Resolution__c object
	String questionRecordTypeId = ObjectUtil.getObjectRecordTypeId(Event_Resolution__c, Question);
	
    for(Event_Resolution__c eventRes: Trigger.new){
		// Added additional check for record type
        if(eventRes.Credits_Approved__c != null && eventRes.RecordTypeId.equals(questionRecordTypeId)){
            CaseComment caseComm = new CaseComment();
            caseComm.ParentId = eventRes.Case__c;
            caseComm.CommentBody = 'QRF agent approved ' + '$' + eventRes.Credits_Approved__c;
            newComment.add(caseComm);
        }
		/ Added additional check for record type
        if(eventRes.Points_Approved__c != null && eventRes.RecordTypeId.equals(questionRecordTypeId)){
            CaseComment caseComm = new CaseComment();
            caseComm.ParentId = eventRes.Case__c;
            caseComm.CommentBody = 'QRF agent approved ' + eventRes.Points_Approved__c + 'Point/s';
            newComment.add(caseComm);
    }
    
    Database.insert(newComment, false);
	}
}

 
This was selected as the best answer
Ian Wright 3Ian Wright 3
Thank you so much Ravi!  This helps a bunch.  I haven't finished researching Apex Classes so I'll have to learn more about those.