You need to sign in to do that
Don't have an account?

Getting a Product ID from an Opportunity
What path should I take to get a Product ID from an Opportunity? I've looked at the API and it seems to state that the PriceBookLineItem should contain the Product ID.
I have tried this:
Code:
And get this error:
public class CP_OpportunityClosedWon { public static void CP_createNewCPUser(Opportunity[] opps){ for (Opportunity o:opps){ if (o.StageName == 'Closed Won') { ID cID = [select contactid from opportunitycontactrole where role = 'Ship To' AND opportunityid = :o.id].contactId; Contact c = [select id from contact where id = :cID]; OpportunityLineItem oli = [select id from opportunitylineitem where opportunityid = :o.Id limit 1]; ID pbeID = oli.priceBookEntryId; ID pID = [Select Product2Id From PricebookEntry where id = :pbeID].product2Id; Product2 p = [select id from product2 where id = :pID]; String cName = [select title from contact where id = :cID].title; if(p.downloadable__c == true){ c.Portal_User__c = True; c.tempName__c = cName; update c; } String t = pbeID; o.tempName__c = t; } } } }
And get this error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CP_AddCPUser caused an unexpected exception, contact your administrator: CP_AddCPUser: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.CP_OpportunityClosedWon.CP_createNewCPUser: line 10, column 26
Review all error messages below to correct your data.
Apex trigger CP_AddCPUser caused an unexpected exception, contact your administrator: CP_AddCPUser: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.CP_OpportunityClosedWon.CP_createNewCPUser: line 10, column 26
Any help is appriciated.
Thanks.
_t
Man, that error message doesn't reference anything that I see in your code snippet. Check any triggers you might have on your contact table. Maybe deactivate it and try your code again and see if the error goes away.
????
:smileyindifferent:
The trigger works and then calls the class. I had a simpler version of the class like this
That works and updates the tempName field in the Opportunity and the Contact. It also marks the Portal_User field as True.
Message Edited by tschatz on 11-13-2007 10:30 AM
Got it working! Here's the code I used, although I'm sure it could have been achieved in a more elegant manor.
Code:
This line got me the Product2Id: ID pID = [Select o.PricebookEntryId, o.PricebookEntry.Product2Id from OpportunityLineItem o where id = :li.id].PriceBookEntry.Product2Id;
_t