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
happy userhappy user 

error found in create records in custom object

We have two custom objects: Invitation and Invitation Card.  We have already selected list of companies to Invitation.  We want to have a trigger to create Invitation Card records after user updates the range of Invitation Card No. to in Invitation record.  Below is our trigger but we found the system creates two Invitation Card for each number.  Anyone can help me to fix it?  Thank you.

 

public list<Invitation_Card__c> card {get;set;}

    card = new list< Invitation_Card __c>();

   

    for(Invitation__c o: Trigger.new){

        if (o.Start_No__c != null &&  

             o.Start_No__c != System.Trigger.oldMap.get(o.Id).Start_No__c) {   

                  

        integer a = integer.valueof(o.Start_ No__c);

        integer b = integer.valueof(o.End_ No__c) + 1;

       

        for(integer i=a; i<b; i++){

           card.add(new Invitation_Card__c(

            Name = string.valueof(i),

            Date __c = date.Today(),

            Invitation__c=o.Id,

            Company__c = o.Company__c));

        }//end for

       

        }//end if

              

    }//end for

           

    insert card;

liron169liron169

What are the values in Start_ No__c, End_ No__c ?

 

Try add debug printing to track the code.

maybe before insert add:

System.debug('Record that will be inserted: ' + card);

Tejpal KumawatTejpal Kumawat

Hi,

 

Please Use this Snippt,

 

Trigger test on Invitation__c (before Insert) {

list<Invitation_Card__c> card = new list<Invitation_Card__c>();
if(Trigger.isInsert){
for(Invitation__c o: Trigger.new){
if (o.Start_No__c != null) {

integer a = integer.valueof(o.Start_ No__c);
integer b = integer.valueof(o.End_ No__c) + 1;

for(integer i=a; i <= b; i++){
card.add(new Invitation_Card__c(Name = string.valueof(i),Date __c = date.Today(),Invitation__c=o.Id,Company__c = o.Company__c));
}//end for

}//end if

}//end for
insert card;
}
}

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Thanks & Regards

 

Tej Pal Kumawat

(Certified Developer) | http://salesforce-evershine-knowledge.blogspot.in/

happy userhappy user

Hi,

 

The trigger is before update.  Our procedure is

 

1) select companies and create Invitation;

2) based on specific criteria, the number of invitation cards is calculated;

3) assign the range (start, end card no.) of invitation cards - done in dataloader or online data entry;

4) trigger to create invitation cards for later use before 3) is updated.

 

Since user may change the start, end card no. later, so we need to check if the start card no. is first time input or is a change to previous input.

 

 

happy userhappy user

I have tried several sets of Start_No__c and End_No__c, the results are the same.

 

I have put some trackings in the codes and found the first for loop was run twice and the.  Do not know why.

Leon MorockiLeon Morocki

Do you have any workflow field updates on this object?

 

The triggers may execute twice in this case.

happy userhappy user

Hi Leon,

 

Thanks.  You are right.  We've a workflow to update a field in the object.  Although it is nothing related to the trigger but it made the trigger run twice.  It works after we disable the worklfow.