• Waqas Ali
  • NEWBIE
  • 114 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 3
    Likes Received
  • 0
    Likes Given
  • 19
    Questions
  • 22
    Replies

I want to get all the records within the last 24-hour according to LastModifiedDate , Here is what I am doing

global class SalesOrderSchedule implements Schedulable
{
    global static String CRON_EXP = '0 0 0   ? *'; 

    global void execute(SchedulableContext sc)
    {
        doOpportunityUpdates();
    }

    global void doOpportunityUpdates()
    {
        List<Opportunity> opps = new List<Opportunity>();
        DateTime dt_lastDay = System.now()-1;
        DateTime dt_currentDay = System.now();
        for(Sales_Order__c salesorder : [select id, Name, LastModifiedDate from Sales_Order__c WHERE LastModifiedDate  > = :dt_lastDay AND LastModifiedDate < :dt_currentDay ] )
        {
         //my functionality    
    }
}
but this query is not working, Guys help
I have a Schedulableclass which is scheduled to execute every night at 12:00 AM,  here is my class, 
global class SalesOrderSchedule implements Schedulable
{

global static void checkrun()
    {
 String day = string.valueOf(system.now().day());
                String month = string.valueOf(system.now().month());
                String hour = string.valueOf(system.now().hour());
                String minute = string.valueOf(system.now().minute());
                String second = string.valueOf(system.now().second());
                String year = string.valueOf(system.now().year());

                String JobName = 'Scheduled Job of sales Order';
                String strSchedule = second +' ' + minute+' '+ hour+' '+ day+' '+month+' ?'+' '+ year;
                system.schedule(JobName, strSchedule, new SalesOrderSchedule());    
    }    
  global void execute(SchedulableContext sc)
  {
  Opportunity opp;
    
  for (Sales_Order__c salesorders : [select id, Name, LastModifiedDate,    Quote__c, OrderNum__c from Sales_Order__c WHERE LastModifiedDate  >= LAST_N_DAYS:1])
         {
             String OppNewStage=null;
             if (salesorders.OrderNum__c=='QUOTE WON')
             {
             OppNewStage='Closed Won';
              }
             else if (salesorders.OrderNum__c=='QUOTE LOST')
             {
             OppNewStage='Closed Lost';
             }
             else 
             {
              OppNewStage='Quoted';
             }
             
       opp=[select id, Name, StageName from Opportunity where id=:salesorders.Quote__c]; 
     opp.StageName=OppNewStage;
     update opp;
     
    
     }
     

   
  }
}

here is my test class,  my code coverage is 13%. 
@isTest
public class SalesOrderScheduleTest {
    static testMethod void myTestMethod() {        
         test.starttest();
     SalesOrderSchedule.checkrun();
     
   String chron = '0 0 12 15 9 ? 2015' ;        
         system.schedule('job', chron, new SalesOrderSchedule());
         test.stopTest();
    }
}

 

Static checkrun method is not covering, how to increase the code coverage ?? Guys help 

I have integrated my heroku apps in sales force using canvus, every thing was fine , apps were wroking fine in salesforce, but now when i tried to acces my apps in salesforce it's giving the following error 

Authentication Failed: OAuth login invalid or expired access token error

 

what can be the possible reason of this issue, Guys help. 

Here is my apex class copying the opportunity line items of current opportunity to newly created order and opportunity. I do not know how to write unit test for opportunity line items,
String theId = ApexPages.currentPage().getParameters().get('id');
 List<OpportunityLineItem> currentOppLI = [
                                SELECT PricebookEntryId, Quantity, UnitPrice,
                                       TotalPrice, ListPrice, Description
                                FROM OpportunityLineItem
                                WHERE OpportunityId = :theId];
 //Create the Order
                    Order odr = new Order(  
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    ,Pricebook2Id = o.Pricebook2Id
                    );
                insert odr;
                //copy Opportunity line items to Order line items
                  List<OrderItem> newOrderProductsList = new List<OrderItem>();
    
        for (OpportunityLineItem item : currentOppLI) {
             newOrderProductsList.add(new OrderItem(
                    PricebookEntryId = item.PricebookEntryId,
                    Quantity = item.Quantity,
                    UnitPrice = item.UnitPrice,
                    Description = item.Description,
                    OrderId = odr.Id));
         }
             insert newOrderProductsList;  

 List<OpportunityLineItem> newOppLI = new List<OpportunityLineItem>();
        
        for (OpportunityLineItem item : currentOppLI) {
             newOppLI.add(new OpportunityLineItem(
             PricebookEntryId = item.PricebookEntryId,   
             Quantity = item.Quantity,
             UnitPrice = item.UnitPrice,
             Description = item.Description,
             OpportunityId = opp.Id));
         }
            
    insert newOppLI;   


//opp is newly created opportunity and odr is newly created order
How to write unit test for opportunity line items and order items??
 
Here is my apex class.
public class myclass{
 
    
    public Opportunity o;
    String theId = ApexPages.currentPage().getParameters().get('id');
  
    
   @TestVisible public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
  
  public  myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
         if (theId == null) {
            return null;
        }
    
records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'  ])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated')); //this line is not getting covered
 }
     return null;
          
}
 public PageReference MainMethod() {
 
/// other code
    
        return null;        
  }


// To Update contracts 
 public PageReference back() {
       List<Contract> contractToBeUpdate = new List<Contract>();
    for(WrapperSObject wrapper : records){
        wrapper.con.status = wrapper.selectedValue;
        contractToBeUpdate.add(wrapper.con);
    }
    update contractToBeUpdate;
    PageReference pageRef ;    
     return pageRef= MainMethod();
  }
 


@TestVisible public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
  }

Here is my test class
@isTest
private class myclassTest {
    private static testMethod void testAutoRun() {

    
        Account acc = new Account(Name='Abce');
  insert acc;
 
  Opportunity  testOppty = new Opportunity();
  testOppty.name='testOppty';
  testOppty.AccountId=acc.id;
  testOppty.CloseDate=System.today();
  testOppty.StageName= 'Closed Won';
  testOppty.Type='EnerLead Renewal';
  testOppty.Description='Newly created Oppertunity';
  insert testOppty;
  
        insert odr;
                  Contract c = new Contract(
                ,StartDate=testOppty.CloseDate
                ,AccountId = testOppty.AccountId
                ,Name = testOppty.Name
                );
            insert c;
  
 List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        
  
        
        test.startTest();
        
            PageReference pageRef = Page.myclass;
            Test.setCurrentPage(pageRef);
            
            pageRef.getParameters().put('id',testOppty.id);
            ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
            
            myclass  controller = new myclass(sc);
            controller.autoRun();
            controller.back();
            controller.MainMethod();
            myclass.WrapperSObject psec = new myclass.WrapperSObject( con, options, 'Activated' );
        
       test.stopTest();
}
}

I am unable to cover the Selection getoption() method to increse code coverage. How to write unit test for selection method ?

records.add(new WrapperSObject(c, getOptions(), 'Activated')); //this line is also not getting covered
 
Here is my apex class
public class myclass{
 
    
    public Opportunity o;
    String theId = ApexPages.currentPage().getParameters().get('id');
  
    
   @TestVisible public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
  
  public  myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
         if (theId == null) {
            return null;
        }
    
records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'  ])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }
     return null;
          
}
 public PageReference MainMethod() {
 
  try{
     //Create new Oppertunity
             Opportunity opp = new Opportunity(
                     Name = 'TESTACCOUNT_test july' +'Date.today()'
                     ,CloseDate=Date.today()
                     ,StageName= 'Renewal'
                     ,Type='EnerLead Renewal'
                     ,Description='Newly created Oppertunity' 
                );
            insert opp;
   
//try section     
}catch(QueryException e){
}catch(DMLException e){}
    
        return null;        
  }


// To Update contracts 
 public PageReference back() {
       List<Contract> contractToBeUpdate = new List<Contract>();
    for(WrapperSObject wrapper : records){
        wrapper.con.status = wrapper.selectedValue;
        contractToBeUpdate.add(wrapper.con);
    }
    update contractToBeUpdate;
    PageReference pageRef ;    
     return pageRef= MainMethod();
  }
 


@TestVisible public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
  }

here is my test class
@isTest
private class myclassTest {
    private static testMethod void testAutoRun() {

    
        Account acc = new Account(Name='Abce');
  insert acc;
 
  Opportunity  testOppty = new Opportunity();
  testOppty.name='testOppty';
  testOppty.AccountId=acc.id;
  testOppty.CloseDate=System.today();
  testOppty.StageName= 'Closed Won';
  testOppty.Type='EnerLead Renewal';
  testOppty.Description='Newly created Oppertunity';
  insert testOppty;
  
        insert odr;
                  Contract c = new Contract(
                ,StartDate=testOppty.CloseDate
                ,AccountId = testOppty.AccountId
                ,Name = testOppty.Name
                );
            insert c;
  
  
        
        test.startTest();
        
            PageReference pageRef = Page.myclass;
            Test.setCurrentPage(pageRef);
            
            pageRef.getParameters().put('id',testOppty.id);
            ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
            
            myclass  controller = new myclass(sc);
            controller.autoRun();
            controller.back();
            controller.MainMethod();
        test.stopTest();
}
}
How to write wrapper class unit test to get the max code coverage? I am unable to access wrapper class in test class. Guys Help.
 
I have a button in opportunity tab , which call this myclass. Here i am changing the status of acyive contacts in vf page as "Expired or Lapse" and copying the contact roles in newyly created opportunity and contract. 
public class myclass{
 
    
    public Opportunity o;
    String theId = ApexPages.currentPage().getParameters().get('id');
    List<OpportunityContactRole> oppRoles;
    Contract[] myContract;
    Opportunity[] myOpportunity;
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
    public  myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
         if (theId == null) {
            return null;
        }
       o=[select id, name, AccountId,  StageName, Type, CloseDate from Opportunity where id =:theId];
 
records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'  ])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }
     return null;
          
}
 public PageReference MainMethod() {
 
  try{
       o=[select id, name, AccountId,  StageName, Type, CloseDate from Opportunity where id =:theId];
 
             
             //Create the contract
            Contract c = new Contract(
                ,StartDate=o.CloseDate
                ,AccountId = o.AccountId
                ,Name = o.Name
                );
            insert c;
       
       
       //copy contact roles

            oppRoles = [SELECT Id, OpportunityID, ContactID, IsPrimary, Role FROM OpportunityContactRole WHERE OpportunityId = :theId LIMIT 10000];
             myContract = [SELECT Id, ContractNumber, StartDate, EndDate,
                           AccountId, ContractTerm, Status, CustomerSignedId FROM Contract WHERE Id = :c.Id LIMIT 1];
             
             if ( ! oppRoles.isEmpty()) {
                    myContract[0].CustomerSignedId = oppRoles.get(0).ContactId;
              }  
              
              if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }
                
     //Create new Oppertunity
             Opportunity opp = new Opportunity(
                     Name = 'TESTACCOUNT_test july' +'Date.today()'
                     ,CloseDate=Date.today()
                     ,StageName= 'Renewal'
                     ,Type='EnerLead Renewal'
                     ,Description='Newly created Oppertunity'
                     ,Product_Family__c='EnerLead' 
                );
            insert opp;
        //Copy roles of old opportunity to new
            
             myOpportunity = [SELECT Id, StageName, Type FROM Opportunity WHERE Id = :opp.Id LIMIT 1];
             
          
              
              if ( ! oppRoles.isEmpty()){
                    List<OpportunityContactRole> newoppRoles = new List<OpportunityContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             newoppRoles.add(new OpportunityContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             OpportunityId = myOpportunity[0].Id));                       
                         }
                         insert newoppRoles;
              
              }          
   
//try section     
}catch(QueryException e){
      ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Something is wrong' );
      ApexPages.addmessage(msg);
}catch(DMLException e){}
    
        return null;        
  }


// To Update contracts 
 public PageReference back() {
       List<Contract> contractToBeUpdate = new List<Contract>();
    for(WrapperSObject wrapper : records){
        wrapper.con.status = wrapper.selectedValue;
        contractToBeUpdate.add(wrapper.con);
    }
    update contractToBeUpdate;
    PageReference pageRef ;    
     return pageRef= MainMethod();
  }
 


public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
  
     

}

Here is my test class
I have very less code coverage.
@isTest
private class myclassTest {
    private static testMethod void testAutoRun() {

    
        Account acc = new Account(Name='Abce');
  insert acc;
 
  Opportunity  testOppty = new Opportunity();
  testOppty.name='testOppty';
  testOppty.AccountId=acc.id;
  testOppty.CloseDate=System.today();
  testOppty.StageName= 'Closed Won';
  testOppty.Type='EnerLead Renewal';
  testOppty.Description='Newly created Oppertunity';
  testOppty.Product_Family__c='EnerLead'; 
  insert testOppty;
  
        //Create the Order
                    Order odr = new Order(  
                    OpportunityId=testOppty.id
                    ,AccountId = testOppty.AccountId
                    ,Name = testOppty.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
        insert odr;
                  Contract c = new Contract(
                ,StartDate=testOppty.CloseDate
                ,AccountId = testOppty.AccountId
                ,Name = testOppty.Name
                );
            insert c;
  
  
        
        test.startTest();
        
            PageReference pageRef = Page.myclass;
            Test.setCurrentPage(pageRef);
            
            pageRef.getParameters().put('id',testOppty.id);
            ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
            
            myclass  controller = new myclass(sc);
            controller.autoRun();
            controller.back();
            controller.MainMethod();
        test.stopTest();
}
}
I want to know how to write unit test for following sections 
  • "copying contact roles from old to new opportunity and contract"
  • "Wrapper class and last getoptions() selection method"?
Guys any help. ? please
I want to add the "Product Related List" in the Opportunity related lists. Every Related list is showing but Product related list is not showing up. How to make it visible ? 
I am copying my contacts roles of old opportunity to new opportunit and also copying contact roles of opportunity to contact roles of newly created contract. Now how to wite test class for this ?
String theId = ApexPages.currentPage().getParameters().get('id');
List<OpportunityContactRole> oppRoles;
oppRoles = [SELECT Id, OpportunityID, ContactID, IsPrimary, Role FROM OpportunityContactRole WHERE OpportunityId = :theId LIMIT 10000];
            
//copying contact roles of old opportunity to new opportunity

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }
// myContract[0] is also defined and working fine,  code will become long so  here not mentioned, 
/// also copying the contact roles of opportunity to newly created contract

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }

Guys any help ?
I want to show all active contracts in VF page and want to change their status either "Expired" or "Lapse" in VF. 
Here is my VF page
<apex:page standardController="Opportunity"
 extensions="Opp2Order"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Active Contracts" > 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract ID</apex:facet> 
                <apex:outputText value="{!wrapper.con.id}"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" onchange="changeStatus()" >
                            <apex:selectOptions value="{!wrapper.options}" />  
                        </apex:selectList>
                        
           </apex:column> 
              </apex:pageBlockTable>    
         </apex:pageBlock> 
  <apex:commandButton action="{!back}" value="Save!" />
  
    </apex:form>
  
  
</apex:page>


Here is my class
public class Opp2Order {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
 
   public Opp2Order(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }

             return null;         
}

 public PageReference back() {
  // How to here update the status of all contracts 
  }
 
 public void changeStatus()
 {
    // in back() method or in this method ? I am confused
 }

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
}

I want to update the status of all contracts according to selection in VF page. How to get selected value from VF pick list and how to use in Apex class. 

I am confused wether i can pick selected value and update contract status in Onchange() method or in back() method i should update the status of all contracts. How i am supposed to do that. I am stucked badly. Guy's help.  
 
I want to show all active contracts in VF page and want to change their status either "Expired" or "Lapse" in VF. 
Here is my VF page
<apex:page standardController="Opportunity"
 extensions="Opp2Order"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Active Contracts" > 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract ID</apex:facet> 
                <apex:outputText value="{!wrapper.con.id}"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" onchange="changeStatus()" >
                            <apex:selectOptions value="{!wrapper.options}" />  
                        </apex:selectList>
                        
           </apex:column> 
              </apex:pageBlockTable>    
         </apex:pageBlock> 
  <apex:commandButton action="{!back}" value="Save!" />
  
    </apex:form>
  
  
</apex:page>
Here is my class
 
public class Opp2Order {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
 
   public Opp2Order(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }

             return null;         
}

 public PageReference back() {
  // How to here update the status of all contracts 
  }
 
 public void changeStatus()
 {
    // in back() method or in this method ? I am confused
 }

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
}

I want to update the status of all contracts according to selection in VF page. How to get selected value from VF pick list and how to use in Apex class. 

I am confused wether i can pick selected value and update contract status in Onchange() method or in back() method i should update the status of all contracts. How i am supposed to do that. I am stucked badly. Guy's help.  
 

I have button in opportunity page , When that button is pressed, I want to show my pending contracts in Visual force page and want o Expire or Laspe through VF page. Here is my class
public class myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
 records = new List<WrapperSObject>();
 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending'])
 {
 records.add(new WrapperSObject(c));
 records.add(new WrapperSObject(getOptions(), '--None--'));
}

             return null;
          
}

 

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
      
    public WrapperSObject( List < SelectOption > options, String selectedValue) {
    
     this.options = options;
     this.selectedValue = selectedValue;

     }
     public WrapperSObject(Contract c) {
     con=c;
     }
     
     
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }

}

Here is my VF page.
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Pending Contracts"> 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!wrapper.con.Status}"/> 
            </apex:column>
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" >
                            <apex:selectOptions value="{!wrapper.options}" />
                        </apex:selectList>
            </apex:column>
                       
        </apex:pageBlockTable>
        
    </apex:pageBlock> 
    </apex:form>
    
   
  
 </apex:page>
Why this small blank Dropdown menu is showing ? 
How to fix this ?
User-added image

Now how to change the status of these contracts ? I mean how to return the value "Expired or Lapse" and update crosponding Contract ? 

Guys any help ?

 
I have button in opportunity page , When that button is pressed, I want to show my pending contracts in Visual force page and want o Expire or Laspe through VF page. Here is my class
public class myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
 records = new List<WrapperSObject>();
 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending'])
 {
 records.add(new WrapperSObject(c));
 records.add(new WrapperSObject(getOptions(), '--None--'));
}

             return null;
          
}

 

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
      
    public WrapperSObject( List < SelectOption > options, String selectedValue) {
    
     this.options = options;
     this.selectedValue = selectedValue;

     }
     public WrapperSObject(Contract c) {
     con=c;
     }
     
     
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }

}

Here is my VF page.
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Pending Contracts"> 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!wrapper.con.Status}"/> 
            </apex:column>
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" >
                            <apex:selectOptions value="{!wrapper.options}" />
                        </apex:selectList>
            </apex:column>
                       
        </apex:pageBlockTable>
        
    </apex:pageBlock> 
    </apex:form>
    
   
  
 </apex:page>

Why this small blank Dropdown menu is showing ? 
How to fix this ?
Here is out put
Now how to change the status of these contracts ? I mean how to return the value "Expired or Lapse" and update crosponding Contract ? 

Guys ay help ?


 

I have a button in Opportuinty page which calls myclass, i am trying to show the contracts in visual force page, my controler is Opportunity type so now i am getting error when i try to access the "ContractNumber" in SObject Opportunity, But i need to use Opportunity controler.  NOw how should i access ContractNumber and Status in Visual force page. 
Here is my Visual force page. 

<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
<apex:pageBlock title="My  Contracts"> 
        <apex:pageBlockTable value="{!Records}"  var="Record"> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!Record.Name}"/> 
            </apex:column> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!Record.ContractNumber}"/> 
            </apex:column> 
           <apex:column > 
                <apex:facet name="header">Contract Status</apex:facet> 
                <apex:outputText value="{!Record.Status"/> 
            </apex:column> 
          </apex:pageBlockTable> 
    </apex:pageBlock> 
</apex:page>
Here is my class.
public class  myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    public List<Contract> Records {get; set;}

   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 Records = [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending']; 
 return null;
 }
I am new to salesforce , may be its too easy but for me its difficult. Guys any Help ! 
I have a button in my opporunity which calls a apex class, Now what i want is: If the Opportunity "Type" field = MyProduct I would like an Visual force page that displays all the active Contracts related to the Account in the Opportunity. I want to be able to change the Status of the existing Contract to Expired or Lapse in visual force page. How should i do all this. I am new to Sales force. That would be a great help if some one can provide some sample code. 

I have a button in my opporunity which calls a apex class, Now what i want is: If the Opportunity "Type" field = MyProduct I would like an Visual force page that displays all the active Contracts related to the Account in the Opportunity. I want to be able to change the Status of the existing Contract to Expired or Lapse in visual force page. How should i do all this. I am new to Sales force. That would be a great help if some one can provide some sample code. 

I am new to apex, I have created a button to call the apex class through the visual force page. 

Here is my visual force page code. 
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
</apex:page>
Here is my apex class.
public class myclass {
    private final Opportunity o;
    String tmp;
   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
    public PageReference autoRun() {
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        for (Opportunity o:[select id, name, AccountId,  from Opportunity where id =:theId]) {
 
                 //Create the Order
                    Order odr = new Order( 
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
                insert odr;
                tmp=odr.id;            
              }                 
        PageReference pageRef = new PageReference('/' + tmp); 
        pageRef.setRedirect(true);
        return pageRef;
      }
}
I want to create test class. I don't know how to refer PageReference autoRun() method from test class. Guys need help if some one can tell me about test class of this apex class. 
 

I am new to apex, I have created a button to call the apex class through the visual force page. 

Here is my visual force page code. 

<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
</apex:page>
Here is my apex class. 
public class myclass {
    private final Opportunity o;
    String tmp;
    
   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
    public PageReference autoRun() { 
        String theId = ApexPages.currentPage().getParameters().get('id');

        for (Opportunity o:[select id, name, AccountId,  from Opportunity where id =:theId]) {

                 //Create the Order
                    Order odr = new Order(  
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
                insert odr;
                tmp=odr.id;             
              }                  
        PageReference pageRef = new PageReference('/' + tmp);  
        pageRef.setRedirect(true);
        return pageRef;
      }
}
I want to create test class. I don't know how to refer PageReference autoRun() method from test class. Guys need help if some one can tell me about test class of this apex class. 

I am totaly new to salesforce, want to do this. 
- Create a Button on Opportunity Details Page.
- Write the code to convert Opportunity to a Contract and Order, when the button is pressed.

I am realy confused how to start ? I am unable to find a solution.  I am hoping that someone here has the knowledge on how to accomplish this.  Thank in advance for your time and help

I have integrated my heroku apps in sales force using canvus, every thing was fine , apps were wroking fine in salesforce, but now when i tried to acces my apps in salesforce it's giving the following error 

Authentication Failed: OAuth login invalid or expired access token error

 

what can be the possible reason of this issue, Guys help. 

I am copying my contacts roles of old opportunity to new opportunit and also copying contact roles of opportunity to contact roles of newly created contract. Now how to wite test class for this ?
String theId = ApexPages.currentPage().getParameters().get('id');
List<OpportunityContactRole> oppRoles;
oppRoles = [SELECT Id, OpportunityID, ContactID, IsPrimary, Role FROM OpportunityContactRole WHERE OpportunityId = :theId LIMIT 10000];
            
//copying contact roles of old opportunity to new opportunity

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }
// myContract[0] is also defined and working fine,  code will become long so  here not mentioned, 
/// also copying the contact roles of opportunity to newly created contract

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }

Guys any help ?
I am new to apex, I have created a button to call the apex class through the visual force page. 

Here is my visual force page code. 
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
</apex:page>
Here is my apex class.
public class myclass {
    private final Opportunity o;
    String tmp;
   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
    public PageReference autoRun() {
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        for (Opportunity o:[select id, name, AccountId,  from Opportunity where id =:theId]) {
 
                 //Create the Order
                    Order odr = new Order( 
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
                insert odr;
                tmp=odr.id;            
              }                 
        PageReference pageRef = new PageReference('/' + tmp); 
        pageRef.setRedirect(true);
        return pageRef;
      }
}
I want to create test class. I don't know how to refer PageReference autoRun() method from test class. Guys need help if some one can tell me about test class of this apex class. 
 
Here is my apex class copying the opportunity line items of current opportunity to newly created order and opportunity. I do not know how to write unit test for opportunity line items,
String theId = ApexPages.currentPage().getParameters().get('id');
 List<OpportunityLineItem> currentOppLI = [
                                SELECT PricebookEntryId, Quantity, UnitPrice,
                                       TotalPrice, ListPrice, Description
                                FROM OpportunityLineItem
                                WHERE OpportunityId = :theId];
 //Create the Order
                    Order odr = new Order(  
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    ,Pricebook2Id = o.Pricebook2Id
                    );
                insert odr;
                //copy Opportunity line items to Order line items
                  List<OrderItem> newOrderProductsList = new List<OrderItem>();
    
        for (OpportunityLineItem item : currentOppLI) {
             newOrderProductsList.add(new OrderItem(
                    PricebookEntryId = item.PricebookEntryId,
                    Quantity = item.Quantity,
                    UnitPrice = item.UnitPrice,
                    Description = item.Description,
                    OrderId = odr.Id));
         }
             insert newOrderProductsList;  

 List<OpportunityLineItem> newOppLI = new List<OpportunityLineItem>();
        
        for (OpportunityLineItem item : currentOppLI) {
             newOppLI.add(new OpportunityLineItem(
             PricebookEntryId = item.PricebookEntryId,   
             Quantity = item.Quantity,
             UnitPrice = item.UnitPrice,
             Description = item.Description,
             OpportunityId = opp.Id));
         }
            
    insert newOppLI;   


//opp is newly created opportunity and odr is newly created order
How to write unit test for opportunity line items and order items??
 
Here is my apex class
public class myclass{
 
    
    public Opportunity o;
    String theId = ApexPages.currentPage().getParameters().get('id');
  
    
   @TestVisible public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
  
  public  myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
         if (theId == null) {
            return null;
        }
    
records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'  ])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }
     return null;
          
}
 public PageReference MainMethod() {
 
  try{
     //Create new Oppertunity
             Opportunity opp = new Opportunity(
                     Name = 'TESTACCOUNT_test july' +'Date.today()'
                     ,CloseDate=Date.today()
                     ,StageName= 'Renewal'
                     ,Type='EnerLead Renewal'
                     ,Description='Newly created Oppertunity' 
                );
            insert opp;
   
//try section     
}catch(QueryException e){
}catch(DMLException e){}
    
        return null;        
  }


// To Update contracts 
 public PageReference back() {
       List<Contract> contractToBeUpdate = new List<Contract>();
    for(WrapperSObject wrapper : records){
        wrapper.con.status = wrapper.selectedValue;
        contractToBeUpdate.add(wrapper.con);
    }
    update contractToBeUpdate;
    PageReference pageRef ;    
     return pageRef= MainMethod();
  }
 


@TestVisible public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
  }

here is my test class
@isTest
private class myclassTest {
    private static testMethod void testAutoRun() {

    
        Account acc = new Account(Name='Abce');
  insert acc;
 
  Opportunity  testOppty = new Opportunity();
  testOppty.name='testOppty';
  testOppty.AccountId=acc.id;
  testOppty.CloseDate=System.today();
  testOppty.StageName= 'Closed Won';
  testOppty.Type='EnerLead Renewal';
  testOppty.Description='Newly created Oppertunity';
  insert testOppty;
  
        insert odr;
                  Contract c = new Contract(
                ,StartDate=testOppty.CloseDate
                ,AccountId = testOppty.AccountId
                ,Name = testOppty.Name
                );
            insert c;
  
  
        
        test.startTest();
        
            PageReference pageRef = Page.myclass;
            Test.setCurrentPage(pageRef);
            
            pageRef.getParameters().put('id',testOppty.id);
            ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
            
            myclass  controller = new myclass(sc);
            controller.autoRun();
            controller.back();
            controller.MainMethod();
        test.stopTest();
}
}
How to write wrapper class unit test to get the max code coverage? I am unable to access wrapper class in test class. Guys Help.
 
I have a button in opportunity tab , which call this myclass. Here i am changing the status of acyive contacts in vf page as "Expired or Lapse" and copying the contact roles in newyly created opportunity and contract. 
public class myclass{
 
    
    public Opportunity o;
    String theId = ApexPages.currentPage().getParameters().get('id');
    List<OpportunityContactRole> oppRoles;
    Contract[] myContract;
    Opportunity[] myOpportunity;
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
    public  myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
         if (theId == null) {
            return null;
        }
       o=[select id, name, AccountId,  StageName, Type, CloseDate from Opportunity where id =:theId];
 
records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'  ])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }
     return null;
          
}
 public PageReference MainMethod() {
 
  try{
       o=[select id, name, AccountId,  StageName, Type, CloseDate from Opportunity where id =:theId];
 
             
             //Create the contract
            Contract c = new Contract(
                ,StartDate=o.CloseDate
                ,AccountId = o.AccountId
                ,Name = o.Name
                );
            insert c;
       
       
       //copy contact roles

            oppRoles = [SELECT Id, OpportunityID, ContactID, IsPrimary, Role FROM OpportunityContactRole WHERE OpportunityId = :theId LIMIT 10000];
             myContract = [SELECT Id, ContractNumber, StartDate, EndDate,
                           AccountId, ContractTerm, Status, CustomerSignedId FROM Contract WHERE Id = :c.Id LIMIT 1];
             
             if ( ! oppRoles.isEmpty()) {
                    myContract[0].CustomerSignedId = oppRoles.get(0).ContactId;
              }  
              
              if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }
                
     //Create new Oppertunity
             Opportunity opp = new Opportunity(
                     Name = 'TESTACCOUNT_test july' +'Date.today()'
                     ,CloseDate=Date.today()
                     ,StageName= 'Renewal'
                     ,Type='EnerLead Renewal'
                     ,Description='Newly created Oppertunity'
                     ,Product_Family__c='EnerLead' 
                );
            insert opp;
        //Copy roles of old opportunity to new
            
             myOpportunity = [SELECT Id, StageName, Type FROM Opportunity WHERE Id = :opp.Id LIMIT 1];
             
          
              
              if ( ! oppRoles.isEmpty()){
                    List<OpportunityContactRole> newoppRoles = new List<OpportunityContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             newoppRoles.add(new OpportunityContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             OpportunityId = myOpportunity[0].Id));                       
                         }
                         insert newoppRoles;
              
              }          
   
//try section     
}catch(QueryException e){
      ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Something is wrong' );
      ApexPages.addmessage(msg);
}catch(DMLException e){}
    
        return null;        
  }


// To Update contracts 
 public PageReference back() {
       List<Contract> contractToBeUpdate = new List<Contract>();
    for(WrapperSObject wrapper : records){
        wrapper.con.status = wrapper.selectedValue;
        contractToBeUpdate.add(wrapper.con);
    }
    update contractToBeUpdate;
    PageReference pageRef ;    
     return pageRef= MainMethod();
  }
 


public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
  
     

}

Here is my test class
I have very less code coverage.
@isTest
private class myclassTest {
    private static testMethod void testAutoRun() {

    
        Account acc = new Account(Name='Abce');
  insert acc;
 
  Opportunity  testOppty = new Opportunity();
  testOppty.name='testOppty';
  testOppty.AccountId=acc.id;
  testOppty.CloseDate=System.today();
  testOppty.StageName= 'Closed Won';
  testOppty.Type='EnerLead Renewal';
  testOppty.Description='Newly created Oppertunity';
  testOppty.Product_Family__c='EnerLead'; 
  insert testOppty;
  
        //Create the Order
                    Order odr = new Order(  
                    OpportunityId=testOppty.id
                    ,AccountId = testOppty.AccountId
                    ,Name = testOppty.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
        insert odr;
                  Contract c = new Contract(
                ,StartDate=testOppty.CloseDate
                ,AccountId = testOppty.AccountId
                ,Name = testOppty.Name
                );
            insert c;
  
  
        
        test.startTest();
        
            PageReference pageRef = Page.myclass;
            Test.setCurrentPage(pageRef);
            
            pageRef.getParameters().put('id',testOppty.id);
            ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
            
            myclass  controller = new myclass(sc);
            controller.autoRun();
            controller.back();
            controller.MainMethod();
        test.stopTest();
}
}
I want to know how to write unit test for following sections 
  • "copying contact roles from old to new opportunity and contract"
  • "Wrapper class and last getoptions() selection method"?
Guys any help. ? please
I am copying my contacts roles of old opportunity to new opportunit and also copying contact roles of opportunity to contact roles of newly created contract. Now how to wite test class for this ?
String theId = ApexPages.currentPage().getParameters().get('id');
List<OpportunityContactRole> oppRoles;
oppRoles = [SELECT Id, OpportunityID, ContactID, IsPrimary, Role FROM OpportunityContactRole WHERE OpportunityId = :theId LIMIT 10000];
            
//copying contact roles of old opportunity to new opportunity

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }
// myContract[0] is also defined and working fine,  code will become long so  here not mentioned, 
/// also copying the contact roles of opportunity to newly created contract

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }

Guys any help ?
I want to show all active contracts in VF page and want to change their status either "Expired" or "Lapse" in VF. 
Here is my VF page
<apex:page standardController="Opportunity"
 extensions="Opp2Order"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Active Contracts" > 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract ID</apex:facet> 
                <apex:outputText value="{!wrapper.con.id}"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" onchange="changeStatus()" >
                            <apex:selectOptions value="{!wrapper.options}" />  
                        </apex:selectList>
                        
           </apex:column> 
              </apex:pageBlockTable>    
         </apex:pageBlock> 
  <apex:commandButton action="{!back}" value="Save!" />
  
    </apex:form>
  
  
</apex:page>
Here is my class
 
public class Opp2Order {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
 
   public Opp2Order(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }

             return null;         
}

 public PageReference back() {
  // How to here update the status of all contracts 
  }
 
 public void changeStatus()
 {
    // in back() method or in this method ? I am confused
 }

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
}

I want to update the status of all contracts according to selection in VF page. How to get selected value from VF pick list and how to use in Apex class. 

I am confused wether i can pick selected value and update contract status in Onchange() method or in back() method i should update the status of all contracts. How i am supposed to do that. I am stucked badly. Guy's help.  
 

I have button in opportunity page , When that button is pressed, I want to show my pending contracts in Visual force page and want o Expire or Laspe through VF page. Here is my class
public class myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
 records = new List<WrapperSObject>();
 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending'])
 {
 records.add(new WrapperSObject(c));
 records.add(new WrapperSObject(getOptions(), '--None--'));
}

             return null;
          
}

 

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
      
    public WrapperSObject( List < SelectOption > options, String selectedValue) {
    
     this.options = options;
     this.selectedValue = selectedValue;

     }
     public WrapperSObject(Contract c) {
     con=c;
     }
     
     
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }

}

Here is my VF page.
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Pending Contracts"> 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!wrapper.con.Status}"/> 
            </apex:column>
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" >
                            <apex:selectOptions value="{!wrapper.options}" />
                        </apex:selectList>
            </apex:column>
                       
        </apex:pageBlockTable>
        
    </apex:pageBlock> 
    </apex:form>
    
   
  
 </apex:page>
Why this small blank Dropdown menu is showing ? 
How to fix this ?
User-added image

Now how to change the status of these contracts ? I mean how to return the value "Expired or Lapse" and update crosponding Contract ? 

Guys any help ?

 
I have button in opportunity page , When that button is pressed, I want to show my pending contracts in Visual force page and want o Expire or Laspe through VF page. Here is my class
public class myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
 records = new List<WrapperSObject>();
 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending'])
 {
 records.add(new WrapperSObject(c));
 records.add(new WrapperSObject(getOptions(), '--None--'));
}

             return null;
          
}

 

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
      
    public WrapperSObject( List < SelectOption > options, String selectedValue) {
    
     this.options = options;
     this.selectedValue = selectedValue;

     }
     public WrapperSObject(Contract c) {
     con=c;
     }
     
     
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }

}

Here is my VF page.
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Pending Contracts"> 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!wrapper.con.Status}"/> 
            </apex:column>
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" >
                            <apex:selectOptions value="{!wrapper.options}" />
                        </apex:selectList>
            </apex:column>
                       
        </apex:pageBlockTable>
        
    </apex:pageBlock> 
    </apex:form>
    
   
  
 </apex:page>

Why this small blank Dropdown menu is showing ? 
How to fix this ?
Here is out put
Now how to change the status of these contracts ? I mean how to return the value "Expired or Lapse" and update crosponding Contract ? 

Guys ay help ?


 

I have a button in Opportuinty page which calls myclass, i am trying to show the contracts in visual force page, my controler is Opportunity type so now i am getting error when i try to access the "ContractNumber" in SObject Opportunity, But i need to use Opportunity controler.  NOw how should i access ContractNumber and Status in Visual force page. 
Here is my Visual force page. 

<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
<apex:pageBlock title="My  Contracts"> 
        <apex:pageBlockTable value="{!Records}"  var="Record"> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!Record.Name}"/> 
            </apex:column> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!Record.ContractNumber}"/> 
            </apex:column> 
           <apex:column > 
                <apex:facet name="header">Contract Status</apex:facet> 
                <apex:outputText value="{!Record.Status"/> 
            </apex:column> 
          </apex:pageBlockTable> 
    </apex:pageBlock> 
</apex:page>
Here is my class.
public class  myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    public List<Contract> Records {get; set;}

   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 Records = [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending']; 
 return null;
 }
I am new to salesforce , may be its too easy but for me its difficult. Guys any Help ! 
I am new to apex, I have created a button to call the apex class through the visual force page. 

Here is my visual force page code. 
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
</apex:page>
Here is my apex class.
public class myclass {
    private final Opportunity o;
    String tmp;
   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
    public PageReference autoRun() {
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        for (Opportunity o:[select id, name, AccountId,  from Opportunity where id =:theId]) {
 
                 //Create the Order
                    Order odr = new Order( 
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
                insert odr;
                tmp=odr.id;            
              }                 
        PageReference pageRef = new PageReference('/' + tmp); 
        pageRef.setRedirect(true);
        return pageRef;
      }
}
I want to create test class. I don't know how to refer PageReference autoRun() method from test class. Guys need help if some one can tell me about test class of this apex class. 
 

I am new to apex, I have created a button to call the apex class through the visual force page. 

Here is my visual force page code. 

<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
</apex:page>
Here is my apex class. 
public class myclass {
    private final Opportunity o;
    String tmp;
    
   public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
    public PageReference autoRun() { 
        String theId = ApexPages.currentPage().getParameters().get('id');

        for (Opportunity o:[select id, name, AccountId,  from Opportunity where id =:theId]) {

                 //Create the Order
                    Order odr = new Order(  
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    );
                insert odr;
                tmp=odr.id;             
              }                  
        PageReference pageRef = new PageReference('/' + tmp);  
        pageRef.setRedirect(true);
        return pageRef;
      }
}
I want to create test class. I don't know how to refer PageReference autoRun() method from test class. Guys need help if some one can tell me about test class of this apex class.