Get a variable outside FOREACH

The call subquery's results are appended to the outer query results, so when the subquery does not return a result, the outer query will stop and no results returned. So in your case, where each subquery is conditionally creating/returning results, the query will stop when either of the two 'line' properties you are checking are null; therefore, returning results from these subqueries will not work.

The following query should work, as there are no subquery return values; therefore, each subquery will execute and perform their merges when the 'where' conditions are satisfied.

load csv with headers from "file:///data.csv" as line
merge (n:Df1{Name: line.`DataField_1 Name`, Class:line.`DataField_1 Class`})

WITH line, n
call {
    with line, n
    with line, n
    where line.DataField_2 is not null

    merge (m:Df2{Name: line.`DataField_2 Name`, Class:line.`DataField_2 Class`})
    merge (n)-[:implies]-(m)
}
call{
    with line, n
    with line, n
    
    where line.`Issue Name` is not null 
    and line.DataField_2 is null

    merge (o:Issue{Name:line.`Issue Name`, Severity:line.`Issue Severity`})
    merge (n)-[:issue]-(o)
}

If you want the nodes created in the subqueries returned, then I think you will need to optionally match on them at the end.

load csv with headers from "file:///data.csv" as line
merge (n:Df1{Name: line.`DataField_1 Name`, Class:line.`DataField_1 Class`})

WITH line, n
call {
    with line, n
    with line, n
    where line.DataField_2 is not null

    merge (m:Df2{Name: line.`DataField_2 Name`, Class:line.`DataField_2 Class`})
    merge (n)-[:implies]-(m)
}
call{
    with line, n
    with line, n
    
    where line.`Issue Name` is not null 
    and line.DataField_2 is null

    merge (o:Issue{Name:line.`Issue Name`, Severity:line.`Issue Severity`})
    merge (n)-[:issue]-(o)
}

optional match (m:Df2{Name: line.`DataField_2 Name`, Class:line.`DataField_2 Class`})
optional match (o:Issue{Name:line.`Issue Name`, Severity:line.`Issue Severity`})

return n, m, o

I did some experimenting and found that the optional matches do not return values if any of the line properties are absent, so you will get a 'null' returned for 'm' and 'o' when they did not get created or didn't exists already.

You can also look a the apoc 'do.when' method:

It was used in another thread I was involved in. You can see an example there:

1 Like