We are creating OpportunityLineItem through Apex, now in isAfterInsert we are creating a Test
object record whose Unique_Key is same as OpportunityLineItem unique_key.
Now attaching the 'Test' to OpportunityLineItem is written in beforeUpdate of OpportunityLineItem. So we are expecting that in isAfterInsert the Test record should be created and when we come to beforeUpdate, we can query the Test record with the unique_key from OpportunityLineItem and attach those to the OpportunityLineItem.
Now the issue is when the code runs the Test record is created, but when it comes to beforeUpdate it cannot find any test record with the Unique key. I have put the debug statements and can see blank List printed after query is executed.
How can I delay this process so that the beforeUpdate runs after the test record is created?
In is after update of OpportunityLineItemTriggerHandler
public static void createTestRecordWithMatchingOLIKey(List<OpportunityLineItem> newList, Map<Id, Opportunity> mapOfOpp){
//Initialise Variables
Map<Id, String> mapOliWithTestKey = new Map<Id, String>();
List<Test> listUpdateYearlyTests = new List<Test>();
for(Integer i=0; i<newList.size();i++){
mapOliWithTestKey.put(newList[i].Id,newList[i].TestMactchingKey__c);
}
if(!mapOliWithTestKey.isEmpty()){
Test yearlySpa = createSpa('Yearly',mapOliWithTestKey.get(newList[i].Id),
newList[i], mapOfOpp.get(newList[i].OpportunityId),null, mapOfOpp.get(newList[i].OpportunityId).test_type__c);
listUpdateYearlyTests.add(yearlySpa);
if(!listUpdateYearlyTests.isEmpty()){
Set<Test> uniqueSPAs = new Set<Test>();
uniqueSPAs.addAll(listUpdateYearlyTests);
listUpdateYearlyTests.clear();
listUpdateYearlyTests.addAll(uniqueSPAs);
List<Database.UpsertResult> upsertResults = Database.upsert(listUpdateYearlyTests,false);
}
}
}
Now in is before Update
matchingKeySet.add(lNew[i].TestMactchingKey__c);
List<Test> testList = new List<Test>([Select Id from Test where Unique_Key__c IN :matchingKeySet]);
Trigger OLI code
if(trigger.isBefore){
// INSERT
if(trigger.isInsert)
{
OpportunityLineItemTriggerHandler.IsBeforeInsert(lNew,mNew);
}
// UPDATE
if(trigger.isUpdate)
{
OpportunityLineItemTriggerHandler.IsBeforeUpdate(lOld,mOld,lNew,mNew);
}
// DELETE
if(trigger.isDelete)
OpportunityLineItemTriggerHandler.IsBeforeDelete(lOld,mOld);
}
/*************************************************
********************* AFTER **********************
*************************************************/
if(trigger.isAfter){
// INSERT
if(trigger.isInsert)
OpportunityLineItemTriggerHandler.IsAfterInsert(lNew,mNew);
// UPDATE
if(trigger.isUpdate)
OpportunityLineItemTriggerHandler.IsAfterUpdate(lOld,mOld,lNew,mNew);
// DELETE
if(trigger.isDelete)
OpportunityLineItemTriggerHandler.IsAfterDelete(lOld,mOld);
}
The matching key from OLI's TestMactchingKey__c matches Test's UnikeyKey__c.
The createSPA
method creates the SPA with data from OLI (OpportunityLineItem).
The funny part is after creation it does not find anything in testList
. But if I just do a dummy update then it finds the testList
.
isBeforeUpdate
on OLI and that is why it is not able to find the Test record. This works after we do a dummy update in OLI,.createTestRecordWithMatchingOLIKey()
method inafter
context, I'm not sure why it's not working. May be check whether your update trigger is firing multiple times. Also if possible, add check only execute your code when certain criteria is met by comparing old value and new value.