You need to sign in to do that
Don't have an account?
salesforce@14
How to write trigger to fill the number automatically to the number field when new record is created.
Thanks for your support.
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
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.
Thanks,
sushma
Thanks.
- 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.
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.
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.
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.