You need to sign in to do that
Don't have an account?
Best practice in trigger
childobject__c is a custom object. and Opportunity is a standard object.
Opportunity having M_01__c,M_02__c,M_03__c,........M_24__c fields.
I want to insert each opportunity field in every record in childobject__c.
(dont use comparing of 'i' values in for loop, I want to passs dynamically i value in api names)
The fallowing approch is correct or not..! if not show me best way.
Please help me ..............
----------------------------------------------------------
trigger rampScheduler on Opportunity (after insert, after update){
if(RecursiveUtilitycls.startramp == true){
RecursiveUtilitycls.startramp = false;
List<childobject__c> insertobjchild = new List<childobject__c>();
if(Trigger.newMap.keySet().size()>0){
for(Opportunity opp : Trigger.new){
for(Integer i=1;i<25;i++){
childobject__c r = new childobject__c();
r.Opportunity__c = opp.Id;
if(i<10){
r.No_of_seats__C = opp.'M_0'+i+'__c';
}else{
r.No_of_seats__C = opp.'M_'+i+'__c';
}
insertobjchild.add(r);
}
}
if(insertobjchild.size()>0)
insert insertobjchild;
}
}
}
This part is not correct:
if(i<10){
r.No_of_seats__C = opp.'M_0'+i+'__c';
}else{
r.No_of_seats__C = opp.'M_'+i+'__c';
}
Try this:
if(i<10){
r.No_of_seats__C = Decimal.valueOf(opp.get('M_0'+i+'__c'));
}else{
r.No_of_seats__C = Decimal.valueOf(opp.get('M_'+i+'__c'));
}
I am not sure about data type for No_of_seats__c so I have used Decimal, you may change it as per your data type. Type casting is neeeded because sobject.get method returns object.
Thanks alot Amit..:)
Its working.
I am using below with help of ur answer.
if(i<10){
r.No_of_seats__C = (opp.get('M_0'+i+'__c') != null)?decimal.valueOf(opp.get('M_0'+i+'__c')+''):0;
}else{
r.No_of_seats__C = (opp.get('M_'+i+'__c') != null)?decimal.valueOf(opp.get('M_'+i+'__c')+''):0;
}