在运行时生成GUID而不使用NetSuite中的表单

时间:2017-08-22 11:16:44

标签: netsuite suitescript

在那里,

我试图在NetSuite中准备一个预定的脚本,它将从文件柜中挑选一个特定的目录并将其部署在SFTP服务器上。我使用2.0模块,这是我的代码 -

require(["N/sftp",'N/record','N/file'], function(sftp,record,file) {

    function onRequest() {
        var myPwdGuid = "13139ac567b14f74bdaXXXXXXX";
        var myHostKey = "AAAAB3NzaC1ycXXXXX";

        var connection = sftp.createConnection({
            username: 'Your user name',
            passwordGuid: myPwdGuid,
            url: 'Your host name',
            directory: '/directory to upload files/',
            hostKey: myHostKey
        });

        var myFileToUpload = file.create({
            name: 'originalname.js',
            fileType: file.fileType.PLAINTEXT,
            contents: 'I am a test file. Hear me roar.'
        });

        connection.upload({
            directory: 'relative/path/to/remote/dir',
            filename: 'newFileNameOnServer.js',
            file: myFileToUpload,
            replaceExisting: true
        });

        var downloadedFile = connection.download({
            directory: 'relative/path/to/file',
            filename: 'downloadMe.js'
        });
    }

    onRequest();
    return {
        onRequest: onRequest
    };
});

现在的问题是,当我尝试运行这些代码行时,我收到一条错误,上面写着" AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID"。 到目前为止,通过我的研究我发现的GUID只能由具有凭证字段的SuitLet表单生成,该凭证字段将再次需要GET和POST方法。但是我不想创建一个suitelet并手动调用它以生成GUID。我想要做的就是 - 运行一个预定的脚本,它将建立与SFTP的连接。选择文件柜中的目录并将其上传到SFTP。

任何帮助将不胜感激!提前谢谢!

2 个答案:

答案 0 :(得分:0)

它比你想象的更容易,更快。获取以下代码并将其加载到NetSuite。快速创建脚本文件和部署,运行SUITElet以获取GUID,将该值粘贴到您的预定脚本中,除非密码更改,否则不要再次使用它。

/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define([
'N/ui/serverWidget',
'N/https'
],
function (
    ui,
    https
) {
    var HOST_KEY_TOOL_URL = 'https://ursuscode.com/tools/sshkeyscan.php?url=';

    function getFormTemplate() {
        var form;
        form = ui.createForm({
            title: 'Password Form'
        });
        form.addSubmitButton({
            label: 'Submit'
        });

        return form;
    }

    function addSelectorFields(form) {
        var select = form.addField({
            id: 'selectaction',
            type: ui.FieldType.SELECT,
            label: 'Select Action'
        });
        select.addSelectOption({
            value: 'getpasswordguid',
            text: 'Get Password GUID'
        });
        select.addSelectOption({
            value: 'gethostkey',
            text: 'Get Host Key'
        });
        return form;
    }

    function addPasswordGUID1Fields(form) {
        var frm = form;

        frm.addField({
            id: 'restricttoscriptids',
            type: ui.FieldType.TEXT,
            label: 'Restrict To Script Ids'
        }).isMandatory = true;
        frm.addField({
            id: 'restricttodomains',
            type: ui.FieldType.TEXT,
            label: 'Restrict To Domains'
        }).isMandatory = true;

        return frm;
    }

    function addPasswordGUID2Fields(form, restrictToScriptIds, restrictToDomains) {
        form.addCredentialField({
            id: 'password',
            label: 'Password',
            restrictToScriptIds: restrictToScriptIds.replace(' ', '').split(','),
            restrictToDomains: restrictToDomains.replace(' ', '').split(',')
        });
        return form;
    }

    function addHostKeyFields(form) {
        form.addField({
            id: 'url',
            type: ui.FieldType.TEXT,
            label: 'URL (Required)'
        });

        form.addField({
            id: 'port',
            type: ui.FieldType.INTEGER,
            label: 'Port (Optional)'
        });

        form.addField({
            id: 'hostkeytype',
            type: ui.FieldType.TEXT,
            label: 'Type (Optional)'
        });
        return form;
    }

    function onRequest(option) {
        var method;
        var form;
        var selectAction;
        var port;
        var hostKeyType;
        var restricttoscriptids;
        var restricttodomains;
        var password;

        var theResponse;
        var myUrl;
        var url;
        method = option.request.method;
        form = getFormTemplate(method);
        if (method === 'GET') {
            form = addSelectorFields(form);
        }
        if (method === 'POST') {
            selectAction = option.request.parameters.selectaction;
            if (selectAction === 'getpasswordguid') {
                form = addPasswordGUID1Fields(form);

            } else if (selectAction === 'gethostkey') {
                form = addHostKeyFields(form);
            } else {
                password = option.request.parameters.password;
                url = option.request.parameters.url;
                port = option.request.parameters.port;
                hostKeyType = option.request.parameters.hostkeytype;
                restricttoscriptids = option.request.parameters.restricttoscriptids;
                restricttodomains = option.request.parameters.restricttodomains;

                if (restricttoscriptids && restricttodomains) {
                    form = addPasswordGUID2Fields(form, restricttoscriptids, restricttodomains);
                }

                if (password) {
                    form.addField({
                        id: 'passwordguidresponse',
                        type: ui.FieldType.LONGTEXT,
                        label: 'PasswordGUID Response',
                        displayType: ui.FieldDisplayType.INLINE
                    }).defaultValue = password;
                }
                if (url) {
                    myUrl = HOST_KEY_TOOL_URL + url + '&port=' + port + '&type=' + hostKeyType;
                    theResponse = https.get({ url: myUrl }).body;
                    form.addField({
                        id: 'hostkeyresponse',
                        type: ui.FieldType.LONGTEXT,
                        label: 'Host Key Response',
                        displayType: ui.FieldDisplayType.INLINE
                    }).defaultValue = theResponse;
                }
            }
        }
        option.response.writePage(form);
    }
    return {
        onRequest: onRequest
    };
});

答案 1 :(得分:0)

NetSuite不支持直接硬编码SFTP密码的功能。 NetSuite使用密码标记化来防止脚本访问用户凭据。因此,只有经过身份验证的用户可以存储密码,并且脚本只能通过标识符(GUID /令牌)访问它。