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
SwaranPoladiSwaranPoladi 

Trigger Record

Hi,

 

I have a scenario.

 

We have batch load that inserts number of records on to custom object. For all these records created in a batch we have to create one parent record e.g:- if a CSV file has 10000 records then 1 record to inserted and tag all 10000 records with it.

 

I have tried using static variable but its not working 

 

below is the code

 

trigger Change on Site__c(before insert, before update){
Set<Id> setSiteId = new Set<Id>();
List<Site__c> ltSiteUpd=new List<Site__c>();
if(setStaticVars_CLS.firstrun==1){
setStaticVars_CLS.setinsertTariffChange();
setStaticVars_CLS.firstrun++;
}
for(Site__c objSR:Trigger.New){
if(Trigger.isInsert){
objSR1.Tariff_Change__c=setStaticVars_CLS.getinsertTariffChange();
ltSiteUpd.add(objSR1);
}
}
}

Static class

global class setstaticVars_Cls{

global static Integer firstrun=1;
global static Id idTC;
global static void setinsertTariffChange(){
Tariff_Change__c objTC=new Tariff_Change__c();
insert objTC;
idTC=objTC.id;
}
global static Id getinsertTariffChange(){
return idTC;
}
}

EnthEnth

Rather than explain the issues in the code I think you need to re-think your solution. I assume you're using the Builk APi to load your data, in which case the 10k records will be loaded in separate batches and each batch will start a new execution context, so even if you have a static variable holding the batch I believe you'll get multiple parent records.

 

Another way to associate these would be to provide your own Batch Number as an External Id on your parent object and then you can get Salesforce to link each of your 10k records to the parent object (after you create a new parent record before your load process).

 

HTH