Question:

regarding returning 'null' in apex

Savannah: 5 days ago

I have datatable consists of checkbox and materialno. If materialno is '1' and checkbox checked it should return null. if materialno '1' and '2' selected and checkbox checked for both it should redirect to opportunity page.

For me,null is coming if 1 is selected with checkbox but if I select both its not redirecting so please help me out.

for(Movie__c m: mp){ 
 PageReference pr = new PageReference('/'+opp_id);
 if (m.checked__c==true){ 
    if(m.material_id__c=='1')
    { 
         return null;
    } 
    return pr; 
 } 
}

Thanks in advance

Answer:
Jayden: 5 days ago

If I understand your problem correctly, this might work for you.

Boolean flag1 = false;
Boolean flag2 = false;
PageReference pr = new PageReference('/'+opp_id);
for(Movie__c m: mp){ 

 if (m.checked__c==true){ 
    if(m.material_id__c=='1')
    { 
        flag1 = true;
    }
    else if(m.material_id__c=='2')
    {
        flag2= true;
    }
 } 
}

if(flag1 && flag2)
    return pr;
else
    return null;