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
case commentscase comments 

Apex code to create related cases

I have a trigger that creates related cases whenever a case is inserted that meets a certain criteria.   On my contact record, I have a lookup field to a User record.  Every internal User has a Contact record.  When the the related cases are created, the Contact record of the User who triggered the code is inserted automatically into the contact field of the related cases. 

trigger AfterCaseTrigger on Case (after insert, after update) {
    Map<String,Case>newObjLinkMap = new Map<String,Case>();
    Set<ID> scID = new Set<ID>();    
    List<Days_in_Status__c>DaysStatus = new List<Days_in_Status__c>();
    Days_in_Status__c dis = new Days_in_Status__c();
    Set<ID> CIds = new Set <ID>();
    List<Case> relatedCases = new List<Case>();
    sObject s = [SELECT ID FROM CONTACT WHERE user__c =: Userinfo.getUserId()];
    ID myid = s.id;

 I seem to be getting errored out on this line of code when a customer portal user creates a case or when a case is created from a web form. 

sObject s = [SELECT ID FROM CONTACT WHERE user__c =: Userinfo.getUserId()];
    ID myid = s.id;

 The rest of my code fires correctly if it is an internal user creating the case.

for(Case caseObj : Trigger.new){
if(Trigger.isInsert){
 if((caseObj.Type == 'Drupal Site Launch' || caseObj.Type == 'Classic Site Launch') && caseObj.ParentId == NULL){
                Case JDLaunchCase = new Case();
                JDLaunchCase.Subject = caseObj.Subject + ' JD Launch Work';
                JDLaunchCase.RecordTypeId = '012A000000177IL';
                JDLaunchCase.ParentId = caseObj.ID;
                JDLaunchCase.Type = caseObj.Type;
                JDLaunchCase.Request_Type__c = 'Request New Development';
                JDLaunchCase.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
                JDLaunchCase.ContactId = myid;
                JDLaunchCase.OwnerId = Userinfo.getUserId();
                relatedCases.add(JDLaunchCase);
                Case ACLaunchCase = new Case();
                ACLaunchCase.Subject = caseObj.Subject + ' AC Launch Work';
                ACLaunchCase.RecordTypeId = '012A000000177IL';
                ACLaunchCase.ParentId = caseObj.Id;
                ACLaunchCase.Type = caseObj.Type;
                ACLaunchCase.Request_Type__c = 'Request New Development';
                ACLaunchCase.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
                ACLaunchCase.ContactId = myid;
                ACLaunchCase.OwnerId = Userinfo.getUserId();
                relatedcases.add( ACLaunchCase);
                Case Content = new Case();
                Content.Subject = caseObj.Subject + ' Content processing for launch';
                Content.RecordTypeId = '012A000000177IL';
                Content.ParentId = caseObj.Id;
                Content.Type = caseObj.Type;
                Content.Request_Type__c = 'Request New Development';
                Content.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
                Content.ContactId = myid;
                Content.OwnerId = Userinfo.getUserId();
                relatedCases.add(Content);
                Case LaunchQA = new Case();
                LaunchQA.Subject = caseObj.Subject + ' Launch QA';
                LaunchQA.RecordTypeId = '012A000000177IL';
                LaunchQA.ParentId = caseObj.Id;
                LaunchQA.Type = caseObj.Type;
                LaunchQA.Request_Type__c = 'Request New Development';
                LaunchQA.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
                LaunchQA.ContactId = myid;
                LaunchQA.OwnerId = Userinfo.getUserId();
                relatedCases.add(LaunchQA);
                Case MobileSupport = new Case();
                MobileSupport.Subject = caseObj.Subject + ' Mobile Support for Launch';
                MobileSupport.RecordTypeId = '012A000000177IL';
                MobileSupport.ParentId = caseObj.Id;
                MobileSupport.Type = caseObj.Type;
                MobileSupport.Request_Type__c = 'Request New Development';
                MobileSupport.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
                MobileSupport.ContactId = myid;
                MobileSupport.OwnerId = Userinfo.getUserId();
                relatedCases.add(MobileSupport);                
                
            }
            
            
        }

 Can some one point me in the right direction as to how to structure this logic better?  Thanks

digamber.prasaddigamber.prasad

Hi,

 

Userinfo.getUserId() doesn't return any value in case of web form. Assuming in such case you don't want to create related case. I have modified code below:-

 

trigger AfterCaseTrigger on Case (after insert, after update) {
    Map<String,Case>newObjLinkMap = new Map<String,Case>();
    Set<ID> scID = new Set<ID>();    
    List<Days_in_Status__c>DaysStatus = new List<Days_in_Status__c>();
    Days_in_Status__c dis = new Days_in_Status__c();
    Set<ID> CIds = new Set <ID>();
    List<Case> relatedCases = new List<Case>();
    List<User> s = [SELECT ID FROM CONTACT WHERE UserName =: Userinfo.getUserId()];
    
	if(s.size() > 0){
		ID myid = s[0].id;
		
		for(Case caseObj : Trigger.new){
			if(Trigger.isInsert){
				if((caseObj.Type == 'Drupal Site Launch' || caseObj.Type == 'Classic Site Launch') && caseObj.ParentId == NULL){
					Case JDLaunchCase = new Case();
					JDLaunchCase.Subject = caseObj.Subject + ' JD Launch Work';
					JDLaunchCase.RecordTypeId = '012A000000177IL';
					JDLaunchCase.ParentId = caseObj.ID;
					JDLaunchCase.Type = caseObj.Type;
					JDLaunchCase.Request_Type__c = 'Request New Development';
					JDLaunchCase.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
					JDLaunchCase.ContactId = myid;
					JDLaunchCase.OwnerId = Userinfo.getUserId();
					relatedCases.add(JDLaunchCase);
					Case ACLaunchCase = new Case();
					ACLaunchCase.Subject = caseObj.Subject + ' AC Launch Work';
					ACLaunchCase.RecordTypeId = '012A000000177IL';
					ACLaunchCase.ParentId = caseObj.Id;
					ACLaunchCase.Type = caseObj.Type;
					ACLaunchCase.Request_Type__c = 'Request New Development';
					ACLaunchCase.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
					ACLaunchCase.ContactId = myid;
					ACLaunchCase.OwnerId = Userinfo.getUserId();
					relatedcases.add( ACLaunchCase);
					Case Content = new Case();
					Content.Subject = caseObj.Subject + ' Content processing for launch';
					Content.RecordTypeId = '012A000000177IL';
					Content.ParentId = caseObj.Id;
					Content.Type = caseObj.Type;
					Content.Request_Type__c = 'Request New Development';
					Content.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
					Content.ContactId = myid;
					Content.OwnerId = Userinfo.getUserId();
					relatedCases.add(Content);
					Case LaunchQA = new Case();
					LaunchQA.Subject = caseObj.Subject + ' Launch QA';
					LaunchQA.RecordTypeId = '012A000000177IL';
					LaunchQA.ParentId = caseObj.Id;
					LaunchQA.Type = caseObj.Type;
					LaunchQA.Request_Type__c = 'Request New Development';
					LaunchQA.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
					LaunchQA.ContactId = myid;
					LaunchQA.OwnerId = Userinfo.getUserId();
					relatedCases.add(LaunchQA);
					Case MobileSupport = new Case();
					MobileSupport.Subject = caseObj.Subject + ' Mobile Support for Launch';
					MobileSupport.RecordTypeId = '012A000000177IL';
					MobileSupport.ParentId = caseObj.Id;
					MobileSupport.Type = caseObj.Type;
					MobileSupport.Request_Type__c = 'Request New Development';
					MobileSupport.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
					MobileSupport.ContactId = myid;
					MobileSupport.OwnerId = Userinfo.getUserId();
					relatedCases.add(MobileSupport);                

				}


			}
		}
	}
	
	if(relatedCases.size() > 0)
		insert relatedCases;
}

 Let me know if you have any question.