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
Ashu sharma 38Ashu sharma 38 

Child to parent records creation via trigger

Hello ,



As i am trying to insert the child records,while opportunity has been created but record is not been creates,please have a look...


trigger Test01 on Demo_Object__c (before insert) {
    public list<Opportunity> opList;
    for(Demo_Object__c dc:trigger.new){
        opList=new list<Opportunity>();
        if(dc.Category__c=='Opportunity'){
            Opportunity op=new Opportunity();
            op.Name='sample';
            op.StageName='Prospecting';
            op.CloseDate=system.today();
            //insert op;
            opList.add(op);
            //dc.Opportunity__c=op.Id;
            
        }
    }
    insert opList;
}
Best Answer chosen by Ashu sharma 38
mritzimritzi
Following is a modified version of your code.
It will assign opportunity's Id to the related child records.

Trigger:
trigger Test01 on Demo_Object__c (before insert) {
    List<Opportunity> opList = new List<Opportunity>();
	Map<Integer, Demo_Object__c> demoMap = new Map<Id, Demo_Object__c();
    for(Integer i=0; i<Trigger.new.size(); i++){
        if(Trigger.new[i].Category__c=='Opportunity'){
            Opportunity op=new Opportunity();
            op.Name='sample';
            op.StageName='Prospecting';
            op.CloseDate=system.today();
            opList.add(op);
            //save Demo_Object__c record in map for re-reference, using index 'i' as key
			demoMap.put(i, Trigger.new[i]);
        }
    }
    insert opList;
	Integer opListIndex = 0;
	//size of values in Map will be equal to size of opList
    // but there can be other records in Trigger.new, which dont meet the criteria
    // size of Trigger.new >= size of opList
	for(Integer i=0; i<Trigger.new.size(); i++){
		if(demoMap.containsKey(i) == true){
			Trigger.new[i].Opportunity__c = opList[opListIndex].Id;
			opListIndex++;
		}
	}
}

Please mark this as Best Answer, if this solves your problem.

All Answers

mritzimritzi
Following is a modified version of your code.
It will assign opportunity's Id to the related child records.

Trigger:
trigger Test01 on Demo_Object__c (before insert) {
    List<Opportunity> opList = new List<Opportunity>();
	Map<Integer, Demo_Object__c> demoMap = new Map<Id, Demo_Object__c();
    for(Integer i=0; i<Trigger.new.size(); i++){
        if(Trigger.new[i].Category__c=='Opportunity'){
            Opportunity op=new Opportunity();
            op.Name='sample';
            op.StageName='Prospecting';
            op.CloseDate=system.today();
            opList.add(op);
            //save Demo_Object__c record in map for re-reference, using index 'i' as key
			demoMap.put(i, Trigger.new[i]);
        }
    }
    insert opList;
	Integer opListIndex = 0;
	//size of values in Map will be equal to size of opList
    // but there can be other records in Trigger.new, which dont meet the criteria
    // size of Trigger.new >= size of opList
	for(Integer i=0; i<Trigger.new.size(); i++){
		if(demoMap.containsKey(i) == true){
			Trigger.new[i].Opportunity__c = opList[opListIndex].Id;
			opListIndex++;
		}
	}
}

Please mark this as Best Answer, if this solves your problem.
This was selected as the best answer
Ashu sharma 38Ashu sharma 38
hello,

i am trying 
list<opportunity> optyList;
    for(Demo_Object__c dc:trigger.new){
        if(dc.Category__c=='Opportunity'){
            Opportunity op=new Opportunity();
            op.Name=dc.Name;
            op.StageName='Prospecting';
            op.CloseDate=system.today();
            optyList.add(op);
        }
    }
    insert optyList;
    
/*
    list<opportunity> myOptyList=[select id,name from Opportunity];
    for(Demo_Object__c dc:trigger.new){
        for(Opportunity o:myOptyList){
            if(dc.Name==o.name){
                dc.Opportunity__c=o.id;
            }
        }
        
    }

Please hav a look
Ashu sharma 38Ashu sharma 38
Hello,

Can we achive it without using Map on it??
mritzimritzi
using 'name' for comparison is not a good practice, as name can be duplicate. In that case your logic will  mis-fire.
Map(using Index as key, as we don't have Id in before Insert context) makes sure that mapping is correct & unique.
Ashu sharma 38Ashu sharma 38
Please share your mailId....
Ajay K DubediAjay K Dubedi
Hi Nitish, 

Below code can fulfill your requirements. Hope this will work for you.

Trigger :

trigger Test01 on Demo_Object__c (after insert) {
    public list<Opportunity> opList;
    for(Demo_Object__c dc:trigger.new){
        opList=new list<Opportunity>();
        if(dc.Category__c=='Opportunity'){
            Opportunity op=new Opportunity();
            op.Name='sample';
            op.StageName='Prospecting';
            op.CloseDate=system.today();
            opList.add(op);
            
        }
    }
    if(opList.seze()>0)
    {
      insert opList;
    }
    
}

    
Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi