function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
m_roarkm_roark 

How does one 'update original object using an update DML operation'?

In the Apex Language reference, on page 168, in  'Table 52: Actions and trigger events', there is a column called 'Can update original object using an update DML operation'.  How does one do this?  I have looked through the entire language reference, and am unable to find out how this is done.
sparkysparky
See pg. 78 in that document.
m_roarkm_roark
Matthew,

Thanks so much for the response.  I have read that section, but am still at a loss.  I am trying to access an object to update it during an 'after undelete' call. 

In order to update the record in question, I have to be able to fetch the record.  However, I am not able to identify how to fetch the ID for the record in question, or fetch the record itself in an 'after undelete' trigger.

Can anyone help me identify how to fetch the record?

Message Edited by m_roark on 11-15-2007 02:17 PM

sparkysparky
trigger.new

see pg. 167

Message Edited by sparky on 11-15-2007 02:56 PM

m_roarkm_roark
Matthew,

I get a null pointer exception when I attempt to access the object using 'Trigger.new' in the 'after undelete' trigger.  The documentation you referenced says that you can only access Trigger.new during an 'insert' or 'update' trigger, and Trigger.old during an 'update' or 'delete' trigger.

I get the following error message when I attempt to reference the object using Trigger.new

Force.com Sandbox

Apex script unhandled trigger exception by user/organization:

checkInventory: execution of AfterUndelete

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

Trigger.checkInventory: line 229, column 21

Here is the code that is failing:

Code:
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;
}
Basically, I am working on an Inventory management system.  If a request for Inventory is being undeleted, I want to set it to a status of 'Backordered' if insufficient inventory exists to fill the request.  However, I can't seem to get at the undeleted record to update the status.
 

sparkysparky
I can't tell what's happening without seeing more of your code.
Is the error happening on that 2nd line in your snippet?  Where does the requestItem variable come from?



m_roarkm_roark
Matthew,

The 'requestItem' variable is the Integer I use for iterating through the items in the 'Trigger' array.

The complete code looks something like this

Code:
trigger checkInventory on Request_Item__c (before insert, before update, before delete, after undelete) 
{
for (Integer requestItem = 0; requestItem < Trigger.size; requestItem++)
{
if (Trigger.IsInsert)
{
\\Insert code
}
else if (Trigger.IsUpdate)
{

\\ Update code
}
else if (Trigger.IsDelete)
{
\\ Delete code
}
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;
}
}
}
As for where the issue is occurring, as near as I can tell, it happens once I try to access any of the field values for the 'newRequestItem' variable, which is simply the current object in the 'Trigger.new' array.  In this case, the error occurs when I attempt to extract the ID for the record from the 'newRequestItem' variable.

sparkysparky
I don't immediately see anything wrong.

is it this line that's throwing the error, then? :
   
String inventoryId = newRequestItem.Inventory__c;

if so, the only things I can think of are:
* use id var type instead of string (not sure that matters)
* are you sure that the record will have anything in the Inventory__c field?
* are you sure you got the field name right?

Message Edited by sparky on 11-15-2007 04:38 PM

sparkysparky
BTW, if should be possible for you to figure this out with a few judicious debug commands, so you can peek at what your variables actually contain.
m_roarkm_roark
Matthew,

Thanks for the suggestion.  After some judicious debugging, I identified that the 'currentInventory.Quantity_backordered__c' variable was null, and as such was triggering the 'Null Reference Exception'. 

This was because I was attempting to use the '+=' operator to increment and assign to this variable, and this is not a valid operator when the variable value is null.

I added a check for null, and am past this error.

Code:
if (currentInventory.Quantity_backordered__c == null)
  currentInventory.Quantity_backordered__c = newRequestItem.Quantity_ordered__c;    
else
  currentInventory.Quantity_backordered__c += newRequestItem.Quantity_ordered__c;    
                   
Thanks again for your assistance.