+ Start a Discussion
Adil_SFDCAdil_SFDC 

Trigger to insert 7 Records for different Record Types

Hi All 

 

I have a requirement to insert 7 records once.

 

I have 7 Record Types. If I create 1 Record with 1 Record Type my trigger to should insert other 6 records with 6 different record types.

I have a Lead Object and the lead has child object called "Home_Service_c"

Home service object has 7 record types.

 

Thanks in Advance

Rahul SharmaRahul Sharma
I would like to know what is the problem you're facing.
SurekaSureka
Hi,

Try this logic:

1. Query the record types for the corresponding Sobject
2. Add the record type ids to a set
3. Iterate through the Trigger.new for corresponding sobject
4. Check if the set contains the record type for the new record
5. Create new records for the other records by iterating through the other record type ids
6. Add the created records to a new list
7. insert the list

Thanks
ItswasItswas


List<Home_Service_c> ObjMainHomeSer = new List<Home_Service_c>();
Set<Id> objSetId = new Set<Id>();
List<RecordType> objRecType = [SELECT Id,IsActive,SobjectType FROM RecordType where SobjectType = 'Home_Service_c'];
for(RecordType obj :objRecType)
{objSetId.add(obj.Id);}
for(Id objId : objSetId)
{ Home_Service_c objHomeSer = Home_Service_c(Name = 'XYZ',RecordTypeId = objId);
ObjMainHomeSer.add(objHomeSer);}
if(!ObjMainHomeSer.IsEmpty())
insert ObjMainHomeSer;

 

Hope the above code snippet will help you

.Please check from yur end and let me know if any further problem occur.

 

Regards,

Itswas