Calling apoc stored procedure from the MATCH cypher query

I have written below query

MATCH (p:Person)

WITH p, apoc.do.when(false,

'CREATE (a:Node{name:"A"}) RETURN a AS node',

'CREATE (b:Node{name:"B"}) RETURN b AS node',

{}

) as nodes

Return *

When I run this above query this is returning

Unknown function 'apoc.do.when'

Can any one help me here, I have installed apoc plugin and when try to run separately its working

#apoc #cypher

The issue is that apoc.do.when is a method, not a function. As such you need a ‘call’ in front of the apoc method.

I am confused of what your query is trying to accomplish. As written, the query does nothing is no Person node exists, while it will always create a Person node with ‘name’ equal to ‘B’ if a Person node does exist. What outcome are you looking for?

Try this:

OPTIONAL MATCH (p:Person)
WITH COALESCE(p)as p1
CALL apoc.do.when(p1 is null,
  'CREATE (a:Node{name:"A"}) RETURN a AS node',
  'CREATE (b:Node{name:"B"}) RETURN b AS node',
  {}
) as nodes
Return *

Getting below exception
Invalid input 'apoc': expected

@glilienfield Thank you so, above code works fine, But in my case I want to create virtual node along with virtual relation between virtual node and original node based on multiple case statements

CALL apoc.do.case and create virtual relation from virtual node along with passing some property value from Person node. Like below

CALL apoc.do.case( [

true,

apoc.create.vRelationship(apoc.create.vNode(["RightPerson"],{ key: p.value }), ,'VR', {}, p ) yield node, rel,

false,

apoc.create.vRelationship(apoc.create.vNode(["WrongPerson"],{ key: p.value }), ,'VR', {}, p ) yield node, rel

],

apoc.create.vRelationship(apoc.create.vNode(["WrongPerson"],{ key: p.value }), ,'VR', {}, p ) yield node, rel ,

{}

)yield value

return p, value.

I want to create switch case and if case match create virtual node and virtual relation with source node as virtual node and target node as Matched node e.g p: Person, with one of the attribute of person node

Try this:

match(p:Person)
CALL apoc.do.case([
true,
'
    with apoc.create.vNode(["RightPerson"],{ key: p.value }) as vNode
    return apoc.create.vRelationship(vNode, "VR", {}, $p ) as vRelationship, vNode
',
false,
'
    with apoc.create.vNode(["WrongPerson"],{ key: p.value }) as vNode
    return apoc.create.vRelationship(vNode, "VR", {}, $p ) as vRelationship, vNode
'],
'
    with apoc.create.vNode(["WrongPerson"],{ key: p.value }) as vNode
    return apoc.create.vRelationship(vNode, "VR", {}, $p ) as vRelationship, vNode
',
{p:p}
) yield value
return p as person, value.vNode as vNode, value.vRelationship as vRelationship

I assume you will replace the conditions 'true' and 'false' with real conditions. Also, the virtual relationships is the same for all three cases, so it could be removed from the 'do.case' statement and executed after the 'do.case' statement. I left it as is, in case you wanted to add different relationship properties or types.

@glilienfield Thanks for the reply.

I am trying to achieve based on of the attribute value from person node I want to create virtual node.

and returning original relationship along with create virtual relationship with virutalNode.

OPTIONAL MATCH (p:Person) where p.type='Doctor'
WITH p
CALL apoc.do.when(p.location=IN,
  'CREATE virtualNode YIELD a AS node',
  'CREATE virtualNode YIELD b AS node',
  {}
) as nodes
Return *

Got it...try this. I didn't know what you wanted to set for the properties of the virtual nodes, so I added place holders. You can configure the maps to set all the virtual node properties you need. You can pass values through the apoc.do.when method's parameters map, which is empty right now, if you need properties from the 'p' node.

match (p:Person) 
where p.type='Doctor'
call apoc.do.when(p.location='IN',
  'call apoc.create.vNode(["Person"],{key: value}) YIELD node',
  'call apoc.create.vNode(["Person"],{key: value}) YIELD node',
  {}
) yield value
return p, value.node