+ Start a Discussion
theoptheop 

Invalid conversion from runtime type String to SOBJECT:Opportunity

I've created a VF page to add multiple products based of http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html

 

Everything works execpt when I try to Save I get the error: common.apex.runtime.impl.ExecutionException: Invalid conversion from runtime type String to SOBJECT:Opportunity

 
But, I'm not assign the opportunity to anything and System.debug don't get trigger in the Save function. Any help will greatly appriciated! Thanks
 
<apex:page standardController="Product2" extensions="CustomProducts" tabStyle="Opportunity">
<apex:sectionHeader title="Custom Messaging Products" subtitle="{!oppSO.Name}"/>
<apex:form >
     <apex:inputHidden value="{!oppSO}"/>
    
      <apex:pageBlock title="New Message Tier Products">
          
          <apex:pageMessages />
              
          <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
                       <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
             <apex:column headerValue="Min">
                <apex:inputField value="{!wrapper.p.Min__c}"/>
             </apex:column>
             <apex:column headerValue="Max">
                <apex:inputField value="{!wrapper.p.Max__c}"/>
             </apex:column>
             <apex:column headerValue="Action">
                <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable" immediate="true">
                   <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
                </apex:commandButton>
             </apex:column>
          </apex:pageBlockTable>
          
          <apex:pageBlockButtons >
              <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable" immediate="true">
                  <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
              </apex:commandButton>
              <apex:commandButton value="Save" action="{!save}"/>
              <apex:commandButton value="Cancel" action="{!cancel}"/>
          </apex:pageBlockButtons>
      </apex:pageBlock>
      
    </apex:form>
</apex:page>

 

public class CustomProducts {
    
    public Opportunity oppSO {get;set;}
    public List<Product2WrapperCustomMsg> wrappers {get;set;}
    public static Integer toDelIdent {get; set;}
	public static Integer addCount {get; set;}
 	private Integer nextIdent;
        
    static final String STR_CUSTOM_LOOLKUP_PRE = 'SB-US-MSGCUSTOM';
    static final String STR_CUSTOM_LOOLKUP_POST = '-USAGE-MT';
    static final String LKP_QBFAMILY = '44100 · Sales Revenue - Standard Rate M';
        
    //
    // Contructor
    //
    public CustomProducts(ApexPages.StandardController controller) {
        String oppId = (String)Apexpages.currentPage().getParameters().get('oppid');
        this.oppSO = [Select Id, Name From Opportunity Where Id = :oppId];
        System.debug('@@@this.oppSO: ' + this.oppSO);
        
        wrappers = new List<Product2WrapperCustomMsg>();
        for (Integer idx = 0; idx < 4; idx++) 
        {
            if (nextIdent == null)
                nextIdent = this.GetNextIncrement();
            else
                nextIdent++;
            
            wrappers.add(new Product2WrapperCustomMsg(nextIdent));
            
        }

    }
    
    public Integer GetNextIncrement()
    {
        Integer currentMax = 0;
        for(Product2 p:[Select Id, Name, ProductCode from Product2 WHERE ProductCode LIKE :STR_CUSTOM_LOOLKUP_PRE + '%'])
        {	
            //SB-US-MSGCUSTOM1-USAGE-MT
            String tempCode = p.ProductCode.substring(STR_CUSTOM_LOOLKUP_PRE.length()); 
            tempCode = tempCode.substring(0, tempCode.indexOf('-')); 
            
            if (integer.valueof(tempCode) > currentMax )
                currentMax = integer.valueof(tempCode);
            
        } //for
        
        return currentMax + 1;
    } // GetNextIncrement
    
    public PageReference save()
    {
            System.debug('@@@ CustomProducts.save.wrappers : ' + wrappers );
        List<Product2> prds = new List<Product2>();
        for (Product2WrapperCustomMsg wrap : wrappers)
        {
            wrap.p.Name = GetProductName(wrap.p.Min__c, wrap.p.Max__c);
            prds.add(wrap.p);
            System.debug('@@@ CustomProducts.save.wrap.p : ' + wrap.p );
        }
        
        insert prds;
        
        return new PageReference('/006V0000002zPVi');
        
        //return new PageReference('/' + Schema.getGlobalDescribe().get('Opportunity').getDescribe().getKeyPrefix() + '/o');
    } // save
    
    public void delWrapper() {
        Integer toDelPos=-1;
        for (Integer idx=0; idx<wrappers.size(); idx++) {
            if (wrappers[idx].ident==toDelIdent) {
                toDelPos=idx;
            }
        }
        
        if (-1!=toDelPos) {
            wrappers.remove(toDelPos);
        }
    } // delWrapper
    
    public void addRows() {
        nextIdent = nextIdent + 1;
        wrappers.add(new Product2WrapperCustomMsg(nextIdent));
        System.debug('@@@ CustomProducts.addRows.wrappers: ' + wrappers);
    }
    
    // Format: Message Plan - Up to 100,000 Messages - Mobile Terminated (MT)
    // Format: Message Plan - 500,000-1,000,000 Messages - Mobile Terminated (MT)
    // Format: Message Plan - 1,000,000 and Above Messages - Mobile Terminated (MT)
    public String GetProductName(Decimal min, Decimal max)
    {
        String retString = '';
        
        if (min == 0 || min == 1) {
            retString = 'Message Plan - Up to ' + max + ' Messages - Mobile Terminated (MT)';
        }
        else if (max == 999999999) {
            retString = 'Message Plan - ' + min + ' and Above Messages - Mobile Terminated (MT)';
        }
        else {
            retString = 'Message Plan - ' + min + '-' + max + ' Messages - Mobile Terminated (MT)';
        }
        System.debug('@@@ Product2WrapperCustomMsg.GetProductName.retString : ' + retString );
        return retString ;
        
    } // GetProductName
    
    public class Product2WrapperCustomMsg
    {
        public Product2 p {get; private set;}
        public Integer ident {get; private set;}
       
        public Product2WrapperCustomMsg(Integer inIdent)
        {
			ident = inIdent;
            
            p = new Product2();
            //p.Name = CustomProducts.GetProductName(0, 999999999);
            p.ProductCode = STR_CUSTOM_LOOLKUP_PRE  + inIdent + STR_CUSTOM_LOOLKUP_POST;
            p.Min__c = 0;
            p.Max__c = 999999999;
            p.QB_Do_Sync__c = true;
            p.QB_AccountFamily__c = LKP_QBFAMILY;
            p.Message_Product_Id__c = inIdent;
            p.CanUseQuantitySchedule = true;
            p.Family = 'Message Plan Tiered';
        }
    } // Product2WrapperCustomMsg
}

 

sivaextsivaext

Hi

 

are you using above code to create multiple products at a time? then what is use of the below lines of code

 

String oppId = (String)Apexpages.currentPage().getParameters().get('oppid');
        this.oppSO = [Select Id, Name From Opportunity Where Id = :oppId];
        System.debug('@@@this.oppSO: ' + this.oppSO);

 

theoptheop

Thanks for replying. I fixed the issue by removing.

 

<apex:inputHidden value="{!oppSO}"/>