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
CeeMoCeeMo 

If statement in method returns opposite value of what is expected

hello

The below code when run is returning the opposite value of what the debug logs show.
 
List <Id> leadsToConvert = new List<Id>();
leadsToConvert.add(leadId);

List<Id> convertedOpps = new List<Id>();
convertedOpps = ConvertLead.ConvertLeads(leadsToConvert);
        
--> this debug statement shows the Opportunity ID and that the size is 1
System.debug('LeadUtilities.convertTheLead Opp Id = ' + convertedOpps[0] + '  - size = ' + convertedOpps.size());

--> the if statement returns Not Converted even though the size is 1
if (convertedOpps.size() > 0) 
   return 'Converted';
else
   return 'Not Converted';
Here are the debug statements that show the size of the list being returned from convertLeads newOpp (1) and the size of the list in the convertTheLead method (1).
User-added image
I have been unable to find anything to explain why this would happen. Has anyone run into this and been able to resolve the issue? Or is their something that I have done that would cause this.

Thank you for any help for a newbie apex developer.

CeeMo
Best Answer chosen by CeeMo
Tarun J.Tarun J.
By looking into code, it seems like it should have to work. Can you check by adding debug logs under IF and ELSE block?

-Thanks,
TK

All Answers

Tarun J.Tarun J.
By looking into code, it seems like it should have to work. Can you check by adding debug logs under IF and ELSE block?

-Thanks,
TK
This was selected as the best answer
rajat Maheshwari 6rajat Maheshwari 6

Hi CeeMo,

Please give a try with :- if (convertedOpps.size() == 1) return 'Converted'; else return 'Not Converted';

CeeMoCeeMo
Thank you all for your quick responses. After adding debug logs under the IF and ELSE I found that the code was going through the right branch. The issue ended up being in the Javascript button we were using = rather than ==. Thank you all again from this newbie.

CeeMo