# Can't return a value after Apoc.do.when() runs

**URL:** https://community.neo4j.com/t/cant-return-a-value-after-apoc-do-when-runs/63349
**Category:** Neo4j Graph Platform
**Tags:** apoc, cypher
**Created:** [July 19, 2023, 8:47pm UTC](https://community.neo4j.com/t/cant-return-a-value-after-apoc-do-when-runs/63349 "2023-07-19T20:47:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![graphene](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/graphene/32/28082_2.png) [@graphene](https://community.neo4j.com/u/graphene)
#### Post date: [July 19, 2023, 8:47pm UTC](https://community.neo4j.com/t/cant-return-a-value-after-apoc-do-when-runs/63349/1 "2023-07-19T20:47:43Z")

</div>

I am trying to merge a user node and return true if the user node was created and false if the user node already existed. And if optional properties come in with the query, add them to the db.  
Here is my query:

```auto
  CALL apoc.merge.node($labels, $identityProps, {created: true}, {created: false})
  YIELD node AS u
  WITH u
  CALL apoc.do.when(
    $optionalProps.advertisingId IS NOT NULL,
    "SET u.limitAdTracking = $optionalProps.limitAdTracking
    MERGE (a:AdvertisingId {advertisingId: $optionalProps.advertisingId})
    MERGE (u)-[:HAS_ADVERTISING_ID]->(a)",
    "",
    {u:u, optionalProps: $optionalProps}
    )
  YIELD value
  WITH u.created AS created, u
  REMOVE u.created
  RETURN created

```

As you can see, I set a temporary property on the user node in the first line, then assign that value to a return value called `created`, and then delete the temporary property before returning.

When I don't send `$optionalProps`, it works as expected: I get a return value of `{created: boolean}` and the `created` property on the user node is deleted at the end of the query.

When I do send `$optionalProps` (and `$optionalProps.advertisingId` is not null), It creates the user node, adds the `created` property, runs the ifQuery, all as expected , but then it returns nothing and doesn't delete `u.created`.  
It seems like if apoc.do.when runs the ifQuery, I can't get it to do anything after the apoc.do.when. If I change the last few lines to  
`YIELD value RETURN value`  
I get nothing back.  
same with  
`YIELD value RETURN u`

I'm pretty stumped.

I'm using the javascript driver 5.1.0 like this (but I get the same behavior in the neo4j browser):

```auto
return await session.executeWrite((tx) => tx.run(query, {labels, identityProps, optionalProps));

```

---

<div class="post-metadata">

### Author: ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)
#### Post date: [July 20, 2023, 1:51am UTC](https://community.neo4j.com/t/cant-return-a-value-after-apoc-do-when-runs/63349/2 "2023-07-20T01:51:38Z")

</div>

Try returning something from each branch of the apoc.do.when procedure. You can try 'return 1', since you already have the node 'u'. Also, the preferred method of removing an attribute is to set its value to 'null'

---

<div class="post-metadata">

### Author: ![graphene](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/graphene/32/28082_2.png) [@graphene](https://community.neo4j.com/u/graphene)
#### Post date: [July 20, 2023, 5:37pm UTC](https://community.neo4j.com/t/cant-return-a-value-after-apoc-do-when-runs/63349/3 "2023-07-20T17:37:18Z")

</div>

that works, thanks!

I thought the ifQuery would just be a part of one query with the rest of it, but looks like it is sort of a separate operation? Or are there other situations where you can have two RETURNs in one query?

---

<div class="post-metadata">

### Author: ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)
#### Post date: [July 21, 2023, 1:07am UTC](https://community.neo4j.com/t/cant-return-a-value-after-apoc-do-when-runs/63349/4 "2023-07-21T01:07:39Z")

</div>

The apoc procedure will execute your write statements, but will not return anything if there is no return statement. As such, value will be null and will cause the query to stop and you will see '(no changes, no records)' in the browser or no result when using a driver.
