+ Start a Discussion
snakerootsnakeroot 

Can' t access opportunity line items for opportunity that the trigger fires on

Disclaimer:  I'm a total newb at apex, so forgive my ignorance.  

 

 

What I'm trying to do: 

 

Check that an opportunity is marked paid if the opportunity represents an invoice

or

Check that an opportunity represents a credit memo

 

If either of the above are true, then I want to query the opportunity line items to see if there's an item key called RRR22.  If there is, I want to add a few pieces of information from that opportunity line item and the opportunity to a custom object (nicknamed the vault). 

 

So far, with the code below I can query the opportunities and determine my first two criteria (listed above) and find the ones that have RRR22 as an item key in the line items.  The problem is, in the for loop that looks through the opportunity line items, I seem to have access to all oli's except the one the trigger is firing on.  So if I have 3 invoices InvoiceA, B and I create C, the trigger fires because C is new.  I'm able to find the line item information from A & B, but not C. 

 

Can anyone shed some light on this for me?  

 

 

trigger RRR_Opportunity on Opportunity (after insert) {
// Check opportunities for line items that use the RRR22 item key


//Create set of opportunities to be checked.  
Set<Id> oppsToBeChecked = new Set<Id>();
       for (Opportunity o : Trigger.new){
         if(o.QB_Template__c !=''){       //Check that the opportunities is from QB before adding it to the set of opportunities to be checked.
           oppsToBeChecked.add(o.Id);
         }
       }
  

if(!oppsToBeChecked.isEmpty()){
	for(Opportunity o : [SELECT Id, Is_Paid__c, Invoice_Num__c, Credit_Memo_Num__c,AccountId,
			   (SELECT Id, PriceBookEntry.Product2.Name
                FROM OpportunityLineItems
                WHERE PriceBookEntry.Product2.Name = 'RRR22'
                ORDER BY PriceBookEntry.Product2.Name) // little trick to get the ones with RRR22 sorted first
            FROM Opportunity
            WHERE QB_Template__c != '' AND Id IN :oppsToBeChecked]){
    // ONLY insert entry into the Vault if opportunity is an invoice, has RRR22 item(s) and is paid OR
    // if the opportunity is a credit memo and has RRR22 item(s)        	
	if((o.Is_Paid__c == true && o.Invoice_Num__c !='') || (o.Credit_Memo_Num__c !='')){
		//insert line item into the Vault
		//system.debug('An entry would be submitted to the vault at this point.');
		
		for(OpportunityLineItem oli : [SELECT Id,PriceBookEntry.Product2.Id,Opportunity.AccountId,OpportunityId,Quantity
			FROM OpportunityLineItem
			WHERE OpportunityId IN :oppsToBeChecked]){
		system.Debug('oli.OpportunityId =' + oli.OpportunityId + ' / o.Id = ' + o.Id);
			
				
				RRR_Vault__c vaultEntry = new RRR_Vault__c();
				vaultEntry.Date__c = date.today();
				vaultEntry.Refrigerant_Type__c = String.valueOf(PriceBookEntry.Product2.Name);   //FIX ME
				vaultEntry.Account__c = o.AccountId;
				vaultEntry.Transaction_Type__c = 'Withdrawal';  // FIX ME
				vaultEntry.Salesforce_ID__c = o.Id;
		
					// Test to see if this is an invoice or credit memo
					if (o.Invoice_Num__c !='') {
						vaultEntry.Linked_To__c = o.Invoice_Num__c;
					}else{
						vaultEntry.Linked_To__c = o.Credit_Memo_Num__c;
						}
			
					vaultEntry.Refrigerant_Qty__c = oli.Quantity;
						
				insert vaultEntry;
				system.debug('Entry successfully entered into the Vault!');	
		
		}
	}

}

}

}

 

 

 

 

snakerootsnakeroot

I'll try simplifying my request and see if someone can help me out.  

 

I have created an opportunity and on the opportunity there is one product line item.  When the trigger below fires, why is the count 0 for the number of line items?

 

trigger RRR_Opportunity on Opportunity (after insert) {

//Create set of opportunities to be checked.  
Set<Id> oppsToBeChecked = new Set<Id>();
       for (Opportunity o : Trigger.new){
         oppsToBeChecked.add(o.Id);
       }

integer c = [SELECT COUNT() FROM OpportunityLineItem WHERE OpportunityId in :Trigger.new];
system.debug (c);	
}