+ Start a Discussion
Allister McKenzie 10Allister McKenzie 10 

Retrieve child value from relationship query

I am trying to use the case priority field, of the most recently inserted case, to change a field on the account. 
 
for (Account a : [SELECT id,(select id, priority from cases order by createddate desc limit 1) FROM account WHERE Id IN :parent]){

            if (a.Priority == 'medium') {
               // update account
            }

}

How would I do this.
Best Answer chosen by James Loghry
James LoghryJames Loghry
 
for (Account a : [SELECT id,(select id, priority from cases order by createddate desc limit 1) FROM account WHERE Id IN :parent]){

    boolean foundMediumPriorityCase = false;
    Integer i = 0;
    while(i < a.Cases.size() && foundMediumPriorityCase == false){
        if('Medium' == a.Cases.get(i).Priority){
            //Do logic here
            foundMediumPriorityCase = true;
        }
        i++;
    }    
}

Maybe not the cleanest example, but this looks for the first case with a priority of medium.