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
BenPBenP 

Code ignoring if statement

I have a force.com site that uses a custom controller.  I'm getting very random unhandled exception error issues and finally caught a log today.  The strange part is that it's coming from a section that shouldn't even run since it's inside an IF statement.  Any reason this section would execute? 

IF statement that seems to be executing:
if( wo != null ){
                    system.debug('new wo is '+wo.Id);
                    objWO1 = [Select Id, Name From Work_Orders__c Where Id=:wo.Id];
                    //system.debug('Inside new work order*************'+objWO1.Name);
                    //system.debug('Inside Id*************'+objWO1.Id);
                    woParam = objWO1.Id;
                    woParamName = objWO1.Name;

Here's a debug log snippet.  The above code starts at line 666 for reference.  You can see that it shouldn't run if wo == null, but the debug shows that it did.
 
15:51:57.0 (1469874893)|SYSTEM_METHOD_ENTRY|[666]|WarrantyClaimController.__sfdc_wo()
15:51:57.0 (1469956278)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"acc":"0x30c3a1ee","accId":"0017000001RadedAAB","accNumber":"be671044","attfile":true,"count":0,"doAction":true,"existWO":"0x680d323e","goodPrefix":true,"lstDTOWOP":"0xa427ac2","lstExistingWOP":"0x34d35062","lstTempWOP":"0x1325b05e","mapExistingWOP":"0x3ec4bf05","mProduct":"0x19514e6d","newwo":false,"newworkorder":false,"nonExistingPrdName":"0x27c95675","partused":true,"prdNameMap":"0x9b742ae","productNameSet":"0x37aea486","reason":false,"serviceteamName":"MID-SOUTH MAINTENANC (7 more) ...","showWOD":true,"TargetLineRecordType":"0x2c0919e1","topPnl":false,"wo":"0x411e98eb","WOName":"wo-00635724","WORecordType":"0x720f0054","workorder":"0x30470007"}|0x5a62d92d
15:51:57.0 (1469968317)|SYSTEM_METHOD_EXIT|[666]|WarrantyClaimController.__sfdc_wo()
15:51:57.0 (1469972333)|STATEMENT_EXECUTE|[666]
15:51:57.0 (1469973796)|STATEMENT_EXECUTE|[667]
15:51:57.0 (1469978048)|HEAP_ALLOCATE|[667]|Bytes:10
15:51:57.0 (1469985060)|SYSTEM_METHOD_ENTRY|[667]|WarrantyClaimController.__sfdc_wo()
15:51:57.0 (1470063224)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"acc":"0x30c3a1ee","accId":"0017000001RadedAAB","accNumber":"be671044","attfile":true,"count":0,"doAction":true,"existWO":"0x680d323e","goodPrefix":true,"lstDTOWOP":"0xa427ac2","lstExistingWOP":"0x34d35062","lstTempWOP":"0x1325b05e","mapExistingWOP":"0x3ec4bf05","mProduct":"0x19514e6d","newwo":false,"newworkorder":false,"nonExistingPrdName":"0x27c95675","partused":true,"prdNameMap":"0x9b742ae","productNameSet":"0x37aea486","reason":false,"serviceteamName":"MID-SOUTH MAINTENANC (7 more) ...","showWOD":true,"TargetLineRecordType":"0x2c0919e1","topPnl":false,"wo":"0x411e98eb","WOName":"wo-00635724","WORecordType":"0x720f0054","workorder":"0x30470007"}|0x5a62d92d
15:51:57.0 (1470075680)|SYSTEM_METHOD_EXIT|[667]|WarrantyClaimController.__sfdc_wo()
15:51:57.0 (1470095580)|SYSTEM_METHOD_ENTRY|[667]|String.valueOf(Object)
15:51:57.0 (1470105725)|SYSTEM_METHOD_EXIT|[667]|String.valueOf(Object)
15:51:57.0 (1470112295)|HEAP_ALLOCATE|[667]|Bytes:14
15:51:57.0 (1470121834)|SYSTEM_METHOD_ENTRY|[667]|System.debug(ANY)
15:51:57.0 (1470126594)|USER_DEBUG|[667]|DEBUG|new wo is null
15:51:57.0 (1470130133)|SYSTEM_METHOD_EXIT|[667]|System.debug(ANY)
15:51:57.0 (1470133136)|STATEMENT_EXECUTE|[668]
15:51:57.0 (1470136876)|HEAP_ALLOCATE|[668]|Bytes:55
15:51:57.0 (1470144596)|HEAP_ALLOCATE|[668]|Bytes:4
15:51:57.0 (1470155526)|SYSTEM_METHOD_ENTRY|[668]|WarrantyClaimController.__sfdc_wo()
15:51:57.0 (1470235255)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"acc":"0x30c3a1ee","accId":"0017000001RadedAAB","accNumber":"be671044","attfile":true,"count":0,"doAction":true,"existWO":"0x680d323e","goodPrefix":true,"lstDTOWOP":"0xa427ac2","lstExistingWOP":"0x34d35062","lstTempWOP":"0x1325b05e","mapExistingWOP":"0x3ec4bf05","mProduct":"0x19514e6d","newwo":false,"newworkorder":false,"nonExistingPrdName":"0x27c95675","partused":true,"prdNameMap":"0x9b742ae","productNameSet":"0x37aea486","reason":false,"serviceteamName":"MID-SOUTH MAINTENANC (7 more) ...","showWOD":true,"TargetLineRecordType":"0x2c0919e1","topPnl":false,"wo":"0x411e98eb","WOName":"wo-00635724","WORecordType":"0x720f0054","workorder":"0x30470007"}|0x5a62d92d
15:51:57.0 (1470247773)|SYSTEM_METHOD_EXIT|[668]|WarrantyClaimController.__sfdc_wo()
15:51:57.0 (1470419544)|SOQL_EXECUTE_BEGIN|[668]|Aggregations:0|SELECT Id, Name FROM Work_Orders__c WHERE Id = :tmpVar1
15:51:57.0 (1470431761)|LIMIT_USAGE|[668]|SOQL|24|100
15:51:57.0 (1470442563)|LIMIT_USAGE|[668]|AGGS|6|300
15:51:57.0 (1472390070)|SOQL_EXECUTE_END|[668]|Rows:0
15:51:57.0 (1472397967)|LIMIT_USAGE|[668]|SOQL_ROWS|20|50000
15:51:57.0 (1472413064)|HEAP_ALLOCATE|[668]|Bytes:4
15:51:57.0 (1472422707)|HEAP_ALLOCATE|[668]|Bytes:0
15:51:57.0 (1472442489)|HEAP_ALLOCATE|[668]|Bytes:4
15:51:57.0 (1472454664)|HEAP_ALLOCATE|[668]|Bytes:40
15:51:57.0 (1472508465)|HEAP_ALLOCATE|[668]|Bytes:46
15:51:57.0 (1472646397)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

 
Alain CabonAlain Cabon
if( wo != null ){ system.debug('new wo is '+wo.Id); }

The two variables are different. The objet is not null but its id is null.

Try this anonymous coude in the developer console:
Account acc = new Account();
if (acc != null) {
   system.debug('acc : ' + acc);
   system.debug('acc.id : ' + acc.id);
}

Alain
BALAJI CHBALAJI CH
Hi BenP,

@Alain Cabon is exactly right. Here you are checking with the object variable wo in IF condition and querying records with its Id i.e., wo.Id

Try changing your IF condition as below:
if( wo.Id != null ){
                    system.debug('new wo is '+wo.Id);
                    objWO1 = [Select Id, Name From Work_Orders__c Where Id=:wo.Id];
                    //system.debug('Inside new work order*************'+objWO1.Name);
                    //system.debug('Inside Id*************'+objWO1.Id);
                    woParam = objWO1.Id;
                    woParamName = objWO1.Name;

Let us know if that helps you.

Best Regards,
BALAJI
BenPBenP
Thank you for the replies.  I understand what you are saying, but changing to wo.Id is causing an issue (Attempt to de-reference a null object).

More information is that this line wo = new Work_Orders__c(); is inside a method that wasn't called when it failed, so it should be null.  Also, this doesn't fail all the time just randomly.

maybe I should set another string for each of the two scenarios I have?  This can be a new wo or an existing wo.