# Unable to use false from query in createIf.

**URL:** <https://community.openfn.org/t/unable-to-use-false-from-query-in-createif/41>\
**Category:** Job Writing\
**Created:** [December 20, 2018, 3:07pm UTC](https://community.openfn.org/t/unable-to-use-false-from-query-in-createif/41 "2018-12-20T15:07:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![patricksjward](https://community.openfn.org/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@patricksjward](https://community.openfn.org/u/patricksjward)\
**Post date:** [December 20, 2018, 3:07pm UTC](https://community.openfn.org/t/unable-to-use-false-from-query-in-createif/41/1 "2018-12-20T15:07:49Z")

</div>

I can now get the information from the query into the state but it's not evaluating to false in the createIf. Here's where I'm currently at:

alterState((state) =\> {  
&nbsp;&nbsp;console.log("Altering1...");  
&nbsp;&nbsp;const idCards = state.data.form.packets;  
&nbsp;&nbsp;state.data.form.packets = [12];  
&nbsp;&nbsp;var highestIndex = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;[1,2,3,4,5].forEach(function(item, index, array) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var packetSize = dataValue("form.recapitulatif\_.packet"+ item +"-size")(state);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( typeof packetSize !== "undefined" && packetSize !== "" && typeof packetSize !== 0 && packetSize !== null) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log("Packet Size", packetSize);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state.data.form.packets[highestIndex] = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;savingGoalInsertName: dataValue("form.client\_id")(state) + "-2019-" + String(dataValue("form.recapitulatif\_.packet"+ item +"-product\_name")(state)).replace(/\+/g,"%2B"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;relatedSavingGoal: dataValue("form.client\_id")(state) + "-2019-" + String(dataValue("form.recapitulatif\_.packet"+ item +"-product\_name")(state)),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;productId: dataValue("form.recapitulatif\_.packet"+ item +"-product\_id")(state),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pricebook: dataValue("form.recapitulatif\_.packet"+ item +"-pricebook\_id")(state),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;packetSize: dataValue("form.recapitulatif\_.packet"+ item +"-size")(state),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product\_type\_id: dataValue("form.recapitulatif\_.packet"+ item +"-product\_type\_id")(state)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log("index", state.data.form.packets[highestIndex] );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;highestIndex += 1;  
&nbsp;&nbsp;&nbsp;&nbsp;} else {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log("Undefined-Packet-Size for item: ", item);  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;});  
&nbsp;&nbsp;return state;  
})(state),

alterState((state) =\> {  
&nbsp;&nbsp;console.log("Altering2...");  
&nbsp;&nbsp;return query("Select Id, Client\_\_c, Program\_\_c , Membership\_End\_Date\_\_c from Program\_Membership\_\_c where Program\_\_c = 'a2H1j0000008Tt6EAE' and Client\_\_c = null")(state);  
}),

alterState((state) =\> {  
&nbsp;&nbsp;state.data.form.isProgramMembership = state.references[0].totalSize !== 0;  
&nbsp;&nbsp;return state;  
});

createIf(dataValue("form.isProgramMembership") , "Program\_Membership\_\_c", fields(  
&nbsp;&nbsp;relationship("Client\_\_r","Name", dataValue("form.isProgramMembership") ),  
&nbsp;&nbsp;field("Program\_\_c", dataValue("form.program\_id")),  
&nbsp;&nbsp;field("Membership\_Start\_Date\_\_c",dataValue("form.start\_date"))));

combine(function(state) {  
&nbsp;&nbsp;if (dataValue("form.meta.username")(state) != "kamara"  
&nbsp;&nbsp;&nbsp;&nbsp;& dataValue("form.meta.username")(state) != "eyram"  
&nbsp;&nbsp;&nbsp;&nbsp;& dataValue("form.meta.username")(state) != "noella") {  
&nbsp;&nbsp;&nbsp;&nbsp;upsert("Client\_\_c","Name", fields(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Name", dataValue("form.client\_id")(state)),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("First\_Name\_\_c", dataValue("form.demographic\_infoss.first\_name")(state)),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Last\_Name\_\_c", dataValue("form.demographic\_infoss.last\_name")),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Sex\_\_c", dataValue("form.demographic\_infoss.gender")),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Village\_Object\_\_c", dataValue("form.village\_sf\_id"))  
&nbsp;&nbsp;&nbsp;&nbsp;))(state),  
&nbsp;&nbsp;&nbsp;&nbsp;beta.each(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPath("form.packets[\*]"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;upsert("Savings\_Goal\_\_c","Name", fields(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;relationship("Client\_\_r", "Name", dataValue("form.client\_id")(state)),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Year\_\_c", "2019"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Price\_Book\_\_c",dataValue("pricebook")),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Name", dataValue("savingGoalInsertName"))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;))  
&nbsp;&nbsp;&nbsp;&nbsp;)(state),  
&nbsp;&nbsp;&nbsp;&nbsp;beta.each(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPath("form.packets[\*]"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create("Goal\_Items\_\_c", fields(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;relationship("Savings\_Goal\_\_r", "Name",dataValue("relatedSavingGoal")),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Product\_Type\_\_c", dataValue("product\_type\_id")),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Product\_\_c", dataValue("productId")),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field("Desired\_Units\_\_c", dataValue("packetSize"))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;))  
&nbsp;&nbsp;&nbsp;&nbsp;)(state);  
&nbsp;&nbsp;}  
});

If I pass in state after the first alter state function it says "Cannot read property query of undefined", if I pass it into the second it says "Cannot read property zero of undefined".

As a test, I included the value 'form.isProgramMembership' on line 41 and it is evaluating to false when there is zero rows returned.

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![Taylor\_Downs](https://community.openfn.org/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@Taylor\_Downs](https://community.openfn.org/u/Taylor_Downs)\
**Post date:** [December 20, 2018, 3:44pm UTC](https://community.openfn.org/t/unable-to-use-false-from-query-in-createif/41/2 "2018-12-20T15:44:11Z")

</div>

Hey Patrick,

Not a problem—this smells like a timing issue with promise returns. I haven’t looked at the **createIf** for quite some time, but I know that you could control the timing of execution by using another generic function. Like this:

alterState(state =\> {  
return query(  
`SELECT Field __c FROM Object__ c WHERE Ext_ID__c = '1'`  
)(state)  
})

alterState(state =\> {  
console.log(state.references);  
return createIf(  
// logical test…  
state.references[0].totalSize \> 0,  
// sObject…  
‘Contact’,  
// params…  
fields(  
field(‘Name’, ‘Taylor’)  
)  
)(state)  
});

In your specific case, try wrapping your createIf(…) function in alterState:  
alterState(state =\> {  
return createIf(…)(state)  
});

Or, using shorthand:  
alterState(state =\> (  
createIf)(state)  
);

Please let me know if this works. (It did for me, using the test code at the top of this response.) If so, it’s safe to say that createIf is _not_ well designed (probably by me!) and that we should adjust it so that (a) it doesn’t get executed until after state is returned from the previous operation and (b) you can use either a simple logical test _or_ a function that returns true/false.

Best,  
Taylor
