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
karan pkaran p 

Could you please any one give a solution for my below query

Hello all

I have "contaminats" Object as a related list for Cases and the field in the objects were as follows (Name, Feed(CheckBox), Regeneration medium(CheckBox)). i need to Auto create 6 contaminats (oxygen,CO,H2,Olefins,Sulfur  components,Chloride components) upon case creation as unchecked
Can any one help me with a class to create these contaminats .
Prateek Singh SengarPrateek Singh Sengar
Hi Karan,
Why do you want to create a apex class or trigger for this. This use case can be easily achieved by using process builders. Please follow the below tutorial in helping you to use process builder for creating a child record.

https://automationchampion.com/2015/02/13/getting-started-with-process-builder-part-1-auto-create-a-record/

Hope this helps.
Ajinkya1225Ajinkya1225
Hi Karan,

There two was through which you can achieve this requirement. Either write a custom Save Button on Case and create records there or  you need to create a trigger on Case, which would fire when a record in inserted and would generate 6 records on a related object is Contaminants__c.

I have written a sample code for you. Don't forget to check the api names before saving your code!

Follow these steps:

Step 1: Go to Setup and in the Quick Find Box, type Classes.
Step 2: Select Apex Classes
Step 3: Paste the following class in it.
 
public class CaseTriggerHelper{
	// Static map to store your Contaminant Name
	public static map<integer,string> mapIntToContaminant = new map<integer,string>{0=>'oxygen',1=>'CO',2=>'H2',3=>'Olefins',4=>'Sulfur  components',5=>'Chloride components'};
	
	public void createContaminats(List <Case> cases){	
		
		// We need to store a List of contaminants to create.
		List<contaminants__c> contaminantsToCreate = new List<contaminants__c>();

        // Loop over the cases. Remember, we don't know how many cases we will have.
        for(Case eachCase : cases){
			for(integer i =0; i <6; i++){	
				contaminants__c newContaminant = new contaminants__c();
				contaminants__c.Name = mapIntToContaminant.get(i);
				contaminants__c.Feed__c = False;
				contaminants__c.Regeneration_medium__c = False;
				contaminantsToCreate.add(newContaminant);
			}
        }
		// Insert your List
	    insert contaminantsToCreate;
	}
}

Step 4: Go to setup and in the Quick Find Box, type Case.
Step 5: Select Case Triggers.
Step 6: Paste the following Trigger in it.
trigger CaseTrigger on Case (after insert) {
    // Create an instance of your helper class
   CaseTriggerHelper helper = new CaseTriggerHelper();

    // This says run if the trigger is after the insert
    if(Trigger.isAfter && Trigger.isInsert){
        // Now call your helper method
        helper.createContaminats(Trigger.new);
    }
}

Step 7: Check if it works or not :)

Can you take a moment to  upvote and mark this answer as solved, if it helped you.
Cheers!
Ajinkya Deshmukh