function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

SOQL not returning value to variable

Hi, 

  I wrote a trigger on a custom object which will conterts data into Account Opportunity and Opportunity Products 

  It is converting into account and opportunity but not able to add products into opportunity product 

  String SOQLCloudProduct = deal.AP_Product__c; //Product Name
  CloudProduct = [SELECT ID FROM Product2 where name = :SOQLCloudProduct limit 1 ].ID; // To get Product ID 

   Above SOQL is not working please suggest me how to fix 

Complete trigger is 
 
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();     
    List<OpportunityLineItem> oliList  = new List<OpportunityLineItem>();
    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 ];
 
 
 System.debug(LoggingLevel.INFO,'Start Testing my trigger');
  
 
  
  String Distributor;   
  String Region = deal.theater__c;
  
  /* Assign Opportunity owner */
  
  if ( Region == 'NAM') 
  {
     OppOwner  = '00560000004HPy1';
   }
  else if ( Region == 'EMEA')
  {   
     OppOwner = '00560000003W8gx';
   }
     
  
  /* Assign distributor */
  if ( deal.AP_Product__c <> NULL && deal.Service_Term__c <> NULL )                 
   {
   Distributor =  deal.distributor__c ;    
   }                     
  else if ( deal.Subscription_Term__c <> NULL && deal.theater__c == 'NAM'  ) 
   {
    Distributor = '0016000000pyLll'; // defaulting to westcon
   }
  else if ( deal.Subscription_Term__c <> NULL && deal.theater__c == 'EMEA'  )
   { 
   Distributor = '0016000000YmDkh'; //defaulting to zyko
   }

  System.debug(LoggingLevel.INFO,Distributor );

  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.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.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';                      
   
   insert opp; 
   
    
    String priceBookName = 'Standard'; 
    String SOQLCloudProduct = deal.AP_Product__c;
	String SOQLCloudServiceTerm = deal.Service_Term__c;
	String SOQLCloudSubscription = deal.Subscription_Term__c; 
    String CloudProduct;
    String CloudServiceTerm;
    String CloudSubscription;
    
    if ( SOQLCloudProduct <> NULL )
    {
    CloudProduct = [SELECT ID FROM Product2 where name = :SOQLCloudProduct limit 1 ].ID;
    system.debug(CloudProduct);
    }   
    
    if ( SOQLCloudServiceTerm <> NULL ) 
    {
    CloudServiceTerm = [SELECT ID FROM Product2 where name = :SOQLCloudServiceTerm limit 1 ].ID;
    system.debug(CloudServiceTerm);
    }
    
    if ( CloudSubscription <> NULL ) 
    {
    
    CloudSubscription = [SELECT ID FROM Product2 where name = :SOQLCloudSubscription limit 1 ].ID;
    system.debug(CloudSubscription);
    }
    
    String CloudPricebookId;
    
    
    IF ( Region == 'NAM') 
    {
      CloudPricebookId = '01s60000000AKxZAAW';
    }
    ELSE IF ( Region == 'EMEA') 
    {
      CloudPricebookId = '01s60000000AKxUAAW';
    }  
      
    List<PricebookEntry>  pbeList  = new List<PricebookEntry>();
    
       
     pbeList = [Select ProductCode, UnitPrice, Product2Id, priceBook2Id From PricebookEntry 
                          where ( Product2Id = :CloudProduct or  Product2Id = :CloudServiceTerm or Product2Id = :CloudSubscription )
                                and priceBook2Id  = :CloudPricebookId];   
     
      system.debug(pbeList);
      
    for(PricebookEntry pbe: pbeList){
         OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.id; 
        //oppprod.Product2 = pbe.Product2Id;
        //oppprod.ProductCode = pbe.Product2.ProductCode;
        oli.PricebookEntryId = pbe.id; 
        oli.Quantity = deal.Product_Quantity__c; 
        oli.UnitPrice = pbe.UnitPrice;
        oliList.add(oli);
      }  

   insert oliList;
   }
}

Thanks
Sudhir

 
NekosanNekosan
Looks like AP_Product__c is a id. You need to get Name instead of id and then put that in query. 
Priya GovindasamyPriya Govindasamy
SOQL return type is always List of sobject. Cloudproduct must be the same type as the SObject returned by the SOQL query. Example

Account s = [SELECT ID, Name FROM Account LIMIT 1];

Here variable s is of type Account which is the return type of SOQL query.

In your code cloudproduct is of type string which is not a valid SOQL return type.

If this helps.. Pl mark it as best answer.