ShowAll Questionssorted byDate Posted
anyioneta1.3025054394678596E12

# Multiple Update on Stock Object

THIS IS WHAT I HAVE:

Object:  Stock

 Record Id Qty  Left Qty Sold Scanned Time a06U0000004prYr 12 10 12-19-2011 5:33 AM a06U0000000vSM3 42 20 12-19-2011 5:32 AM a06U0000000vSLy 50 8 12-19-2011 5:31 AM a06U0000000vSLt 60 10 12-19-2011 5:30 AM

Assuming that after stock keeper has entered some records; he discovers that he erroneously entered an incorrect Qty Sold for record three (which reads 8 instead of 5) and he decides to update.

WHAT I WANT TO ACHIEVE:

A bulk Trigger to update the two Later (Upper) records immediately after stock keeper updates Qty Sold for record three from 8 to 5.

Logic: add the difference b/w the Old Qty Sold and New Qty Sold (i.e. 8 - 5 = 3) to the Qty Left of the two Later  (Upper) records.

This will display the accurate information about the stock table as shown below:

 Record Id Qty  Left Qty Sold Scanned Time a06U0000004prYr 15 10 12-19-2011 5:33 AM a06U0000000vSM3 45 20 12-19-2011 5:32 AM a06U0000000vSLy 50 5 12-19-2011 5:31 AM a06U0000000vSLt 60 10 12-19-2011 5:30 AM

TRIGGER CODE:

trigger stockKeeping on Stock__c (after Update) {

Map<String, Stock__c> stockMap = new Map<String, Stock__c>();

Decimal val = 0;
String oldName = '';

for(Stock__c stock: System.Trigger.new) {

oldName = System.Trigger.oldMap.get(stock.Id).Name;

if(stock.Name == oldName){
val = stock.Qty_Sold__c - System.Trigger.oldMap.get(stock.Id).Qty_Sold__c;
stockMap.put(stock.Name, stock);
}
}

List<Stock__c> listStock = [SELECT Id, Name, Stock__c, Qty_Sold__c, Date__c FROM Stock__c WHERE Name NOT IN     :stockMap.KeySet() ORDER BY Date__c DESC];

for (Stock__c stock: listStock) {

if(ock.Date__c > stockMap.get(oldName).Date__c ) {
stock.Stock__c += val;
}
}

update listStock;        //line 25, column 5
}

ERROR:

Invalid Data.
stockKeeping: execution of AfterUpdate caused by: System.DmlException: Update failed.
First exception on row 0 with id a06U0000004prYrIAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, stockKeeping: maximum trigger depth exceeded Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Test trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr] Stock trigger event AfterUpdate for [a06U0000000vSLy] Stock trigger event AfterUpdate for [a06U0000000vSLt, a06U0000000vSM3, a06U0000004prYr]: []: Trigger.stockKeeping: line 25, column 5

Please can somebody help!!.

Thanks.

yvk431

I am not very clear with your logic here 'adding hte differece to the upper records , looks like hardcoding for me"

but coming to the trigger , it should be always a last option for us to use dml in a trigger on the same object.

Try using Before Update rather than After Update ..... so that you dont have to use dml the changes made to the records will get committed automatically.

--yvk

anyioneta1.3025054394678596E12

Tried using a before trigger this is the error I get.

Error:

Invalid Data. Review all error messages below to correct your data.

Apex trigger stockKeeping caused an unexpected exception, contact your administrator:

stockKeeping: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a06U0000000vSM3IAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,

stockKeeping: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a06U0000004prYrIAI; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a06U0000004prYr) is currently in trigger testTest, therefore it cannot recursively update itself: [] Trigger.stockKeeping: line 25, column 5

======Clearify my Logic======

>> I already have a trigger that subtracts Qty Sold From an invisible column that holds Number of goods in the Wearhouse in  to get Qty Left.

>> Whenever stock keeper updates the Qty Sold for a record, he also has to update the Qty Left of its subsequent records individually to get the balance Number of goods left in the warehouse after a sale is made.

>> From my Stock Table example I tried to explain this:
Notice that if the Qty Sold of Record3 is update then Qty Left of Records 1 and 2 also has to be updated individually to get the actual Quantity of number of goods left in the warehouse after each sales made.

>> imagine that out of 100 sales record, stock keeper realizes that he made a mistake in the Qty Sold of record 11; it then means that after correcting the Qty Sold of record 11, he also has to correct the Qty Left of 89 records individually. Nobody would want to do that even if he has the whole time.

>>What I want to achieve is a trigger that would automate this process
such that whenever the Qty Sold of a record is updated then the Qty Left of its subsequent records should be updated too.

Pls Im I clear with My Logic now and How can it be solved.

Thanks.

anyioneta1.3025054394678596E12

I removed update listStock;        //line 25, column 5

Only Record3  updated But  Record2 and Record1 probably updated but did not commit to the database so it still will not give me what I want

If you refer to my Stock table example

I want as soon as Qty Sold of Record3 is Updated

then Qty Left of Record1 and 2 should be updated automatically

Pls ids this achievable??

yvk431

hey sorry i mistook ur requirement, thats why i deleted my post.

Since we need to update the records which are not part of the bulk trigger, we need to have the dml in place . But by doing so you will initiate an infine loop which is the actual problem in here. To avoid this you should make this trigger work only for certain criteria let say only when sold quantity is modified. Sorry for t\he trouble again.