You need to sign in to do that
Don't have an account?
Update Index value of record in Name(standard field)
Hi All,
Following is my requirement :
i want to update Name field on a record with it's index value in salesforce.
For e.g Object A is the child of Object B :
Then before insert a trigger runs and checks number of records in the related list (object A) of Object B and puts that value as the serial number of that record.Now the problem is, when, i insert multiple records both get the same index number as both records are inserted at the same time.
So, if earlier to insert, count () was 2
Then 2 records are added so both get the Name '4'
while it should be
'3' for the former and
'4' for the latter.
This is the code i have used for single record insert.
trigger updatecompland on ObjA__c (before insert)
{
for (ObjA__c bp:trigger.new)
{
Set<Id> s1 = new Set<Id>();
s1.add(bp.ObjB__c);
Integer bpscount = [Select count() from ObjA__c where ObjB__c IN :s1] + 1 ;
string t1 = string.valueOf(bpscount);
bp.name = t1;
}
}
How to make it work for bulk inserts??
Please help!!!
Many thanks in advance.
Regards,
Anidev
P.S. - "after insert,after update" - triggers cannot be used as this field becomes"read only" after insert.
Please correct, if my understanding is incorrect
Do let me know if this help. If so mark this as a solution and hit the Kudos button.
Regards
vbs
All Answers
Try the above code.
I just bulkified your code. Have a look on this you will have a Idea how to use collection to bulkify ur code. Also you can modify the trigger.new in before triggers only.
Do let me know if this help. If so mark this as a solution and hit the Kudos button.
Regards
vbs
Hi Avidev9,
Firstly, sincerest apologies for replying so very late.
however i thought it best to try out your code in as many ways possible and then reply.
Post same, i feel, this is may not be associated with bulkifying triggers as - this trigger works fine irrespective of whether i insert 1 record or many ( i++ ).
Let me highlight the problem in another way -
Lets for e.g keep the child record counter at 0
//Initial Position
Count().ObjA = 0
Now "1" record is inserted so
Count().ObjA = 1
Same is assigned as record name
// works perfect
But now 3 records are inserted
Count().ObjA = 4 (1+3)
All records are assigned name as "4" // which is incorrect//
So some thing like a savepoint() is required where i can store records on a temp() basis before commiting records to database.
This save point stores the staggered changes and then on each iteration -
1- 2
2- 3
3- 4
same value is attached to respective records.
Hope i was able to make it clearer.
Many thanks in advance and Thanks for the immediate reply made earlier.
Regards,
Anidev
Hi vbs,
Thanks for the suggestion.
I did eventually have to go in for the solution you suggested as you know all of us have to work under tight time schedules.
however, we face precisely the problem i had anticipated -
What happens is, the auto numbers are assigned based on number of records created under particular object.
So for e.g. the record created in related list "Obj A" would be the 1st one under Obj B, but the auto number shows 56.
There you get it, the limitation of auto numbers.
Please correct me if my understanding is incorrect.
Regards,
Anidev