Check string value in salesforce test class

0 votes

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?

Mar 3, 2022 in SalesForce by surbhi
• 3,820 points
1,619 views

1 answer to this question.

0 votes

Because the field objCase.standard_or_nonstandard_c is set to 'Yes' before the condition. This indicates that when the condition is assessed, the field can never be equal to 'No.' As a result, the first 'if' block is always used, whereas the else condition is never used. In order for test data to enter the otherwise block, you'll need to remove that line (objCase.standard_or_nonstandard_c = 'Yes';) before the If statement.

I hope this helps!

Check out the salesforce training to learn from the industry experts.

answered Mar 3, 2022 by CoolCoder
• 4,420 points

Related Questions In SalesForce

0 votes
0 answers

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Salesforce Test Class - Facing - System.NullPointerException: ...READ MORE

Mar 1, 2022 in SalesForce by surbhi
• 3,820 points
2,065 views
0 votes
1 answer

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Because testAccount.account is never set, it is ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
2,851 views
0 votes
0 answers

Apex (Salesforce) read json from response and store it in a string variable

I am trying to hit a API ...READ MORE

Mar 4, 2022 in SalesForce by surbhi
• 3,820 points
1,204 views
0 votes
1 answer

Apex Class and Apex Trigger is invisible in Salesforce Developer Console

You'll need to create a new developer ...READ MORE

answered Mar 8, 2022 in SalesForce by anonymous

edited Jun 19, 2023 by Khan Sarfaraz 2,675 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,342 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,018 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
711 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 707 views
+1 vote
1 answer

Apex (Salesforce) read json from response and store it in a string variable

String jsonStr = '{ "oktaToken":"eyJraWQiOiIyNlN1NHFMNnVVZTVJX2M5X2Z3WmZvX09ON0dNUHRtQzlEeHFsTGplLS00IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULlVINmdIOF9BcWJ" }'; Map<String, String> ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 27, 2023 by Khan Sarfaraz 9,626 views
0 votes
1 answer

How can I rename my Apex Class name in Salesforce

If you wanted to do it through ...READ MORE

answered Apr 1, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 19, 2023 by Khan Sarfaraz 4,298 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP