Expand my Community achievements bar.

SOLVED

getIfExists results and based on that if and else in Script activity

Avatar

Level 4

So I am getting error when I try to get if statements on the result set that i get it from below query. 

MEmail is variable i decleared in page and based on that i am running query against recipient to check if it exists.  if record exists , then send one delivery else send another is plan. 

 

var query = xtk.queryDef.create(
<queryDef schema="nms:recipient" operation="getIfExists">
<select>
<node expr="@email"/>
<node expr="@id"/>
</select>
<where>
<condition expr={"@email='"+ctx.vars.MEmail+"'"}/>
</where>
</queryDef>
)


var res = query.ExecuteQuery()

 

 

// Send email

 if(res.@email!=''){

  nms.delivery.QueueNotification('mediaconfirmationwid',

  <delivery>

   <targets>

      <deliveryTarget>

        <targetPart type='query' exclusion='false' ignoreDeleteStatus='false'>

          <where>

            <condition expr={'@id ='+ res.@id}/>

         </where>

       </targetPart>

      </deliveryTarget>

   </targets>

   </delivery>)

  }

  

  if(res.@email=''){

  nms.delivery.QueueNotification('mediaconfirmationwid',

  <delivery>

   <targets>

      <deliveryTarget>

        <targetPart type='query' exclusion='false' ignoreDeleteStatus='false'>

          <where>

            <condition expr={'@id ='+ res.@id}/>

         </where>

       </targetPart>

      </deliveryTarget>

   </targets>

   </delivery>)

  }

 

if I remove if statement i get delivery but the moment i add if and conditions i dont get delivery. 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

//Defined getIfExists

var query = xtk.queryDef.create(

<queryDef schema="nms:recipient" operation="getIfExists">

<select>

<node expr="@email"/>

<node expr="@id"/>

</select>

<where>

<condition expr={"@email='"+ctx.recipient.@email+"'"}/>
<condition expr={"@sourceType='Media'"}/>

</where>
</queryDef>
)


var res = query.ExecuteQuery()

 

//Run the query

var res = query.ExecuteQuery()

 

//Called if statement worked 

if(res.@email.length()>0){

nms.delivery.QueueNotification

View solution in original post

10 Replies

Avatar

Level 3

Hi @Prasanna_Soni,

 

I tried executing the below js in the web app.  I am able to receive the delivery for 1st if(res.@email!='').

 

You can use the below condition in the 2nd if statement to check the email returned by the query is empty.

if(res.@email=="" || res.@email==undefied)

{

}

But, if the email passed in MEmail variable is not present in the recipient then you will not get @id in the return. so delivery will not get executed(as you have given the condition @id=res.@id). 

 

 

Thanks,

Amit

 

Avatar

Level 4

You are right .. for 2nd Condition I should send delivery to email variable that we got 

 

if(res.@email=="" || res.@email==undefied <mailto:res.@email==undefied> ) {
nms.delivery.QueueNotification('mediaconfirmation',

<delivery>

<targets>

<deliveryTarget>

<targetPart type='query' exclusion='false' ignoreDeleteStatus='false'>

<where>

<condition expr={'@email='+ctx.vars.MediaEmail}/>

</where>

</targetPart>

</deliveryTarget>

</targets>

</delivery>)

}

Avatar

Level 3

You need to insert the email id in the recipient before sending the delivery as delivery will not execute if the email id is not present in the recipient.

Avatar

Level 4
Yes that make sense and Recipient will be email address from Variable @MEmail .. But how do i insert that i am not sure.

Avatar

Level 3

Use the below script.

 

var rcpt = <recipient xtkschema = "nms:recipient" _operation = "insert" email={ctx.vars.MEmail}/>;

try {
xtk.session.Write(rcpt);

} catch(e) {

}

Avatar

Level 4

Let me give a try

 

however I was also thinking to pass MEmail variable to 2nd if (else) part of delivery and remove query from target part. 

 

nms.delivery.QueueNotification('mediaconfirmation',

<delivery>

<targets>

<deliveryTarget>

<targetPart exclusion='false' ignoreDeleteStatus='false'>

<where>

<condition expr={"@email='"+ctx.vars.MediaEmail+"'"}/>

</where>

</targetPart>

</deliveryTarget>

</targets>

</delivery>)

Avatar

Level 4

@AmitNope insert idea seems did not worked 

 

if(res.@email!=''){

nms.delivery.QueueNotification('mediaconfirmationwid',

<delivery>

<targets>

<deliveryTarget>

<targetPart type='query' exclusion='false' ignoreDeleteStatus='false'>

<where>

<condition expr={'@id ='+ res.@id}/>

</where>

</targetPart>

</deliveryTarget>

</targets>

</delivery>)

} else


{

var rcpt = <recipient xtkschema = "nms:recipient" _operation = "insert" email={ctx.vars.MediaEmail}/>;

xtk.session.Write(rcpt);

nms.delivery.QueueNotification('mediaconfirmation',

<delivery>

<targets>

<deliveryTarget>

<targetPart type='query' exclusion='false' ignoreDeleteStatus='false'>

<where>

<condition expr={'@email='+ctx.vars.MediaEmail}/>

</where>

</targetPart>

</deliveryTarget>

</targets>

</delivery>)


}

Avatar

Level 3

Try below condition in else part.

 

<where>

<condition expr={"@email='"+ctx.vars.MediaEmail+"'"}/>

</where>

Avatar

Administrator

Hi @Prasanna_Soni,

Were you able to resolve this query or do you still need more help here? Do let us know.

Thanks!



Sukrity Wadhwa

Avatar

Correct answer by
Level 4

//Defined getIfExists

var query = xtk.queryDef.create(

<queryDef schema="nms:recipient" operation="getIfExists">

<select>

<node expr="@email"/>

<node expr="@id"/>

</select>

<where>

<condition expr={"@email='"+ctx.recipient.@email+"'"}/>
<condition expr={"@sourceType='Media'"}/>

</where>
</queryDef>
)


var res = query.ExecuteQuery()

 

//Run the query

var res = query.ExecuteQuery()

 

//Called if statement worked 

if(res.@email.length()>0){

nms.delivery.QueueNotification