You need to sign in to do that
Don't have an account?
Ashu 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;
}
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;
}
It will assign opportunity's Id to the related child records.
Trigger:
Please mark this as Best Answer, if this solves your problem.
All Answers
It will assign opportunity's Id to the related child records.
Trigger:
Please mark this as Best Answer, if this solves your problem.
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
Can we achive it without using Map on it??
Map(using Index as key, as we don't have Id in before Insert context) makes sure that mapping is correct & unique.
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