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
JNicJNic 

Query returning inconsistent results...

Hey all,

 

I've made this syncronous trigger:

 

 

trigger inventoryItemAfter on inventoryItem__c (after update) {

list<inventoryItem__c> itms = [Select
i.quantityOnHand__c,
i.lastSyncTime__c,
i.Id, (Select id, quantity__c, dateTime__c From iiQuantities__r)
From inventoryItem__c i
where i.Id IN :Trigger.newMap.keySet()
];

//Create a list of quantities
list<iiQuantity__c> qs = new list<iiQuantity__c>();

//For each inventory item that has been updated
for (inventoryItem__c i : Trigger.new) {
//If we have increased by one day
if (Trigger.oldMap.get(i.id).lastSyncTime__c.day() != i.lastSyncTime__c.day() ) {
//Then we insert a NEW history row item...
iiQuantity__c q = new iiQuantity__c(
inventoryItem__c = i.Id,
quantity__c = i.quantityOnHand__c
);
qs.add(q);
} else {
/*
//We just update the existing one...
iiQuantity__c q = i.iiQuantities__r[0];
q.quantity__c = i.quantityOnHand__c;
q.dateTime__c = dateTime.now();
qs.add(q);
*/
system.debug('\n\nTHESE ARE THE QUANTITIES: ' + i.iiQuantities__r + '\n\n');
}
}
//Do it now!!!
if (!qs.isEmpty()) {
upsert qs;
}
}

 But the iiQuantities__r is null!

 

I know it's not null, because when i run it in the eclipse org it returns 4 children... why is is empty in my debug log?

 

The relationship is master-detail

 

Debug:

 

16.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
13:01:30.158|EXECUTION_STARTED
13:01:30.158|CODE_UNIT_STARTED|[EXTERNAL]|01qM00000004Czg|inventoryItemAfter on inventoryItem trigger event AfterUpdate for [a0eM0000000J3W3]
13:01:30.159|SOQL_EXECUTE_BEGIN|[3]|Aggregations:1|Select
i.quantityOnHand__c,
i.lastSyncTime__c,
i.Id, (Select id, quantity__c, dateTime__c From iiQuantities__r)
From inventoryItem__c i
where i.Id IN :Trigger.newMap.keySet()

13:01:30.161|METHOD_ENTRY|[8]|MAP.keySet()
13:01:30.161|METHOD_EXIT|[8]|MAP.keySet()
13:01:30.171|SOQL_EXECUTE_END|[3]|Rows:1
13:01:30.171|METHOD_ENTRY|[17]|Datetime.day()
13:01:30.171|METHOD_ENTRY|[17]|MAP.get(ANY)
13:01:30.172|METHOD_EXIT|[17]|MAP.get(ANY)
13:01:30.172|METHOD_EXIT|[17]|Datetime.day()
13:01:30.172|METHOD_ENTRY|[17]|Datetime.day()
13:01:30.172|METHOD_EXIT|[17]|Datetime.day()
13:01:30.172|METHOD_ENTRY|[32]|System.debug(ANY)
13:01:30.172|USER_DEBUG|[32]|DEBUG|

THESE ARE THE QUANTITIES: ()


13:01:30.172|METHOD_EXIT|[32]|System.debug(ANY)
13:01:30.172|METHOD_ENTRY|[36]|LIST.isEmpty()
13:01:30.172|METHOD_EXIT|[36]|LIST.isEmpty()
13:01:30.972|CUMULATIVE_LIMIT_USAGE
13:01:30.972|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 5 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Number of script statements: 4 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

13:01:30.972|CUMULATIVE_LIMIT_USAGE_END

13:01:30.173|CODE_UNIT_FINISHED|inventoryItemAfter on inventoryItem trigger event AfterUpdate for [a0eM0000000J3W3]
13:01:30.173|EXECUTION_FINISHED

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JNicJNic

LOL ok, I'm a fool!

 

I the for loop is looking at Trigger.new, not itms!