Skip to main content
here my code,pls helpme to write test class for  this

 

public class ContractLineItemclass {

 

    public static void createRec(List<contract> contractList){

 

    List<Contract_Line_Item__c> termlist  = new List<Contract_Line_Item__c>();

 

    

 

     set<id> oppsetid= new set<id>();

 

        

 

      for(Contract c:contractList){

 

      oppsetid.add(c.opportunity__C) ;

 

        }

 

        

 

    map<id,Opportunity> opp=new map<id,Opportunity>([select id,amount from Opportunity where id in:oppsetid]);

 

   for(contract co:contractList){

 

   opportunity op=opp.get(co.opportunity__C);

 

       

 

 

 

 

                for(contract s : contractList){

 

                if(s.contractTerm != null){

 

                for(Integer i = 0; i < s.ContractTerm; i++){

 

                

 

                    Contract_Line_Item__c con = new Contract_Line_Item__c();

 

                    con.Start__c = s.StartDate.addmonths(i);

 

                    con.End__c  = con.Start__c.addDays(Date.daysInMonth(con.Start__c.year(),con.Start__c.month()) - 1);

 

                    con.Amount__c = (op.Amount/s.ContractTerm);    

 

                    con.Contract__c = s.Id;

 

                    termlist .add(con);

 

       

 

                }

 

                }

 

                    

 

try{

 

insert termlist;

 

}

 

 

catch(system.dmlException e){

 

for (contract con : contractlist) {

 

          con.addError('There was a problem Inserting the contract');

 

 }

 

 }

 

 }

 

}

 

}  

 

}
2 Antworten
  1. 8. Feb. 2020, 19:46
    Looks like an easy one. But we are missing the trigger here. If we imagine that it is triggered when you create a contract.

     

    1. Create an account with any info

     

    2. Create an opportunity with any info, relate it to the account

     

    3. Create a Contract with contract term so that your for loop runs. relate it to the opportunity.

     

    Done