• nxkhanh091.392265354033245E12
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Anyone can help me with this please?! I check the query in force.com explorer and get a result but don't know why I got this error. Here are my code
My Apex Class
public with sharing class OrderItemExtension {

    public Order__c theOrder {get;set;}
    public String searchString {get;set;}
    public Order_Line__c [] shoppingCart {get;set;}
    public priceBookEntry[] AvailableProducts {get;set;}
    public Pricebook2 theBook {get;set;} 
  
    public String toSelect {get; set;}
    public String toUnselect {get; set;}
    public Decimal Total {get;set;}
  
    public Boolean overLimit {get;set;}
    public Boolean multipleCurrencies {get; set;}
  
  
    private String qString {get; set;}
    private Boolean forcePricebookSelection = false;
 
      public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }

  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Product2.Family'; } return sortField;  }
    set;
  }
  

  

    private Order_Line__c[] forDeletion = new Order_Line__c[]{};


    public OrderItemExtension(ApexPages.StandardController controller) {

        // Need to know if org has multiple currencies enabled
        multipleCurrencies = UserInfo.isMultiCurrencyOrganization();

        // Get information about the Opportunity being worked on
        if(multipleCurrencies)
            theOrder = database.query('select Id, PricebookId__c, PricebookId__r.name, PricebookId__r.id, CurrencyIsoCode from Order__c where Id = \'' + controller.getRecord().Id + '\' limit 1');
        else
            theOrder = [select Id, CurrencyIsoCode, PricebookId__c, PricebookId__r.name, PricebookId__r.id from Order__c limit 1];
      
        // If products were previously selected need to put them in the "selected products" section to start with
        shoppingCart = [select Id, Front_LED_Colour__c, PricebookEntry__c, Back_LED_Colour__c, Body_Colour__c, On_Request__c, Quantity__c, TotalPrice__c, UnitPrice__c, Description__c, Product__c from Order_line__c where Order__c=:theOrder.Id];

        // Check if Opp has a pricebook associated yet
        if(theOrder.PricebookID__c == null){
            Pricebook2[] activepbs = [select Id, Name from Pricebook2 where isActive = true limit 2];
            if(activepbs.size() == 2){
                forcePricebookSelection = true;
                theBook = new Pricebook2();
            }
            else{
                theBook = activepbs[0];
            }
        }
        else{
            theBook.id = theOrder.PricebookId__r.id;
        }
      
        if(!forcePricebookSelection)
            updateAvailableList();
    }
  
    // this is the 'action' method on the page
    public PageReference priceBookCheck(){
  
        // if the user needs to select a pricebook before we proceed we send them to standard pricebook selection screen
        if(forcePricebookSelection){      
            return changePricebook();
        }
        else{
      
            //if there is only one active pricebook we go with it and save the opp
            if(theOrder.PricebookID__c != theBook.Id){
                try{
                    theOrder.PricebookID__c = theBook.Id;
                    update(theOrder);
                }
                catch(Exception e){
                    ApexPages.addMessages(e);
                }
            }
          
            return null;
        }
    }
     
    public String getChosenCurrency(){
  
        if(multipleCurrencies)
            return (String)theOrder.get('CurrencyIsoCode');
        else
            return '';
    }

    public void updateAvailableList() {
  
        // We dynamically build a query string and exclude items already in the shopping cart
        qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where IsActive=true and Pricebook2Id = \'' + theBook.Id + '\'';
        if(multipleCurrencies)
            qstring += ' and CurrencyIsoCode = \'' + theOrder.get('currencyIsoCode') + '\'';
      
        // note that we are looking for the search string entered by the user in the name OR description
        // modify this to search other fields if desired
        if(searchString!=null){
            qString+= ' and (Product2.Name like \'%' + searchString + '%\' or Product2.Description like \'%' + searchString + '%\')';
        }
      
        Set<Id> selectedEntries = new Set<Id>();
        for(Order_Line__c d:shoppingCart){
            selectedEntries.add(d.Product__c);
        }
      
        if(selectedEntries.size()>0){
            String tempFilter = ' and Id not in (';
            for(Id i : selectedEntries){
                tempFilter+= '\'' + (String)i + '\',';
            }
            String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
            extraFilter+= ')';
          
            qString+= extraFilter;
        }
      
       qString+= ' order by '+sortField+' '+sortDir;
        qString+= ' limit 101';
      
        system.debug('qString:' +qString);      
        AvailableProducts = database.query(qString);
      
        // We only display up to 100 results... if there are more than we let the user know (see vf page)
        if(AvailableProducts.size()==101){
            AvailableProducts.remove(100);
            overLimit = true;
        }
        else{
            overLimit=false;
        }
    }
  
  
  
  
  
     // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query ag
      updateAvailableList();
  }

  // runs the actual query

  
  
  
  
  
    public void addToShoppingCart(){
  
        // This function runs when a user hits "select" button next to a product
  
        for(PricebookEntry d : AvailableProducts){
            if((String)d.Id==toSelect){
                shoppingCart.add(new Order_Line__c(Order__c=theOrder.Id, Product__c=d.Product2.name, UnitPrice__c=d.UnitPrice));
                break;
            }
        }
      
        updateAvailableList();
    }
  

    public PageReference removeFromShoppingCart(){
  
        // This function runs when a user hits "remove" on an item in the "Selected Products" section
  
        Integer count = 0;
  
        for(Order_Line__c d : shoppingCart){
            if((String)d.Product__c==toUnselect){
          
                if(d.Id!=null)
                    forDeletion.add(d);
          
                shoppingCart.remove(count);
                break;
            }
            count++;
        }
      
        updateAvailableList();
      
        return null;
    }
  
    public PageReference onSave(){
  
        // If previously selected products are now removed, we need to delete them
        if(forDeletion.size()>0)
            delete(forDeletion);
  
        // Previously selected products may have new quantities and amounts, and we may have new products listed, so we use upsert here
        try{
            if(shoppingCart.size()>0)
                upsert(shoppingCart);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
            return null;
        }
         
        // After save return the user to the Order
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    }
  
    public PageReference onCancel(){

        // If user hits cancel we commit no changes and return them to the Order 
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    }
  
    public PageReference changePricebook(){
  
        // This simply returns a PageReference to the standard Pricebook selection screen
        // Note that is uses retURL parameter to make sure the user is sent back after they choose
  
        PageReference ref = new PageReference('/oppitm/choosepricebook.jsp');
        ref.getParameters().put('id',theOrder.Id);
        ref.getParameters().put('retURL','/apex/OrderProductEntry?id=' + theOrder.Id);
      
        return ref;
    }
}
I did try the query in Force.com Explore and get the result whoever when I ran the test this error comes up.  Please help!!!
Here is my code:

@istest
private class QuoteProductEntryTests {

    static testMethod void theTests(){
      
          
        QuoteLineItem qli = [select Id, PricebookEntryId, PriceBookEntry.Product2Id, QuoteId, Quote.OpportunityId from QuoteLineItem limit 1];
             
              
        ////////////////////////////////////////
        //  test QuoteProductEntry
        ////////////////////////////////////////
      
        // load the page     
        PageReference pageRef = Page.QuoteProductEntry;
        pageRef.getParameters().put('Id',qli.QuoteId);
        Test.setCurrentPageReference(pageRef);
      
        // load the extension
        QuoteitemExtension oPEE = new QuoteItemExtension(new ApexPages.StandardController(qli.Quote));
      
        // test 'getChosenCurrency' method
        if(UserInfo.isMultiCurrencyOrganization())
            System.assert(oPEE.getChosenCurrency()!='');
        else
            System.assertEquals(oPEE.getChosenCurrency(),'');

        // we know that there is at least one line item, so we confirm
        Integer startCount = oPEE.ShoppingCart.size();
        system.assert(startCount>0);

        //test search functionality without finding anything
        oPEE.searchString = 'michaelforce is a hip cat';
        oPEE.updateAvailableList();
        system.assert(oPEE.AvailableProducts.size()==0);
      
        //test remove from shopping cart
        oPEE.toUnselect = qli.PricebookEntryId;
        oPEE.removeFromShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount-1);
      
        //test save and reload extension
        oPEE.onSave();
        oPEE = new QuoteItemExtension(new ApexPages.StandardController(qli.Quote));
        system.assert(oPEE.shoppingCart.size()==startCount-1);
      
        // test search again, this time we will find something
        oPEE.searchString = qli.PricebookEntry.Name;
        oPEE.updateAvailableList();
        system.assert(oPEE.AvailableProducts.size()>0);     

        // test add to Shopping Cart function
        oPEE.toSelect = oPEE.AvailableProducts[0].Id;
        oPEE.addToShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount);
              
        // test save method - WITHOUT quanitities and amounts entered and confirm that error message is displayed
        oPEE.onSave();
        system.assert(ApexPages.getMessages().size()>0);
      
        // add required info and try save again
        for(QuoteLineItem o : oPEE.ShoppingCart){
            o.quantity = 5;
            o.unitprice = 300;
        }
        oPEE.onSave();
      
        // query line items to confirm that the save worked
        QuoteLineItem[] qli2 = [select Id from QuoteLineItem where QuoteId = :qli.QuoteId];
        system.assert(qli2.size()==startCount);
      
        // test on new Opp (no pricebook selected) to make sure redirect is happening
        Quote newQuote = new Quote(Name='New quote', OpportunityId=qli.Quote.OpportunityId);
        insert(newQuote);
        oPEE = new QuoteItemExtension(new ApexPages.StandardController(newQuote));
        System.assert(oPEE.priceBookCheck()!=null);
      
        // final quick check of cancel button
        System.assert(oPEE.onCancel()!=null);
      
      
        ////////////////////////////////////////
        //  test redirect page
        ////////////////////////////////////////
      
        // load the page

   
    }
}
I got the visualforce pages to create new contact & account seperately but I can't merge it together due to the Contact account look up field. I would really appreciate if anyone could help me with this.
here is my controller:
public class controllermethod
{    public account account{get;set;}
    public contact contact{get;set;}

    public controllermethod()
    {
        account=new account();
        contact=new contact();
    }
public PageReference accsave() {
    insert  account;
       account = new account();
                       ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.INFO, 'The company was added');
            ApexPages.addmessage(msgErr);
           return null;
    }
          public PageReference consave() {
    insert  contact;
       contact = new contact();
                       ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.INFO, 'The contact was added');
            ApexPages.addmessage(msgErr);
           return null;
    }
   
           public PageReference conacc() {
        accsave();  

         consave();
   
      
           return null;
    }}

Thanks before hand
Anyone can help me with this please?! I check the query in force.com explorer and get a result but don't know why I got this error. Here are my code
My Apex Class
public with sharing class OrderItemExtension {

    public Order__c theOrder {get;set;}
    public String searchString {get;set;}
    public Order_Line__c [] shoppingCart {get;set;}
    public priceBookEntry[] AvailableProducts {get;set;}
    public Pricebook2 theBook {get;set;} 
  
    public String toSelect {get; set;}
    public String toUnselect {get; set;}
    public Decimal Total {get;set;}
  
    public Boolean overLimit {get;set;}
    public Boolean multipleCurrencies {get; set;}
  
  
    private String qString {get; set;}
    private Boolean forcePricebookSelection = false;
 
      public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }

  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Product2.Family'; } return sortField;  }
    set;
  }
  

  

    private Order_Line__c[] forDeletion = new Order_Line__c[]{};


    public OrderItemExtension(ApexPages.StandardController controller) {

        // Need to know if org has multiple currencies enabled
        multipleCurrencies = UserInfo.isMultiCurrencyOrganization();

        // Get information about the Opportunity being worked on
        if(multipleCurrencies)
            theOrder = database.query('select Id, PricebookId__c, PricebookId__r.name, PricebookId__r.id, CurrencyIsoCode from Order__c where Id = \'' + controller.getRecord().Id + '\' limit 1');
        else
            theOrder = [select Id, CurrencyIsoCode, PricebookId__c, PricebookId__r.name, PricebookId__r.id from Order__c limit 1];
      
        // If products were previously selected need to put them in the "selected products" section to start with
        shoppingCart = [select Id, Front_LED_Colour__c, PricebookEntry__c, Back_LED_Colour__c, Body_Colour__c, On_Request__c, Quantity__c, TotalPrice__c, UnitPrice__c, Description__c, Product__c from Order_line__c where Order__c=:theOrder.Id];

        // Check if Opp has a pricebook associated yet
        if(theOrder.PricebookID__c == null){
            Pricebook2[] activepbs = [select Id, Name from Pricebook2 where isActive = true limit 2];
            if(activepbs.size() == 2){
                forcePricebookSelection = true;
                theBook = new Pricebook2();
            }
            else{
                theBook = activepbs[0];
            }
        }
        else{
            theBook.id = theOrder.PricebookId__r.id;
        }
      
        if(!forcePricebookSelection)
            updateAvailableList();
    }
  
    // this is the 'action' method on the page
    public PageReference priceBookCheck(){
  
        // if the user needs to select a pricebook before we proceed we send them to standard pricebook selection screen
        if(forcePricebookSelection){      
            return changePricebook();
        }
        else{
      
            //if there is only one active pricebook we go with it and save the opp
            if(theOrder.PricebookID__c != theBook.Id){
                try{
                    theOrder.PricebookID__c = theBook.Id;
                    update(theOrder);
                }
                catch(Exception e){
                    ApexPages.addMessages(e);
                }
            }
          
            return null;
        }
    }
     
    public String getChosenCurrency(){
  
        if(multipleCurrencies)
            return (String)theOrder.get('CurrencyIsoCode');
        else
            return '';
    }

    public void updateAvailableList() {
  
        // We dynamically build a query string and exclude items already in the shopping cart
        qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where IsActive=true and Pricebook2Id = \'' + theBook.Id + '\'';
        if(multipleCurrencies)
            qstring += ' and CurrencyIsoCode = \'' + theOrder.get('currencyIsoCode') + '\'';
      
        // note that we are looking for the search string entered by the user in the name OR description
        // modify this to search other fields if desired
        if(searchString!=null){
            qString+= ' and (Product2.Name like \'%' + searchString + '%\' or Product2.Description like \'%' + searchString + '%\')';
        }
      
        Set<Id> selectedEntries = new Set<Id>();
        for(Order_Line__c d:shoppingCart){
            selectedEntries.add(d.Product__c);
        }
      
        if(selectedEntries.size()>0){
            String tempFilter = ' and Id not in (';
            for(Id i : selectedEntries){
                tempFilter+= '\'' + (String)i + '\',';
            }
            String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
            extraFilter+= ')';
          
            qString+= extraFilter;
        }
      
       qString+= ' order by '+sortField+' '+sortDir;
        qString+= ' limit 101';
      
        system.debug('qString:' +qString);      
        AvailableProducts = database.query(qString);
      
        // We only display up to 100 results... if there are more than we let the user know (see vf page)
        if(AvailableProducts.size()==101){
            AvailableProducts.remove(100);
            overLimit = true;
        }
        else{
            overLimit=false;
        }
    }
  
  
  
  
  
     // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query ag
      updateAvailableList();
  }

  // runs the actual query

  
  
  
  
  
    public void addToShoppingCart(){
  
        // This function runs when a user hits "select" button next to a product
  
        for(PricebookEntry d : AvailableProducts){
            if((String)d.Id==toSelect){
                shoppingCart.add(new Order_Line__c(Order__c=theOrder.Id, Product__c=d.Product2.name, UnitPrice__c=d.UnitPrice));
                break;
            }
        }
      
        updateAvailableList();
    }
  

    public PageReference removeFromShoppingCart(){
  
        // This function runs when a user hits "remove" on an item in the "Selected Products" section
  
        Integer count = 0;
  
        for(Order_Line__c d : shoppingCart){
            if((String)d.Product__c==toUnselect){
          
                if(d.Id!=null)
                    forDeletion.add(d);
          
                shoppingCart.remove(count);
                break;
            }
            count++;
        }
      
        updateAvailableList();
      
        return null;
    }
  
    public PageReference onSave(){
  
        // If previously selected products are now removed, we need to delete them
        if(forDeletion.size()>0)
            delete(forDeletion);
  
        // Previously selected products may have new quantities and amounts, and we may have new products listed, so we use upsert here
        try{
            if(shoppingCart.size()>0)
                upsert(shoppingCart);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
            return null;
        }
         
        // After save return the user to the Order
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    }
  
    public PageReference onCancel(){

        // If user hits cancel we commit no changes and return them to the Order 
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    }
  
    public PageReference changePricebook(){
  
        // This simply returns a PageReference to the standard Pricebook selection screen
        // Note that is uses retURL parameter to make sure the user is sent back after they choose
  
        PageReference ref = new PageReference('/oppitm/choosepricebook.jsp');
        ref.getParameters().put('id',theOrder.Id);
        ref.getParameters().put('retURL','/apex/OrderProductEntry?id=' + theOrder.Id);
      
        return ref;
    }
}
I did try the query in Force.com Explore and get the result whoever when I ran the test this error comes up.  Please help!!!
Here is my code:

@istest
private class QuoteProductEntryTests {

    static testMethod void theTests(){
      
          
        QuoteLineItem qli = [select Id, PricebookEntryId, PriceBookEntry.Product2Id, QuoteId, Quote.OpportunityId from QuoteLineItem limit 1];
             
              
        ////////////////////////////////////////
        //  test QuoteProductEntry
        ////////////////////////////////////////
      
        // load the page     
        PageReference pageRef = Page.QuoteProductEntry;
        pageRef.getParameters().put('Id',qli.QuoteId);
        Test.setCurrentPageReference(pageRef);
      
        // load the extension
        QuoteitemExtension oPEE = new QuoteItemExtension(new ApexPages.StandardController(qli.Quote));
      
        // test 'getChosenCurrency' method
        if(UserInfo.isMultiCurrencyOrganization())
            System.assert(oPEE.getChosenCurrency()!='');
        else
            System.assertEquals(oPEE.getChosenCurrency(),'');

        // we know that there is at least one line item, so we confirm
        Integer startCount = oPEE.ShoppingCart.size();
        system.assert(startCount>0);

        //test search functionality without finding anything
        oPEE.searchString = 'michaelforce is a hip cat';
        oPEE.updateAvailableList();
        system.assert(oPEE.AvailableProducts.size()==0);
      
        //test remove from shopping cart
        oPEE.toUnselect = qli.PricebookEntryId;
        oPEE.removeFromShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount-1);
      
        //test save and reload extension
        oPEE.onSave();
        oPEE = new QuoteItemExtension(new ApexPages.StandardController(qli.Quote));
        system.assert(oPEE.shoppingCart.size()==startCount-1);
      
        // test search again, this time we will find something
        oPEE.searchString = qli.PricebookEntry.Name;
        oPEE.updateAvailableList();
        system.assert(oPEE.AvailableProducts.size()>0);     

        // test add to Shopping Cart function
        oPEE.toSelect = oPEE.AvailableProducts[0].Id;
        oPEE.addToShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount);
              
        // test save method - WITHOUT quanitities and amounts entered and confirm that error message is displayed
        oPEE.onSave();
        system.assert(ApexPages.getMessages().size()>0);
      
        // add required info and try save again
        for(QuoteLineItem o : oPEE.ShoppingCart){
            o.quantity = 5;
            o.unitprice = 300;
        }
        oPEE.onSave();
      
        // query line items to confirm that the save worked
        QuoteLineItem[] qli2 = [select Id from QuoteLineItem where QuoteId = :qli.QuoteId];
        system.assert(qli2.size()==startCount);
      
        // test on new Opp (no pricebook selected) to make sure redirect is happening
        Quote newQuote = new Quote(Name='New quote', OpportunityId=qli.Quote.OpportunityId);
        insert(newQuote);
        oPEE = new QuoteItemExtension(new ApexPages.StandardController(newQuote));
        System.assert(oPEE.priceBookCheck()!=null);
      
        // final quick check of cancel button
        System.assert(oPEE.onCancel()!=null);
      
      
        ////////////////////////////////////////
        //  test redirect page
        ////////////////////////////////////////
      
        // load the page

   
    }
}
I got the visualforce pages to create new contact & account seperately but I can't merge it together due to the Contact account look up field. I would really appreciate if anyone could help me with this.
here is my controller:
public class controllermethod
{    public account account{get;set;}
    public contact contact{get;set;}

    public controllermethod()
    {
        account=new account();
        contact=new contact();
    }
public PageReference accsave() {
    insert  account;
       account = new account();
                       ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.INFO, 'The company was added');
            ApexPages.addmessage(msgErr);
           return null;
    }
          public PageReference consave() {
    insert  contact;
       contact = new contact();
                       ApexPages.Message msgErr = new ApexPages.Message(ApexPages.Severity.INFO, 'The contact was added');
            ApexPages.addmessage(msgErr);
           return null;
    }
   
           public PageReference conacc() {
        accsave();  

         consave();
   
      
           return null;
    }}

Thanks before hand