function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PS81PS81 

help to write test case - new starter

Hi

I'm pretty much new to salesforce. As my first task i had written a trigger but need help to write a test case or test class. Please any help on this?
 
trigger OpportunityWeightageRev on Opportunity (before update) {
    double prob_diff = 0;
    double Nrev = 0; //New revenue
    double Orev = 0; //Old revenue
    
    Opportunity OprLast; //Previous opportunity data
    for(Opportunity Opr : Trigger.new){
        OprLast = Trigger.oldMap.get(Opr.id); 
        if(OprLast.Probability != opr.Probability){
            if(prob_diff==0){
                prob_diff = math.abs(OprLast.Probability - opr.Probability);
            }
            if((opr.Total_Contract_Value__c==0) || (opr.Total_Contract_Value__c==null)) opr.Total_Contract_Value__c = 0;
            nrev = opr.Total_Contract_Value__c * (prob_diff/100);
            orev = OprLast.Weighted_Revenue__c;
            
            if(orev<>0){
                opr.mplan__c 		=  nrev / orev;

            }
        }            
    }
}

 
Best Answer chosen by PS81
ManojjenaManojjena
Hi Ps81,
Try with belwo code .You need to assign stage name as per your organisation.You need to update with one stage which probability is different then the insert stage probability .
 
@isTest
public class OpportunityWeightageRevTest{
  private static testMethod void unitTest(){
    Account acc=new Account();
	acc.name='TestAccount';
	Insert acc;
	Opportunity opp=new opportunity();
	opp.StageName='Prospecting';
	opp.CloseDate=System.Today();
	opp.Accountid=acc.id;
	insert opp;
	opp.StageName='Value Proposition';
	update opp;
  
  }
}
Fo rsome test class concept you can check belwo links !!
http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html

Let me know if it helps !!\
Thanks
Manoj
 

All Answers

ManojjenaManojjena
Hi Ps81,
Try with belwo code .You need to assign stage name as per your organisation.You need to update with one stage which probability is different then the insert stage probability .
 
@isTest
public class OpportunityWeightageRevTest{
  private static testMethod void unitTest(){
    Account acc=new Account();
	acc.name='TestAccount';
	Insert acc;
	Opportunity opp=new opportunity();
	opp.StageName='Prospecting';
	opp.CloseDate=System.Today();
	opp.Accountid=acc.id;
	insert opp;
	opp.StageName='Value Proposition';
	update opp;
  
  }
}
Fo rsome test class concept you can check belwo links !!
http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html

Let me know if it helps !!\
Thanks
Manoj
 
This was selected as the best answer
SivaGSivaG
Hi,

Please see if this test class works for your trigger. Correct wherever needed.

@isTest
private class OpportunityWeightageRevtest(){

     private static testmethod unittest1(){
      
     Account acc = new Account(name = 'TestAcc');
     insert acc;
     
    Opportunity opp = new Opportunity(name = 'TestOpp',AccountId = acc.Id,StageName = 'Prospecting',Closedate = Date.Today(),Weighted_Revenue__c = 100);
    insert opp;

    Opportunity op = [Select id,StageName from Opportunity WHERE Id = :opp.Id];

    op.StageName = 'Negotiating';

   Test.startTest();
   update op;
  Test.stopTest();

Opportunity o = [Select id,StageName,mplan__c from Opportunity WHERE Id = :op.Id];

System.assertNotEquals(Null,o.mplan__c);
      
           

}


}
Thanks
Kumar
PS81PS81
Thanks both, i think it didnt work i.e. the code coverage shows 0%
 
@isTest	
		public class OpportunityWeightageRev_Test {	
		    static testmethod void OpportunitySave(){	
			
		        opportunity opr = new opportunity();	
		        // fill the fields	
		        opr.name = 'Test opportunity';	
		        opr.AccountId = '00126000004aWaq';	
		        opr.Area__c = 'New Business';	
		        opr.Sub_Area__c = 'External';	
		        opr.Type = 'FP';	
		        opr.Primary_Location__c = 'London';	
		        opr.StageName = 'Opportunity Qualified';	
		        opr.Start_Date__c = date.today();	
		        opr.CloseDate = date.parse('30/12/2016');	
		        opr.Probability = 30;	
		        //insert the opportunity	
		        insert opr;	
			
		        opr.StageName = 'Shortlisted';	
		        //update to test the trigger	
		        update opr;	
		    }	
		}

anything i'm doing wrong? or am i'm looking into the correct place for code coverage (as in test tab of the apex console)?
PS81PS81
Hi

only when i hit 'Run all' i get the code coverage as 26% but when i hit 'Run' and select the test class and method i do not see any code coverage...why is that so? and below the code coverage hit:

User-added image

how can i make it atleast 75% ?
PS81PS81
I think its now sorted....took care of the calculated values i.e. the formula fields and now it is 100% coverage