Thursday, 4 February 2021

Adobe Campaign: Javascript XML E4X Switch Case

Given the following table data model in Adobe Campaign Data Model

I have developed the following script, which is a mix of javascript, xml, e4x (used by adobe campaign). The script basically iterates through each line and executes for each switch the code inside the cases. I am looking for a way to simplify the switches/cases as they are a bit redundant? can anyone suggest a better approach?

  var query = xtk.queryDef.create(
               <queryDef schema="temp:enrich" operation="select">
                  <select>                                       
                    <node expr="@id"/>
                    <node expr="@fun"/>
                    <node expr="@news"/>
                    <node expr="@events"/>
                    <node expr="@student"/>                       
                  </select> 
               </queryDef>)    
  var result = query.ExecuteQuery();  

for each(var i in result.enrich)
{
    //Debug: logInfo(i.@id+ " "+i.@fun+" " +i.@news+" " +i.@student);
    var recipient = <recipient _key = "@id" id = {i.@id} />;
    
    var fun     = parseInt(i.@fun);
    var news    = parseInt(i.@news);
    var events  = parseInt(i.@events);
    var student = parseInt(i.@student);    

    switch(fun) {
      case 0:
        nms.subscription.Unsubscribe("uosFunStuff", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosFunStuff", recipient,false);
        break;
      default:
        // donothing
    }        
    switch(news) {
      case 0:
        nms.subscription.Unsubscribe("uosUniversityNews", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosUniversityNews", recipient,false);
        break;
      default:
        // donothing
    }         
    switch(events) {
      case 0:
        nms.subscription.Unsubscribe("uosEvents", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosEvents", recipient,false);
        break;
      default:
        // donothing
    }     
    switch(student) {
      case 0:
        nms.subscription.Unsubscribe("uosStudentLife", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosStudentLife", recipient,false);
        break;
      default:
        // donothing
    }      

}


from Adobe Campaign: Javascript XML E4X Switch Case

No comments:

Post a Comment