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
Tejas Wadke 5Tejas Wadke 5 

Creating multiple child records from parent record

Hello,

I have a number field in Contact Object(Parent).
I want to create multiple records in Case Object(child)  based on the value in  number field in Contact Object.

Thanks,
Tejas
Best Answer chosen by Tejas Wadke 5
Head In CloudHead In Cloud
Hi Tejas,

You can create a trigger on Contact object like this:
For example the field you are using for count is "test_field__c"
trigger createChildRecordsOnContact on Contact(after insert, after update){
    list<case> caseesToInsert = new list<Case>();
    for(contact con : trigger.new){
    	if(con.test_Field__c != null){
    		for(integer i = 0; i< integer.valueOf(con.test_field__c); i++){
    			case c= new case();
    			c.closeDate = date.today();
    			c.contactId = con.id;
    			caseesToInsert.add(c);
    		}
    	}
    }
    if(!caseesToInsert.isEmpty()){
    insert caseesToInsert;
    }
}

This will solve your issue. Please mark as best answer if it helps. Thanks

All Answers

Marcilio SouzaMarcilio Souza
Hello,

You need to create a Trigger after an update or insert Contact.

See more about trigger:
https://developer.salesforce.com/trailhead/module/apex_triggers

If this answer help you please mark as the best
Marcilio
Head In CloudHead In Cloud
Hi Tejas,

You can create a trigger on Contact object like this:
For example the field you are using for count is "test_field__c"
trigger createChildRecordsOnContact on Contact(after insert, after update){
    list<case> caseesToInsert = new list<Case>();
    for(contact con : trigger.new){
    	if(con.test_Field__c != null){
    		for(integer i = 0; i< integer.valueOf(con.test_field__c); i++){
    			case c= new case();
    			c.closeDate = date.today();
    			c.contactId = con.id;
    			caseesToInsert.add(c);
    		}
    	}
    }
    if(!caseesToInsert.isEmpty()){
    insert caseesToInsert;
    }
}

This will solve your issue. Please mark as best answer if it helps. Thanks
This was selected as the best answer