Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

Below is the code for my test class.

Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>]; 

Case objCase = new Case(); 

objCase.standard_or_nonstandard__c = 'Yes';

if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested 

    opp.Deal_Type__c = 'Standard'; 

}

 else{                                          // else part is getting skipped

     opp.Deal_Type__c = 'Not Standard'; 

}

And only first if condition is getting tested and other is skipping which is why the code is not reaching 75% off code coverage.

the field standard_or_nonstandard__c is picklist having two values Yes & No.

And if the value if Yes, the deal type should be standard, and if No, the deal type is not standard.

Any suggestion on this?

1 Answer

0 votes
by (32.1k points)
edited by

You should write both IF and ELSE condition in a test like below sample:

Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>]; 

Case objCase = new Case(); 

objCase.standard_or_nonstandard__c = 'Yes';

if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested 

    opp.Deal_Type__c = 'Standard'; 

}

 else{                                          // else part is getting skipped

     opp.Deal_Type__c = 'Not Standard'; 

}

objCase.standard_or_nonstandard__c = 'No';

if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested 

    opp.Deal_Type__c = 'Standard'; 

}

 else{                                          // else part is getting skipped

     opp.Deal_Type__c = 'Not Standard'; 

}

Go for this in-depth job-oriented salesforce training online now! 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...