Writing If-Else Within the fields portion of a JOB

7 views
Skip to first unread message

Mathew Juma

unread,
Mar 12, 2020, 12:45:36 PM3/12/20
to OpenFn Community

Is it  correct to write if-else within the fields portion of openfn? as below:

.
.
fields(

if(state.data.form_field_a == "xxxxx"){
field("field_1__c", dataValue("form.value_1")),
field("field_2__c", dataValue("form.value_2")),         
field("field_3__c", dataValue("form.value_3")),
field("field_4__c", dataValue("form.value_4"))
else if (state.data.form_field_a == "yyyyyy" || state.data.form_field_a == "zzzzz"){
field("field_1__c", dataValue("form.value_5")),
field("field_2__c", dataValue("form.value_6"))
}

)
.
.

NB: Job submission target is Salesforce

Taylor Downs

unread,
Mar 13, 2020, 8:50:04 AM3/13/20
to OpenFn Community
There are a bunch of options here. Would && or a ternary work for you? See below:

upsert(
'Patient__c',
'Patient_ID__c',
fields(
field('Name__c', dataValue('name')),
field('b', 2),
// Omit field if test fails
state.data.testValue > 1 && field('c', dataValue('something')),
// Omit field with ternary if test fails
state.data.anotherTest ? field('d', 13) : '',
// Ternary to change field
state.data.finalTest ? field('e', 16) : '',
// Ternary to change value
field('f', state => {
console.log('this is common place to calculate a value');
const something = state.data.thatOne;
const somethingElse = 4 * 2.42 + state.data.testValue;
if (state.data.test === 'foo') {
return something;
}
return somethingElse;
})
)
);
Reply all
Reply to author
Forward
0 new messages