Adobe Campaign: Call Java Script from Input Form

I came across a scenario where I need to do some custom auditing when some fields are  changed via an input form. So for that I had to call javascript functions from an input form.
I have custom schema as cus:Preference with 1:1 join with nms:Recipient. And audit was required to capture who is making change to the preference page if any.

Step 1: Create an entry in the input form for the soapCall. 
<form>
  <container >
<!--Enter the form fields here-->
  </container>
  <leave>
<!--servi = "name of the schema where the method is declared"-->
    <soapCall name="auditPreference" service="cus:Preference">
      <param exprIn="[@id]" type="int"/>
      <param exprIn="[@recipientId]" type="int"/>
      <param exprIn="[@prefAllCommunications]" type="boolean"/>
      <param exprIn="[@prefAllCommunicationsModifiedDate]" type="datetime"/>
    </soapCall>
  </leave>
</form>

Step 2: Add the method signature in the schema cus:Preference

<srcSchema>
<element>
<!--Enter the actual schema fields here-->
</element>
<methods>
    <method library="cmn:preference" name="auditPreference" static="true">
      <help>Capture Audit on preference data update</help>
      <parameters>
      <param desc="Preference Id" inout="in" name="preferenceId"
           type="long"/>
      <param desc="Recipient Id" inout="in" name="recipientId"
           type="long"/>  
      <param desc="Pref All Communications" inout="in" name="prefAllCommunications"
           type="boolean"/>
      <param desc="prefAllCommunicationsModifiedDate" inout="in" name="prefAllCommunicationsModifiedDate"
      </parameters>
    </method>

  </methods>
</srcSchema>

Step 3: Declare the function in JavaScript
Create the javascript file "cmn:preference" and add the below fiuction
Note: Function name: <schema-namespace>_<schema-name>_<method-name>

function cus_Preference_auditPreference(preferenceId, recipientId, prefAllCommunications, prefAllCommunicationsModifiedDate)
{
  logInfo("Inside cus_Preference_auditPrvPreference");
  var query = NLWS.xtkQueryDef.create(
    {queryDef: {schema: "cus:preference", operation: "select",
    select: {
        node: [{expr: "@id"},
               {expr: "@prefAllCommunications"},
               {expr: "@prefAllCommunicationsModifiedDate"}]
    },
    where: {
      condition: [{expr: "@id = " + preferenceId.toString()},
                  {expr: "@recipientId = "+ recipientId.toString()}]
    }
  }})

  var res = query.ExecuteQuery();

  var prefs = res.getElementsByTagName("preference")

  var logMessage = "";
  for each (var pref in prefs)
  {
    var id = pref.getAttribute("@id");
    var oldPrefAllCommunications = pref.getAttribute("@prefAllCommunications");
    var oldPrefAllCommunicationsModifiedDate = pref.getAttribute("@prefAllCommunicationsModifiedDate");
 
    logMessage = "Id:" + id + " prefAllCommunications:" + prefAllCommunications + " prefAllCommunicationsModifiedDate:" + prefAllCommunicationsModifiedDate;
    logMessage += "Old values: prefAllCommunications:" + oldPrefAllCommunications + " prefAllCommunicationsModifiedDate:" +oldPrefAllCommunicationsModifiedDate;
    logCustomInfo(preferenceId, "Preference", logMessage);
 
    //Check if the values have changed
    if(prefAllCommunicationsModifiedDate != prefAllCommunications)
    {
      //Update in the audit table
      var userInfo = xtk.session.GetUserInfo();
      //alert(userInfo.@loginCS);
      var user = userInfo.@loginCS;
      logMessage = "prefAllCommunications updated by user:" + user + " at " + formatDate(getCurrentDate(),"%4Y/%2M/%2D %02H:%02N:%02S");
      logMessage +="\nOld Value:" + oldPrefAllCommunications + "\nNew value:" + prefAllCommunications;
      logCustomInfo(preferenceId, "Preference", logMessage);
// Using the same logCustomInfo as example. ideally, this audit should be saved into separate table
    }  
    break;
  }
}


I realized that the logInfo in this case doesn't write the log as there is no workflow associated with it. So I created a custom basic logging mechanism.
Created a database table as CustomLog

CREATE TABLE [neolane].[CustomLog](
[RecordID] [bigint] NULL,
[RecordType] [varchar](100) NULL,
[CustomLog] [varchar](max) NULL,
[LogDate] [datetime2](7) NULL
)

Then created a function in the same js file as below
function logCustomInfo(recordId,recordType, info)
{
  var sql = "Insert into CustomLog ([RecordID],[RecordType], [CustomLog]) values ($(l),$(sz),$(sz))";
  var recordsDeleted = sqlExec(sql,recordId,recordType, info);
}

Comments

Popular posts from this blog

Avoid Proxy for HttpClientRequest - IOB-090007 Network error (send(), errno=10054: an existing connection was forcibly closed by the remote host

Broadlog Resequencing in Adobe Campaign Classic