Can Built javascript file as pcg package and add to profile for execute after installed profile

R
rathasatekun
Gosoft (Thailand) Co.,Ltd.

HI SOTI Community

I have use case that 

 i want to send script (javascript) to all device as profile for end user input text to file.

so, i do this

1. built pcg file with javascript file (file.js) 

2. add pcg file to profile

3. assign to clients

but script not working how to do or have another solution.

this script

#!/usr/bin/env js
 
 
inputStore();
function inputStore() {
    mobicontrol.message.createTextPromptDialog('รหัสร้าน 5 หลักของคุณคือ?')
        .withCallback(onConfirm).show();
}
function onConfirm(result) {
    var storeId = result.inputText;
    if (isValidStoreId(storeId)) {
        var stl = storeId.length;
        var mcsetupFile = new mobicontrol.io.File('/sdcard/Documents/mcsetup.ini');
        if (mcsetupFile.exists) {
            var data = mcsetupFile.readText();
            var keyText = "StoreID=";
            var ktl = keyText.length;
            var idx = data.indexOf(keyText);
            var oldStoreId = data.substring((idx + ktl), idx + ktl + stl);
            var offset = stl;
            if (!isValidStoreId(oldStoreId)) {
                offset = 0;
            }
            var result = data.substring(0, idx + ktl) + storeId + data.substring(idx + ktl + offset);
            mcsetupFile.writeText(result);
        }
    } else {
      mobicontrol.message.createInfoDialog('Invalid store:'+storeId).show(); 
  inputStore(); //repeat input
      //mobicontrol.log.info('Invalid store:' + storeId);
    }
}
function isValidStoreId(storeId) {
return ((typeof storeId === 'string' && !isNaN(storeId)) && storeId.length == 5)};
 

My server version Version: 15.3.3.1065

a year ago
SOTI MobiControl
ANSWERS
MD
Matt Dermody Diamond Contributor
a year ago

Are you just distributing the script as a file in the package? If so the script is just going to sit there and do nothing. You'd probably want to insert the script as a post-install script instead of delivering it as a Package File. Otherwise what are you doing to actually trigger the script to run? Are you linking to it from the lockdown?

R
rathasatekun
a year ago

Thx Matt Dermody

SO, i shoud to send file javascript into device and then execute that script ,

How to trigger that script with command or profile?

Add or Drag and drop the file to the package and then double click it in the list.

Then you see a checkbox "Automatically Execute", use this to execute it after it has been copied to the device.

R
rathasatekun
a year ago

thx but i try to this solution but not working , javascript in file not trigger 

javascript file can sent to end device client

but script not working . 

MD
Matt Dermody Diamond Contributor
a year ago

What happens when you send the javascript script directly to the device from the console? Is the javascript script executing when sent directly? If it doesn't work when sent directly then maybe your device agents are too old to support JS based scripts. 

ZC
Zafer Cigdem
a year ago

Hi,

I think, you are taking an action based on the user's feedback (such as clicking on "Yes/Confirm" or "No"), right?

If yes, I did something similar earlier for OS upgrade. But my method was little bit different, I send out this survey to the users, and based on their choise I create 2 different custom data (User Feedback (Yes or No) and  Feedback Date time) and I assign profiles (if User Feedback is yes) such as Profile -> Task scheduler; and here I can use any JS script periodically.

If the survey answer is no, and you would need to ask this question again to the user, you may create another profile to assign if the feedback is "No" case, to re-triggering the survey to send out to the user periodically or such.

I hope this helps, if you may have any questions please let me know.

Zafer

R
rathasatekun
a year ago

Yes , Like this case , can you shared dessign?

ZC
Zafer Cigdem
a year ago

Sure, here is the JS script, there would be space alignment issue here because of the copy-paste, you may do double check and let me know if you need any help:

var now = new Date()

var survery_feedback_ini_file = new mobicontrol.io.File('/sdcard/Download/survey.ini');
var dialog_box_elements = ['Yes, install', 'Postpone to later'];
mobicontrol.message.createInfoDialog('Would you like to install new OS patch on your device?')
    .withButtons(dialog_box_elements[0], dialog_box_elements[1]).withCallback(onConfirm).show();

function onConfirm(result) {
    if (result.buttonIndex != null) {
        switch (dialog_box_elements[result.buttonIndex]) {
            case 'Yes, install':
				var user_feedback = "New OS upgrade is confirmed by user"
				var survey_ini_textContent= "[User Feedback]\nFeedback=" +user_feedback + "\nFeedback Date time=" + now;
                mobicontrol.message.createInfoDialog("Thanks for confirming to install new OS on your device, Current Date/Time:" +now ).show();
				survery_feedback_ini_file.writeText(survey_ini_textContent);

                break;
            case 'Postpone to later':
				var user_feedback = "New OS upgrade is postponed"
				var survey_ini_textContent= "[User Feedback]\nFeedback=" +user_feedback + "\nFeedback Date time=" + now;
                mobicontrol.message.createInfoDialog("You just postponed to new OS upgrade on your device, Current Date/Time:" +now ).show();
				survery_feedback_ini_file.writeText(survey_ini_textContent);
                break;
        }
    }
}

I'll post an article related to OS upgrade (but it's similar scenario of yours) this soon, I've already submitted and waiting for confirmation, as soon as it's publicly available I may share here in case you may need to see the steps with Screenshots. 

Zafer

R
rathasatekun
a year ago

thx for shared i will test with my case 

P
PMMOD@SOTI
a year ago

Hi Rathasatekun,

 

Thank you for your post on SOTI Pulse, do you still require any assistance with the above issue?

 

Hopefully, you were able to solve the post by using the above-suggested solutions.  If yes, please mark it as "solution".

 

Please let us know so we can proceed further accordingly.

EG
Edgar Gomez
8 months ago

Hi,

I came across this issue and finally solve it.

To include a Javascript in the package, you should add it as pre-install/post-install script, but delete all these initial comments, so that the shebang line #!/usr/bin/env js is the first line in the script: