+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Not able to add opportunity products

Hi,

  I was to convert data that is captured in custom object into account, opportunity and opportunity products. I am able to create account and opportunity I am facing issue in adding products to products not able add products please adive me what wrong am I doing in this code 
 
trigger DealReg_To_Opportunity on DealReg__c (After Insert)
{
 // asyncApex.processOpportunity(Trigger.newMap.keySet());
 
     Set<Id> idSet = Trigger.newMap.keySet();

    Account acc = new Account();
    Opportunity opp = new opportunity();     
     
    String Don = '00560000003W8gx';
    String Roger = '00560000004HPy1';
    String OppOwner;
    
    
  DealReg__c deal = [SELECT id, customer_name__c, deal_stage__c,
                            account_id__c, estimated_close_date__c,
                            estimated_value__c, competitor__c,
                            account__c, project_name__c,
                            distributor__c, reseller__c,
                            channel_source__c, discount_program__c,
                            ownerid, status__c, owner.type,
                            field_representative__c, theater__c,
                            Partner_Initiated__c,Partner_Led__c,K_12__c,Industry__c,Company_Url__c,Cloud_Deal_Registration__c,Product_Quantity__c,
                            Cloud_Product__c,Cloud_Service_Term__c,Cloud_Subscription_Term__c,
                            AP_Product__c,Service_Term__c,Subscription_Term__c
                        FROM DealReg__c
                        WHERE id = :idSet ]; 
   
  String Distributor;   
  String Region = deal.theater__c;
  String OppRecordType;
  
  /* Assign Opportunity Record type */
  if ( deal.Cloud_Product__c <> NULL ) 
  {
   OppRecordType = '0123000000094yHAAQ';
  }
  
  else if ( deal.Cloud_Subscription_Term__c <> NULL )
  {
   OppRecordType = '012g00000004qDPAAY';
  }
  
  /* Assign Opportunity owner */
  
  if ( Region == 'NAM') 
  {
     OppOwner  = '00560000004HPy1';
   }
  else if ( Region == 'EMEA')
  {   
     OppOwner = '00560000003W8gx';
   }
     
  
  /* Assign distributor */
  if ( deal.Cloud_Product__c <> NULL && deal.Cloud_Service_Term__c<> NULL )                 
   {
   Distributor =  deal.distributor__c ;    
   }                     
  else if ( deal.Cloud_Subscription_Term__c <> NULL && deal.theater__c == 'NAM'  ) 
   {
    Distributor = '0016000000pyLll'; // defaulting to westcon
   }
  else if ( deal.Cloud_Subscription_Term__c <> NULL && deal.theater__c == 'EMEA'  )
   { 
   Distributor = '0016000000YmDkh'; //defaulting to zyko
   }
   else if ( deal.Cloud_Subscription_Term__c <> NULL && deal.theater__c == NULL  ) 
   {
    Distributor = '0016000000pyLll'; // defaulting to westcon
   }

  String CloudPricebookId;
     
    /* Price Book */
    IF ( Region == 'NAM') 
    {
      CloudPricebookId = '01s60000000AKxZAAW';
    }
    ELSE IF ( Region == 'EMEA') 
    {
      CloudPricebookId = '01s60000000AKxUAAW';
    } 
    ELSE 
    {
      CloudPricebookId = '01s60000000AKxZAAW';
    }
    
  If ( deal.Cloud_Deal_Registration__c == 'Yes') 
   {                        
  acc.name = Deal.customer_name__c;
  acc.Industry = Deal.Industry__c;
  acc.Website = Deal.Company_Url__c;
  acc.Type = 'Prospect';
                  
  Insert acc;                      
   
        opp.AccountId = acc.id;     
        opp.RecordTypeId = OppRecordType;                
        opp.name = Deal.project_name__c +  ' ' +'FOR'+' '+deal.customer_name__c;    
        opp.OwnerId = '00560000004HPy1';
        opp.discount_program__c = 'DEALREG/PI/PL';
        opp.Abbv_Discount_Program__c =  'DR/PI/PL';  
        opp.StageName = Deal.deal_stage__c;
        opp.CloseDate = Deal.estimated_close_date__c;   
        opp.Primary_Competitor__c = deal.competitor__c;
        opp.type = 'Existing Customer';
        opp.Government_Contract__c = 'None';
        opp.leadsource = 'Deal Registration';
        opp.Partner_Driven__c = 'yes';
        opp.Partner_Account__c = Distributor;
        opp.primary_distributor__c = Distributor;
        //opp.primary_reseller__c = deal.reseller__c;
        opp.primary_reseller__c = Distributor;
        opp.channel_source__c = deal.channel_source__c;
        opp.K_12__c  = deal.K_12__c;
        opp.Renewal_Opportunity__c = 'No';
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'No';    
        opp.DEALREG_SEND_QUOTE__c = 'Do Not Send';
        opp.RENEWAL_SEND_QUOTE__c = 'Do Not Send';  
        opp.Partner_Driven__c  = 'Yes';
        opp.Partner_Led__c = 'Yes';     
        opp.deal_registration__c = deal.id; 
        opp.Partner_Initiated_International__c = 'No';  
        opp.Pricebook2Id    =   CloudPricebookId;                  
   
   insert opp; 
 
  
    
    String CloudProduct = deal.Cloud_Product__c; 
    String CloudServiceTerm = deal.Service_Term__c; 
    String CloudSubscription = deal.Subscription_Term__c;  
          
     
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
    List<PricebookEntry> pbeList = new List<PricebookEntry>();  
    
    
     pbeList = [SELECT Id, UnitPrice, Product2Id, priceBook2Id 
              FROM PricebookEntry 
              WHERE 
              Pricebook2Id = :CloudPricebookId AND 
              ( Product2Id = :CloudProduct or Product2Id = :CloudServiceTerm or Product2Id = :CloudSubscription)  ];
   
   if(pbeList.size() > 0 ){     
    
    for(PricebookEntry pbe: pbeList ){
    
       OpportunityLineItem oli = new OpportunityLineItem();
    
        oli.OpportunityId = opp.id;         
        oli.PricebookEntryId = pbe.id; 
        oli.Quantity = deal.Product_Quantity__c; 
        oli.UnitPrice = pbe.UnitPrice;
        oliList.add(oli);        
     
        }     
                               
        insert oliList; 
      }                                                                             

     
   }
}


Products are not getting added please suggest.

Thanks

Sudhir

Vijay AdusumilliVijay Adusumilli
I would suggest that you verify if you are actually finding any entries in the price book corresponding to your where clause.