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
ranadheer reddy chaduvuranadheer reddy chaduvu 

I have 2 objects 1 is Trig1__c,second one s Trig2__c......on these 2 objects i have written a trgger to create

I have 2 objects 1 is Trig1__c,second one s Trig2__c......in Trig1__C i have a numberfield called No_of_Trig2__c(Numberfield)....so here what i need is if i enter the 10 in No_of_Trig2__c then 10 records should be created autometically in Trig2__c,......so here is my trrigger

trigger Cretaingrecontrig2 on Trig1__c (after insert) {
  List<Trig2__c>Listtri=New List<Trig2__C>();

  for(Trig1__c tr:Trigger.new){
        if(Trig1__c.No_of_Trig2__c!=null){
        for(integer i=0;i<Trig1__C.No_of_Trig2__c;i++){
            Trig2__c tri=New List Trig2__c();
            tri.id=tr.id;
            tri.Name=tr.Name;
        Listtri.add(tri);
  }
  if(!Listtri.Isempty()){
  insert Listtri;
  }
  }
  }
  }



so here the trigger is saved fine ...but the problem is while saving the record am getting below error

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Cretaingrecontrig2 caused an unexpected exception, contact your administrator: Cretaingrecontrig2: execution of AfterInsert caused by: System.TypeException: Invalid integer: common.apex.runtime.impl.ApexFieldToken@7bce7881: Trigger.Cretaingrecontrig2: line 6, column 1



what may be the problem

ranadheer reddy chaduvuranadheer reddy chaduvu

sorry here is my trigger .....small change


trigger Cretaingrecontrig2 on Trig1__c (after insert) {
  List<Trig2__c>Listtri=New List<Trig2__C>();

  for(Trig1__c tr:Trigger.new){
        if(Trig1__c.No_of_Trig2__c!=null){
        for(integer i=0;i<Integer.valueof(Trig1__C.No_of_Trig2__c);i++){
            Trig2__c tri=New List Trig2__c();
            tri.id=tr.id;
            tri.Name=tr.Name;
        Listtri.add(tri);
  }
  if(!Listtri.Isempty()){
  insert Listtri;
  }
  }
  }
  }
Anoop yadavAnoop yadav
Hi,

Put a debug to print this  " Integer.valueof(Trig1__C.No_of_Trig2__c) " value before

this Line
for(integer i=0;i<Integer.valueof(Trig1__C.No_of_Trig2__c);i++){
Sonam_SFDCSonam_SFDC
try if(tr.No_of_Trig2__c!=null)
 instead of 
if(Trig1__c.No_of_Trig2__c!=null)
Chidambar ReddyChidambar Reddy
Hi Ranadheer

trigger Cretaingrecontrig2 on Trig1__c (after insert) {
  List<Trig2__c>Listtri=New List<Trig2__C>();

  for(Trig1__c tr:Trigger.new){
        if(tr.No_of_Trig2__c!=null){
        for(integer i=0;i<Integer.valueof(tr.No_of_Trig2__c);i++){
            Trig2__c tri=New Trig2__c();
            
/**You need to change the below Line */
tri.id=tr.id; 
// Please give the Parent Relationship Name (ex: tri.Trig1__c, tri.Object1__c)

            tri.Name=tr.Name;
        Listtri.add(tri);
  }
}
  if(!Listtri.Isempty()){
  insert Listtri;
  }
 

  }

Choose it as Best Answer if it resolves your issue