Client-side scripts

Run from Action or Java Script

A client-side script can be called from an action element, as well as from an HTML JS element The parameters for the action and the call to XP.ExecAction() can be found in the provided action examples in the designer, as well as here: Actions and Plugins.

Option 1: Action

frmactn

Option 2: JavaScript

<!-- Dump all from JS -->
<script>
    function MyDump()
    {
        XP.ExecAction('BATCH', 'RUN', 'Name=SkriptDumpAll.bat|Params=MyAppLinkParam');
    }
</script>
<div>
    <a id="myLink" href="#" onclick="MyDump();">DumpAll als JS</a>
</div>

Data transfer in environment variables

The data context of a dashboard is copied to the environment variables of the process when a batch file or script is called. The notation of the data corresponds to that of the data fields described in Variable names of the data fields. The environment variables additionally have the prefix

  • XP.Contact

  • XP.Call

  • XP.User

The call number of a call partner can be found, for example, in the environment variable XP.Call.CALLNO. Please note that environment variables (unlike JavaScript) are not case-sensitive. In addition to the data for Contact, User and Call, the following can also be used:

  • XP.Data

  • XP.Data

These objects are intended for the transmission of form data and action parameters.

Hint

The contents correspond to the JS variables under JavaScript library and data objects.

Batch file with embedded script

In many cases it is useful to extend the limited functionality of batch files via scripting. The example shows how to create a “hybrid” file. The commands of the batch file are moved to a comment section that is only valid for scripting. The file is executed twice when a command is executed: Once, via the command line interpreter (CMD) and the second time from the batch section via the scripting host. So the only advantage is that there is no need to maintain two files. The example calls the Notepad program and passes the first command line parameter as file name. A special feature is the enclosing of the script passing parameter in quotes “escaped” by the carat symbol (“^”). This syntax allows parameters to be passed with spaces. The passing of parameters via the command line is only available for compatibility reasons with older XPhone Connect versions. The example script shows the current variant of parameter passing via environment variable.

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf" ^""%1"^"
rem cscript //nologo //D //x "%~f0?.wsf" ^""%1"^" 'debug version
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
rem get access to data context
set wshShell = wscript.CreateObject( "wscript.Shell" )
Set wshProcEnv = wshShell.Environment( "PROCESS" )
rem get command line parameter
Params = wshProcEnv( "xp.action.params" )
rem Params contains data from Link-URL "Params=..."
</script></job>

The commented out call (rem) of the scripting host (cscript) with the parameters //D //x in the example calls the script in debug mode. This allows control of variables and script execution on the workstation if a working script debugger (e.g. Visual Studio) is installed. See also Microsoft Support - Script Debugging.

Caution

Note that the script (or debugger) is called only if the script does not contain syntax errors. Since the output of the command line interpreter is suppressed, syntax error messages are not visible. Therefore, check the script via a manual call of the batch file from the command line.

Examples client scripts

Data output (dump)

The code shows a button that leads to the dashboard context data screen display. In addition, two custom parameters are appended to the data context.

HTML/JS element

<!-- Dump all from JS -->
<script>
    function MyDump()
    {
        XP.Call.MyNewCallParam1="NewCallParam1";
        XP.Data.MyNewParam1 = "NewParam1";
        XP.ExecAction('BATCH', 'RUN', "Name=SkriptDumpAll.bat");
    }
</script>
<div>
    <a id="myLink" href="#" onclick="MyDump();">DumpAll als JS</a>
</div>

Alternatively, an action can be used instead of the HTML/JS element:

action-dump

Batch script

The script SkriptDumpAll.bat must be located in the directory <Drive>:\Program Files (x86)\Common Files\C4B\AppLink and have the following content:

<!-- : Begin batch script SkriptDumpAll.bat
@echo off
cscript //nologo "%~f0?.wsf"
rem cscript //nologo //D //x "%~f0?.wsf" ^""%1"^" 'debug version
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
set wshShell = wscript.CreateObject( "wscript.Shell" )
Set wshSystemEnv = wshShell.Environment( "PROCESS" )
For Each strItem In wshSystemEnv
    rem data contexts are: xp.contact, xp.call, xp.user, xp.data, xp.action
        pos = InStr( 1, strItem, "XP.", vbTextCompare )
        if pos <> 0 then
            xpEnv = xpEnv + vbCrlf + strItem
    end if
Next
wshShell.Popup xpEnv, AUTO_DISMISS, "Dump output", OK_BUTTON
</script></job>

Create Outlook task

<!-- : Begin batch script SkriptOlTask.bat
@echo off
cscript //nologo "%~f0?.wsf" ^""%1"^"
rem debug version: cscript //nologo //D //x "%~f0?.wsf" ^""%1"^"
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
Dim olApp, myTask, name
set wshShell = wscript.CreateObject( "wscript.Shell" )
Set wshSystemEnv = wshShell.Environment( "PROCESS" )
Set olApp = GetObject(, "Outlook.Application")
Set myTask = olApp.CreateItem(3) ' olTaskItem
name = wshSystemEnv( "xp.contact.NAME" )
myTask.Subject = "Anruf von - " & name
myTask.Display false
</script></job>

Check call status

The script checks the status of a call using the XP.Call.STATE variable:

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf"
rem cscript //nologo //D //x "%~f0?.wsf" ^""%1"^" 'debug version
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
set wshShell = wscript.CreateObject( "wscript.Shell" )
Set wshSystemEnv = wshShell.Environment( "PROCESS" )
state = wshSystemEnv( "XP.Call.STATE" )
if (state and 2) <> 0 then
    xpEnv = xpEnv + vbCrlf + "State: Ringing"
end if
if (state and 4) <> 0 then
    xpEnv = xpEnv + vbCrlf + "State: Connected"
end if
if (state and 1) <> 0 then
    xpEnv = xpEnv + vbCrlf + "State: Outgoing"
end if
For Each strItem In wshSystemEnv
    rem WScript.Echo strItem
    pos = InStr( 1, strItem, "XP.", vbTextCompare )
    if pos <> 0 then
        xpEnv = xpEnv + vbCrlf + strItem
    end if
Next
wshShell.Popup xpEnv, AUTO_DISMISS, "Dump output", OK_BUTTON
</script></job>

Pass parameters to batch file in action

The script reads the call partner’s phone number from the environment variable xp.call.callno. If a value for params was passed in Action, it is available in the script variable params:

<!-- : Begin batch script dumpcallno.bat
@echo off
cscript //nologo "%~f0?.wsf"
rem cscript //nologo //D //x "%~f0?.wsf" ^""%1"^" 'debug version
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
set wshShell = wscript.CreateObject( "wscript.Shell" )
Set wshSystemEnv = wshShell.Environment( "PROCESS" )
params = wshProcEnv( "xp.action.params" ) rem if the Applink has a Params-value, it is delivered here
callno = wshSystemEnv( "xp.call.callno" )
wshShell.Popup callno, AUTO_DISMISS, "Dump output", OK_BUTTON
REM ***** process content of "callno" here *********
</script></job>

Create new Word document

The example shows how a Word document is created via an action, which is supplied with data from the dashboard after opening, for example to create a personalized letter.

The example consists of an action, a script and a Word document template. The action calls the script and passes the name and path of a document template.

A new document is created from the document template. The script copies the elements of the data context into document variables ( the name is kept). To display the data at specific locations in the document, Word field functions must be inserted at those locations. These field functions must reference the corresponding document variables.

Action

word

The params parameter contains a UNC path. Due to limitations of the current version of XPhone Connect, double backslash characters cannot be passed. For this reason, the script places another backslash in front of the path.

Batch file

For replacing the document variables with the data context from the dashboard:

<!-- : Begin batch script SkriptNewWordDoc.bat
@echo off
cscript //nologo "%~f0?.wsf" ^""%1"^"
rem debug version: cscript //nologo //D //x "%~f0?.wsf" ^""%1"^"
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
Dim pos, wdApp, wdVar, wdDocName, wdDoc, wdField, wshEnv, key, value
On Error Resume Next
rem get access to data context
set wshShell = wscript.CreateObject( "wscript.Shell" )
Set wshProcEnv = wshShell.Environment( "PROCESS" )
rem get dot-file name
wdDocName = wshProcEnv( "xp.action.params" ) rem this is, where the Params section is transferred in
if len( wdDocName ) = 0 then
    MsgBox "Add file name to 'Params' parameter in your Link-URL: Name=OpenDoc.bat|Params=myfile.dotx"
    WScript.Quit
end if
wdDocName = "\" + wdDocName rem quick fix for parameter coding error
rem open word app and document
Set wdApp = GetObject(, "Word.Application")
if wdApp is nothing then Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
rem Set wdDoc = wdApp.Documents.Open( wdDocName )
set wdDoc = wdApp.Documents.Add( wdDocName )
if IsEmpty( wdDoc ) then
    MsgBox "Unable to generate Document (" & wdDocName & ")"
    WScript.Quit
end if
On Error goto 0
rem delete all doc variables
For Each wdVar In wdDoc.Variables
    wdVar.Delete
next
rem copy environment variables to doc variables
For Each strItem In wshProcEnv
    pos = InStr( 1, strItem, "XP.", vbTextCompare )
    if pos <> 0 then
        pos = InStr( 1, strItem, "=", vbTextCompare )
        key = Left( strItem, pos )
        key = Replace( key, "=", "")
        value = Mid( strItem, pos )
        value = Replace( value, "=", "")
        wdDoc.Variables.Add key, value
    end if
Next
wdDoc.Fields.Update
rem delete unassigned doc variable fields
For Each wdField in wdDoc.Fields
    if wdField.Type = 64 then
        pos = InStr( 1, wdField.result.text, "Fehler", vbTextCompare )
        rem "Fehler" must be localized to your MS-word language
        if pos > 0 then
            wdField.Delete
        end if
    end if
next
wdDoc.Fields.Update
</script></job>

Word document

The data to be displayed are stored as field functions in the document. The fields reference document variables. The field functions are stored in Microsoft Word via the command Insert/Quick blocks/Field. The variable names follow the familiar notation (XP…). Inserting a field that references a document variable:

word1

View of the document template with activated field functions (key combination ALT-F9)

word2

When creating the document, the environment variables with the contact data are written instead of the field functions.

word3

Have you found a mistake on this page?

Or is something not formulated well or too vague? Then we look forward to receiving an e-mail, preferably with a suggestion for improvement, to doku@c4b.de. Thank you very much!