i am getting this error, in the last for loop how i can get the market of the ownerId for an opportunity. where market is a custom field in User, we are trying to compare market of the user to the pricebook name which we have created seprately.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger prcieBookBasedOnMarket caused an unexpected exception, contact your administrator: prcieBookBasedOnMarket: execution of BeforeInsert caused by: System.StringException: Invalid id: id: External entry point

    public class HND_OpportunityHandler extends HND_BaseHandler implements HND_IHandler{

       private PVD_OpportunityProvider opportunityProvider;
       private HND_PriceBook2Handler priceBookHandler = new HND_PriceBook2Handler();
       private HND_UserHandler userHandler = new HND_UserHandler();

       public HND_OpportunityHandler()
           opportunityProvider = (PVD_OpportunityProvider) getProvider();         


       public override PVD_Iprovider loadProvider(){
           return (PVD_Iprovider) new PVD_OpportunityProvider();

       public void setPriceBookBasedOnMarket(List<Opportunity> newTrigger) {

           set<string> opportunityOwner = new set<string>();
           map<id,string> priceBookNameMap = new map<id,string>();
           set<string> opportunityOwnerIdMarket = new set<string>();

           // Get the list of all OwnerId from opportunity

List<Opportunity> opportunityOwnerIdList = opportunityProvider.getOpportunityOwnerId(newTrigger);

             List<User> userMarketList = userHandler.getMarketList();

             for(User u : userMarketList) {
                 opportunityOwnerIdMarket.add(u.Market__c + ' Price Book');

             List<PriceBook2> pricebook = priceBookHandler.getPriceBookNameList(opportunityOwnerIdMarket);

             for(PriceBook2 prc : pricebook) {



             for(Opportunity opty : newTrigger) {

                     string tempname; 

                     tempname = opty.owner.Market__c + ' Price Book';

                     System.debug('tempname :'+tempname);

                     string tempname1;

                     tempname1 = priceBookNameMap.get('id');

                     System.debug('tempname1 :'+tempname1);

                     if(tempname==tempname1) {
                         opty.Pricebook2Id = priceBookNameMap.get('id');



Issue in your line opty.Pricebook2Id = priceBookNameMap.get('id');

priceBookNameMap.get('id'); map will return Name(string) not Id..

Modify your code and assign Id to opty.Pricebook2Id

Here Pricebook2Id dataType is Id. So you need to assign Id

Looks like relationship is opportunity owner, userid, createdby in pricebook

Create a map and store createdbyId to PriceBook2 record

map<Id, PriceBook2> mapUserIdToPriceBook2 = new map<Id, PriceBook2>();
for(PriceBook2 prc : pricebook) {
            mapUserIdToPriceBook2.put(prc.CreatedById, prc);

And below map based on ownerId get Pricebook2Id

for(Opportunity opty : newTrigger) {

        opty.Pricebook2Id = mapUserIdToPriceBook2.get(opty.OwnerId).Id;
  • Before I assign the Id to opty.Pricebook2Id, i need to compare the value of the market of User and PriceBook. How can i compare? If you see I'm appending ' Price Book' to the string tempname so that I can compare it to the Market of Pricebook as market field in PriceBook is entered as India Price Book – RAT__ Feb 14 '16 at 9:34
  • 1
    Compile Error: Illegal assignment from Pricebook2 to Id – RAT__ Feb 14 '16 at 10:03
  • @RAT__ is this issue resolved ? – Ratan Paul Feb 16 '16 at 5:15
  • no ratan, bt i m working on it, i'll let u know if there is any issue, mean while i m also some doubts that i'll be posting in my next question, if possible plz help me there – RAT__ Feb 16 '16 at 6:53
  • @RAT__ Yeah sure – Ratan Paul Feb 16 '16 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.