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
KatherineCKatherineC 

Trigger Error: Duplicate variable

I have a custom object Pint, I want it create new pint again automatically once a pint's status is Closed.  I got an error message while creating a trigger, please help.

Error Error: Compile Error: Duplicate variable: p (attempt to re-create the variable with type: Pint__c) at line 4 column 32

trigger CreatePin on Pint__c (after update) {
       for (Pint__c p : Trigger.new) {
               if (p.Status == 'Closed') {
                       Pint__c p = new Pint__c();
                       p.Account__c = c.AccountId;                  
               insert p;
        }
        }
        }
Best Answer chosen by KatherineC
Gigi.OchoaGigi.Ochoa
You are using the variable p twice.  

Change the new Pint you creating to a different variable:  

EX:
Pint__c newPint = new Pint__c();
newPint.Account = c.AccountId;

Also, it is best practice not have have DML inside a for loop.  If you do, you code can error out due to governor limits.
https://developer.salesforce.com/page/Apex_Code_Best_Practices

To avoid governor limits issues, before your for loop, create a new List of Pint__c ==> List<Pint__c> pints = new List<Pint__c>();

Then instead of inserting your pint inside the for loop, add the new Pint to the list.  Then if your list has any pints, insert the List.

trigger CreatePin on Pint__c (after update) {
    
    List<Pint__c> pints = new List<Pint__c>();

   for (Pint__c p : Trigger.new) {
           if (p.Status == 'Closed') {
               Pint__c newPint = new Pint__c();
               newPint.Account__c = c.AccountId;                  
               pints.add(newPint);
        }
    }

   if(pints.size() > 0) insert pints;
}






All Answers

AshlekhAshlekh
Hi,

Use below code: 
trigger CreatePin on Pint__c (after update) 
{
	List<Pint__c> toInsertList = new List<Pint__c>();
	for (Pint__c r : Trigger.new) 
	   {
               if (r.Status == 'Closed') 
			   {
                   Pint__c p = new Pint__c();
                   p.Account__c = r.AccountId;                  
				   toInsertList.add( p);
               }
        }
	if(toInsertList.size()> )
	insert toInsertList;
 }


Gigi.OchoaGigi.Ochoa
You are using the variable p twice.  

Change the new Pint you creating to a different variable:  

EX:
Pint__c newPint = new Pint__c();
newPint.Account = c.AccountId;

Also, it is best practice not have have DML inside a for loop.  If you do, you code can error out due to governor limits.
https://developer.salesforce.com/page/Apex_Code_Best_Practices

To avoid governor limits issues, before your for loop, create a new List of Pint__c ==> List<Pint__c> pints = new List<Pint__c>();

Then instead of inserting your pint inside the for loop, add the new Pint to the list.  Then if your list has any pints, insert the List.

trigger CreatePin on Pint__c (after update) {
    
    List<Pint__c> pints = new List<Pint__c>();

   for (Pint__c p : Trigger.new) {
           if (p.Status == 'Closed') {
               Pint__c newPint = new Pint__c();
               newPint.Account__c = c.AccountId;                  
               pints.add(newPint);
        }
    }

   if(pints.size() > 0) insert pints;
}






This was selected as the best answer
KatherineCKatherineC
Thanks to both of you for the great help!!