+ Start a Discussion
m_roarkm_roark 

Errors occurring when attempting to update object in 'after undelete' trigger

I am having trouble with updating a record which triggered an 'after undelete' trigger in the trigger iteself, and need assistance with attempting to isolate why I am encountering an error message.

I am working on an Inventory control application, used for placing orders for and managing our marketing materials.

The trigger in question is fired when a 'Request Item' (an object used to track a single item order in our 'shopping cart') is undeleted.  In this case, if the user is undeleting a record which has a 'Quantity ordered' value greater than the amount of inventory currently available in the related 'Inventory' record, I want to undelete the record, but move it to a 'Backordered' status.

The trouble is, I can't seem to get at the 'Request Item' record which is being undeleted.  I need to get to it to update the status value.  I have tried three different methods; all are below.  The code generating the error messages (which are below the code) is highlighted.



First method:

else if (Trigger.IsUndelete)
        {
            Request_Item__c newRequestItem = Trigger.new[requestItem];
            String inventoryId = newRequestItem.Inventory__c;
            Inventory__c currentInventory = [Select Quantity_backordered__c, Quantity_on_order__c, Quantity_in_stock__c, Quantity_available__c, Last_order_date__c from Inventory__c where Id = :inventoryId][0];
            
            if (newRequestItem.Status__c == 'Backordered')
            {
                currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
            }
            else
            {
                if (currentInventory.Quantity_available__c < newRequestItem.Quantity_ordered__c)
                {
                    currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
                    newRequestItem.Status__c = 'Backordered';
                }
                else
                {
                    currentInventory.Quantity_available__c -= newRequestItem.Quantity_ordered__c;
                }
            }
            update currentInventory;
        }

Second method:

else if (Trigger.IsUndelete)
        {
            Request_Item__c newRequestItem = Trigger.new[requestItem];
            String inventoryId = newRequestItem.Inventory__c;
            Inventory__c currentInventory = [Select Quantity_backordered__c, Quantity_on_order__c, Quantity_in_stock__c, Quantity_available__c, Last_order_date__c from Inventory__c where Id = :inventoryId][0];
            
            if (newRequestItem.Status__c == 'Backordered')
            {
                currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
            }
            else
            {
                if (currentInventory.Quantity_available__c < newRequestItem.Quantity_ordered__c)
                {
                    currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
                    newRequestItem.Status__c = 'Backordered';
                    update newRequestItem;

                }
                else
                {
                    currentInventory.Quantity_available__c -= newRequestItem.Quantity_ordered__c;
                }
            }
            update currentInventory;
        }


Third method:

else if (Trigger.IsUndelete)
        {
            Request_Item__c newRequestItem = Trigger.new[requestItem];
            String inventoryId = newRequestItem.Inventory__c;
            Inventory__c currentInventory = [Select Quantity_backordered__c, Quantity_on_order__c, Quantity_in_stock__c, Quantity_available__c, Last_order_date__c from Inventory__c where Id = :inventoryId][0];
            
            if (newRequestItem.Status__c == 'Backordered')
            {
                currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
            }
            else
            {
                if (currentInventory.Quantity_available__c < newRequestItem.Quantity_ordered__c)
                {
                    currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
                    Request_Item__c updateRequestItem = [Select Id, Status__c from Request_Item__c where Id = :newRequestItem.Id];
                    updateRequestItem.Status__c = 'Backordered';
                    update updateRequestItem;

                }
                else
                {
                    currentInventory.Quantity_available__c -= newRequestItem.Quantity_ordered__c;
                }
            }
            update currentInventory;
        }


In the first case, I get the following error message:

Apex script unhandled trigger exception by user/organization: [value removed]/[value removed]

checkInventory: execution of AfterUndelete

caused by: System.Exception: Record is read-only

Trigger.checkInventory: line 228, column 21



In the second and third cases, I get the following error message:

Apex script unhandled trigger exception by user/organization: [value removed]/[value removed]

checkInventory: execution of AfterUndelete

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.checkInventory: line 228, column 21



According to 'Table 52: Actions and trigger events' on page169 of the APEX language reference, I should be able to 'update original object using an update DML operation' during an 'after undelete' call.

My question is how I can do this if the methods outlined above are failing.  Is this functionality which will be coming in Winter '08?

Message Edited by m_roark on 10-26-2007 03:49 PM

Message Edited by m_roark on 10-26-2007 03:50 PM

*Edited the message to make it more readable*

Message Edited by m_roark on 10-26-2007 04:20 PM

m_roarkm_roark
Perhaps one of the admins might be able to assist me in figuring out what I am doing wrong?

Is this functionality not available until Winter '08?