You need to sign in to do that
Don't have an account?
salesforce@14
To autopopulate the number field automatically when i delete the records using trigger.
Hi,
I need to autopopulate the number datatype field after the record is deleted.
For example,
I have 5 records and their serial numbers are 1,2,3,4 and 5. If i delete the 3rd record , then the serial number of the 4th record should be automatically populated as 3 and 5th records as 4.
How to achieve this delete operation?
My Trigger Code:
trigger RecordSteps on Record__c(before insert,after delete)
{
List<Record__c> cp = new List<Record__c>();
set<id> id = 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)
{
}
}
}
I need to autopopulate the number datatype field after the record is deleted.
For example,
I have 5 records and their serial numbers are 1,2,3,4 and 5. If i delete the 3rd record , then the serial number of the 4th record should be automatically populated as 3 and 5th records as 4.
How to achieve this delete operation?
My Trigger Code:
trigger RecordSteps on Record__c(before insert,after delete)
{
List<Record__c> cp = new List<Record__c>();
set<id> id = 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)
{
}
}
}
Try this updated code.
Best Regards,
Mithun.
All Answers
You can try below updated code.
Mark this as Best Answer, if it solves your problem.
Best Regards,
Mithun.
I got output.
when i am inserting new record the serial number is starting from 0 but i want to start it from 1.
How to do this?
Thanks for your support.
Try this updated code.
Best Regards,
Mithun.
Thanks MithunP.
For example:
Object__c - parent object.
record__c - child object
I am inserting new record in record__c under A record in Object__c, the serial number is 1. If i insert new record in record__c under B record in Object__c means the serial number is populated as 2. But i need to populate as 1 instead of 2.
How to do this?
Thanks.
You may try below code :
The above code will work in all situations and will not run into the Salesforce goverer limits,unless there are more 10,000 rows to be updated. If the Record object holds more that 10k records then I'd urge you to use an asynchronous apex approach to do it, otherwise you'd hit 10k DML rows exception.
Thanks for your support.
The code which u mentioned above is updating the serial number continuously for all the records in object__c. But my requirement is like given below.
For example i created two records as Test and Test1 in Object__c.
1)I want to insert 100 records in Test, then the serial number should be 1 to 100.
2)Sameway if i insert 200 records in Test1 ,then the serial number should starts from 1 and endup with 200.
Thanks.
I did not get your requiremet. Here is what I understood:
- There is only one object "Object__c", for which you want this serial number logic.
- If there are zero records for "Object__c" object and you insert 100 records for this object, then all new 100 records should get the serial number from 1 to 100.
- Now let's say, if there are already 10 records for "Object__c" object and then you insert 100 records for this "Object__c" object, then eventually all new 100 records should get the serial number from 11 to 110.
- And; If there are already 100 records for "Object__c" object and then you delete a record with serial number "11", then eventually there should be 99 records with serial numbers 1 to 99 (including 11).
Please correct me on my understanding.Yes you are correct.
But there are two objects,
Object__c -> parent record
record__c -> child record
These both have lookup relationship.
eg:
I am inserting 100 records in record__c under Object__c(Test). The serial number should be populates as 1 to 100.
If i am inserting 200 records in record__c under Object__c(Test11). The serial number should be populates as 1 to 200.
Thanks for your support.
You may try the below updated code:
The above snippet should run correctly. This might hit the governor limits in case you have large record count for "Record__c" object. I think if your requirements allow, you should try it in asynchronous way.
getting eror----Illegal assignment from Decimal to String
plz reply
trigger Records on Student__c(before insert,before delete)
{
List<Student__c> cp = new List<Student__c>();
set<id> id = new set<id>();
List <AggregateResult> requirements = new List<AggregateResult>();
if (Trigger.isBefore && Trigger.isInsert)
{
List <Student__c> reqSize = [select id,NO_of_Student__c from Student__c];
for(Student__c pt:trigger.new)
{
requirements=[select Count(NO_of_Student__c) from Student__c];
system.debug('+++++++++++'+requirements.size());
system.debug('++++++reqSize+++++'+reqSize.size());
if(requirements.size() > 0)
{
for(AggregateResult ar:requirements)
{
pt.NO_of_Student__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0')))+1;
}
}
if(reqSize.size() <= 0){
pt.NO_of_Student__c = 1;
}
}
}
if (Trigger.isBefore && Trigger.isDelete)
{
List<Student__c> allRecs = [select id,NO_of_Student__c from Student__c where id not in: trigger.oldmap.keyset()];
Map<Decimal,Student__c> RecMap = new Map<Decimal,Student__c>();
for(Student__c rcs: allRecs){
RecMap.put(rcs.NO_of_Student__c,rcs);
}
for(Student__c pt1:trigger.old)
{
for(Student__c rc : allRecs){
if(rc.NO_of_Student__c > pt1.NO_of_Student__c){
rc.NO_of_Student__c-= 1;
}
}
}
update allRecs;
}
}
This information has inspired me to take online courses and exams to better myself. I currently work in an auto repair shop in Rancho Cordova (https://autorepairshopranchocordova.com/). As an apprentice, I am learning brake repair (https://autorepairshopranchocordova.com/), automobile electrical repair (https://autorepairshopranchocordova.com/), transmission repair (https://autorepairshopranchocordova.com/) and suspension repair (https://autorepairshopranchocordova.com/), but I need more formal education to get a better job.
www jacklistens com