0

My controller class is

public class Iapc {

    public boolean detailpage{set;get;}
    public boolean show{set;get;}
        public boolean hide{set;get;}
    public boolean editpage{set;get;}
    public boolean savepage {set;get;}
    public opportunity op{set;get;}
    public integer count{set;get;}
    public boolean app {set;get;}
    public string Fromlimit{set;get;}
    public string Tolimit{set;get;}
    public Boolean aprej {set;get;}
    public Boolean space{get;set;}
    public Boolean newrec{get;set;}
    public string idsparam{get;set;}
    public list<opportunity> opplist{set;get;}
    public Iapc()
    {
     op=new opportunity();

        show=false;
        hide=false;
        editpage= false;
        count=0;
        savepage= false;
        app=false;
        newrec = true;
        aprej = false;
        space = false;

         opplist=[select id,name,Ownerid,StageName,Amount,CloseDate,Discount_offered__c,DiscountApproved__c,Comment__c,Approval_Status__c from opportunity where Approval_Status__c='pending'];

    }
    public void newrecord(){
        newrec = false;
        savepage= true;


    }
    public void savemethod()
    {
        savepage= true;
        editpage=false;
        hide=false;

         app=false;

         List<user> userlist = [Select id,name,userrole.Name,userrole.developername from user];

        for(user uc:userlist)
        {
                  if(UserInfo.getUserId()==uc.Id){
         if(uc.UserRole.developername=='Manager' ) 
         {

           app=false;
            hide=false;

               }


            }

        }

      insert op;  

      if(op.Discount_offered__c>='1500')
      {
          app=true;

      }
        else{ 
        app=false;
            }
    }
 public void approval()
    {
        op.Approval_Status__c='Pending';
        app=false;
        aprej = true;
        editpage=true;
        savepage= false;



        List<user> userlist = [Select id,name,userrole.Name,userrole.developername from user];


        for(user uc:userlist)
        {

            if(UserInfo.getUserId()==uc.Id){
         if(uc.UserRole.developername=='Manager') 
         {

          hide=true; 
             aprej = false;
            space = false;
         }
                }

        }



        update op;

    }
    public void approvestatus()
    {

        space= true;
        hide=false;
        app=false;
        editpage=true;


    }
    public void SaveStatus(){
              if(op.DiscountApproved__c>'1650'){

         apexpages.addMessage(new ApexPages.message(apexpages.Severity.ERROR,'discount should be with in the limit')); 
             space=false;
            savepage= false;
             editpage= false;
                    aprej=false;
        }
        else{
         app=false;
        space= false;
         aprej=true;
        editpage= true;
        hide=false;

        update op;
        }
        }

    public void detailpage(){
    op=[select id,name,Ownerid,StageName,Amount,CloseDate,Discount_offered__c,DiscountApproved__c,Comment__c,Approval_Status__c from opportunity where id=:idsparam];
        editpage= true;

        newrec = false;
        hide=false;

        if(op.Discount_offered__c<'1500')
        {
     app=false;

        }
        else{

             app=true;
        }
        if(op.Approval_Status__c=='Approved'){
            app=false;

        }
         List<user> userlist = [Select id,name,userrole.Name,userrole.developername from user];

        for(user uc:userlist)
        {
            if(UserInfo.getUserId()==uc.Id){
         if(uc.UserRole.developername=='Manager') 
         {

              hide=true;
             app=false;
           if(op.Approval_Status__c=='Approved' || op.Approval_Status__c=='Rejected'){
           app=false;
             hide=false;
             aprej=true;


        }

         }
         }


        }

    }
    public void submiteditpage()
    {


        detailpage=false;
        editpage=true;
        savepage = false;
          hide=false;
        app=false;


        }


}

My VF is

<apex:page controller="Iapc" tabStyle="Opportunity" >

<apex:form id="myfrm">        
    <apex:pageBlock id="ss">
    <apex:pageMessages ></apex:pageMessages>
         <apex:outputPanel rendered="{!newrec}">
        <center>
        <apex:commandButton value="New" action="{!newrecord}" reRender="ss" /></center>
              <apex:pageBlockTable value="{!opplist}" var="op">
            <apex:column headerValue="Name">
            <apex:commandLink value="{!op.name}" action="{!detailpage}" reRender="ss">
                <apex:param value="{!op.id}" assignTo="{!idsparam}"  name="name"/>
                </apex:commandLink>
            </apex:column> </apex:pageBlockTable>


        </apex:outputPanel>


            <apex:pageBlockSection title="Opportunity Information" rendered="{!savepage}" id="pg1">


        <apex:inputfield value="{!op.Name}" />
            <apex:inputField value="{!op.Amount}"/>
             <apex:inputfield value="{!op.CloseDate}"/>  
           <apex:inputfield value="{!op.StageName}"/> 
           <apex:inputField value="{!op.Discount_offered__c}"/> 


                   </apex:pageBlockSection>


        <apex:pageBlockSection rendered="{!savepage}" id="pg2" >
        <apex:commandButton value="Save" Action="{!savemethod}" rerender="ss" style="float: right;"/>
        </apex:pageBlockSection>

                <apex:pageBlockSection title="Opportunity detail Information" rendered="{!editpage}" id="pg3">
             <apex:outputField value="{!op.Name}" />
            <apex:outputField value="{!op.Amount}"/>
             <apex:outputField value="{!op.CloseDate}"/>  
           <apex:outputField value="{!op.StageName}"/> 
           <apex:outputField value="{!op.Discount_offered__c}"/> 
                <apex:outputField value="{!op.DiscountApproved__c}"/> 
                    <apex:outputField value="{!op.Approval_Status__c}"/>
    </apex:pageBlockSection>
            <apex:pageBlockSection id="pg4" >
                 <center>
                     <apex:outputPanel rendered="{!app}"   >
          <apex:commandButton value="submitforApproval" action="{!approval}" rendered="{!If((op.Approval_Status__c==Null),true,false)}" style="float:right;"/>


        </apex:outputPanel> </center>
    </apex:pageBlockSection>



                <apex:outputPanel rendered="{!aprej}">
                    <apex:pageBlockSection title="Approval History"/>
                    <table>
                         <tr>   
                        <td>
                       <apex:outputLabel >Action</apex:outputLabel></td>
                             <td style="width:10%;">

                        <apex:outputLabel >Status</apex:outputLabel></td></tr>

                             <tr>   
                        <td>
                       <apex:outputLabel >Discount Approved</apex:outputLabel></td>
                          </tr>

                        <tr><td>

                       &nbsp;

                            </td><td>
                        <apex:outputtext value="{!op.Approval_Status__c}"/> 
                            </td></tr> </table>  



        </apex:outputPanel>


                <apex:outputPanel rendered="{!hide}">
                    <apex:pageBlockSection title="Approval History"/>
                    <table>
                         <tr>   
                        <td>
                       <apex:outputLabel >Action</apex:outputLabel></td>

                             <td style="width:10%;">

                        <apex:outputLabel >Status</apex:outputLabel></td></tr>
                        <tr><td>

                   <apex:commandLink action="{!approvestatus}" reRender="ss">Approve/Reject</apex:commandLink>
                            </td><td>
                       <apex:outputtext value="{!op.Approval_Status__c}"/> 

                       </td></tr>   
                            </table>


        </apex:outputPanel>
        <apex:outputPanel id="s1" rendered="{!space}">
        <apex:pageBlockSection title="Approval Status">

        <apex:inputfield value="{!op.Approval_Status__c}"/>
              <apex:pageBlockSectionItem >                         

                          <apex:outputPanel >
                              <apex:outputLabel value="Manager Min limit" style="margin-left:150px;" >
                <apex:pageBlockSectionItem >                         
                              <apex:selectList value="{!Fromlimit}" size="1" style="margin-left:27px;" styleClass="ph">
                              <apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
     <apex:selectOption itemLabel="1500" itemvalue="1500"></apex:selectOption>

    </apex:selectList>

        </apex:pageBlockSectionItem>

                              </apex:outputLabel>
                  </apex:outputPanel></apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >                         

                          <apex:outputPanel >
                              <apex:outputLabel value="Manager Max limit" style="margin-left:150px;" >
                <apex:pageBlockSectionItem >                         
                              <apex:selectList value="{!Tolimit}" size="1" style="margin-left:27px;" styleClass="ph">
                              <apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
     <apex:selectOption itemLabel="1650" itemvalue="1650"></apex:selectOption>

    </apex:selectList>

        </apex:pageBlockSectionItem>

                              </apex:outputLabel>
                  </apex:outputPanel></apex:pageBlockSectionItem>
             <apex:inputField value="{!op.DiscountApproved__c}"/> 

        </apex:pageBlockSection>

            <apex:pageBlockSection >
            <apex:commandButton value="Save" action="{!SaveStatus}" style="float:right;" reRender="ss" />
            </apex:pageBlockSection>
             </apex:outputPanel>
        </apex:pageBlock>



    </apex:form>
</apex:page>

My Test class is:

@isTest
public class IApp1test {

    static testmethod void newrecord(){
          Test.StartTest(); 
        opportunity opp=new opportunity(Name='Hello123',Amount=4552001,StageName='Prospecting',CloseDate=date.today(),Discount_offered__c='1500');
        /*opp.Name='Hello123';
        opp.Amount=4552001;
        opp.CloseDate=date.today();
        opp.StageName='Prospecting';
        opp.Discount_offered__c='1500';
        //opp.Approval_Status__c='Pending';*/
        insert opp;

    Iapc opt=new Iapc();

opt.savemethod();

        Test.StopTest();
        }}
1

Please use below code in your test class

@isTest
public class IApp1test {

    static testmethod void newrecord(){
          Test.StartTest(); 
        opportunity opp=new opportunity(Name='Hello123',Amount=4552001,StageName='Prospecting',CloseDate=date.today(),Discount_offered__c='1500');

        Iapc opt=new Iapc();
        opt.op = opp;
        opt.savemethod();
        Test.StopTest();

        List<Opportunity> oppsList = [Select id,name from Opportunity];
        System.assert(oppsList.size() > 0);
}

The reason behind this error is that you are trying to insert opportunity record named op field w/o filling its required details thats why it is giving you an error.

0

Just before inserting the element use the System.debug('The value is: ' + theValue) So that you actually see what you are trying to insert. Then see what are the required fields for the opportunity. And remember that debugging messages are the developers best friend when something breaks and you are not sure why.

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.