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
howard zien 24howard zien 24 

having problems bulkifying a trigger

i made a custom object to test some logic I am trying to learn.  i could have used the standard object account, but wanted to isolate my changes in a custom object.

the custom object is hpzaccount.
there is a field in this table called name.
I made a 2nd field in the table called name_unique.

name_unique is a read only-field that does not permit dups.

whenever a new record is added or updated to hpzaccount,  the trigger sets
   name_unique__c = name;

the non-bulk version of this trigger works fine. It is
trigger create_unique_name on hpzaccount__c (before insert, before update) {
for (hpzaccount__c    hpza : Trigger.new){
  hpza.name_unique__c = hpza.name;
}
}

but i want to bulk up the trigger so that it works with dataloader.
I am getting a syntax error that I cannot figure out. can you help?

  
trigger create_unique_name on hpzaccount__c ( before insert, before update) {
    List< hpzaccount__c >  hpzlist  = new List< hpzaccount__c >{};
     //Loop through all records in the Trigger.new collection
   for(hpzaccount__c   hpza  :  Trigger.new){
        
      //Concatenate fields to list
        hpzlist.name = hpza.name;
       hpzlist.name_unique__c = hpza.name;
                                                             }
Insert hpzlist;
  }      








 
Best Answer chosen by howard zien 24
Alexander TsitsuraAlexander Tsitsura
Hi Howard,

But this is bulk trigger
trigger create_unique_name on hpzaccount__c (before insert, before update) {
  for (hpzaccount__c hpza : Trigger.new){
    hpza.name_unique__c = hpza.name;
  }
}
In this trigger you can not perform any dml/soql. In before trigger action you can change field value without dml on trigger objects.

Thanks,
Alex
 

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Howard,

But this is bulk trigger
trigger create_unique_name on hpzaccount__c (before insert, before update) {
  for (hpzaccount__c hpza : Trigger.new){
    hpza.name_unique__c = hpza.name;
  }
}
In this trigger you can not perform any dml/soql. In before trigger action you can change field value without dml on trigger objects.

Thanks,
Alex
 
This was selected as the best answer
howard zien 24howard zien 24
good to know.  thanks.
Howard