+ Start a Discussion
salesforce@14salesforce@14 

How to write trigger to fill the number automatically to the number field when new record is created.

Thanks for your support.
pbattissonpbattisson
A trigger may not be the best way to automatically set the value in a number field, I would suggest you look at an autonumber field, default value formaula or workflow field to see if these would meet your needs. If you provided more information (what object, field values needed etc.) then perhaps more help could be provided.
salesforce@14salesforce@14
Thanks pbattisson.

But my requirement is to write a trigger code. The Serial Number field( Number datatype) should be populates automatically when new records is created or deleted.
 
pbattissonpbattisson
Why is your requirement to write a trigger? How is the serial number calculated? What is is made up of?
salesforce@14salesforce@14
I need to autopopulate the serial number eachtime when new record is created.
pbattissonpbattisson
I understand that - how is the serial number calculated? Is it retrieved from another source? Is it an automatically inceremented number? Answering how the serial number is generatedor retrieved will determine whether you need a trigger and how to give you the needed functionality.
salesforce@14salesforce@14
can u share the sample trigger code for this functionality.

Thanks,
sushma
MaddyConnectMaddyConnect
Autonumber data type provide feature to generate formated serial number in the incremental order. Let us know if you have any specific requirement where you trying to place trigger instead of autonumber data field.
salesforce@14salesforce@14
Why we going for Trigger means, In auto number field the value wont be automatically adjusted when u delete any record. 

Thanks.
 
pbattissonpbattisson
Clarify that your requirement is:
- A new record is created
- The serial number field should be set to be the next possible value from all the serial numbers
- When the record is deleted, the serial number should then dcrement for all those records with a higher serial number.

I cannot share trigger code with you for this because (a) you haven't defined the object or fields for use (b) we don't know how the serial number is generated. Please answer the question then we can help you.
salesforce@14salesforce@14
Yes my requirement is,
 
1) The serial number is populated as 1 when i create a first record and S.NO is populated as 2 when i create a second record and so on...
2) when  i delete the 4th record,the S.NO in 5 th record should be autopopulate as 4.

Thanks.

 
pbattissonpbattisson
OKay, so your trigger should be an insert, deleta dn undelete trigger. On insert, retrieve the count of all existing records, (use the COUNT SOQL query) then loop through new records and set the value for the serial number, incremementing it each time.

On delete you should retrieve all the reocrds with a serial number greater than the record being deleted and decrement them by one before updating them. Note that if you have multiple items being deleted at once you will need to handle this by choosing the lowest item as the starting point, retrieveing all other items in order and correctly updating them, being carefgul to jump when you hit other higher valued items to be deleted.

Finally on undelete, you can either set the serial number for the old item as the next iteration, or incremenet the serial numbers for all higher valued records.

I would recommend you use an autonumber for this as it would seem to be a very abd idea to have serial numbers being reordered, reassigned and recalculated. The idea of the serial number is likely to be that it could be used as a reference going forwards anc chaning the value in its lifecycle negates the use of it from this perspective.

The forums are designed to help in providing guidance on code or debugging issues where you have made an attempt. You now have guidance to get started so should be able to start writing the code from there. Without you providing a code attempt nobody can help you with further code. 
salesforce@14salesforce@14
Thanks for your support pbattisson.
salesforce@14salesforce@14
Hi,
  
  I did upto incrementing the serial number field automatically eachtime i create an new record.The value is starting from 0 but i need to start it from 1.
And I dont know how to do for before delete. How to do it?

My Trigger Code:

trigger TrialUATSteps on Record__c(before insert,after delete) 
    {
        List<Record__c> cp = new List<Record__c>(); 
        set<id> uatid = new set<id>();
        List <AggregateResult> requirements = new List<AggregateResult>();  
       if (Trigger.isBefore && Trigger.isInsert)
       { 
        for(Record__c pt:trigger.new)
        {
         requirements=[select Count(Seq__c) from Record__c];
         if(requirements.size() > 0)
         {
         for(AggregateResult ar:requirements)
         {
         pt.Seq__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0')));
          }
      }
      }
}
      if (Trigger.isAfter && Trigger.isDelete) 
      {
      for(Record__c pt1:trigger.old)
        {
          }
          }   
     }

Thanks for your support.