Hello! I am beggining with SuiteScript and I have ...
# suitescript
s
Hello! I am beggining with SuiteScript and I have to develop a script in order to check if the value of a field is unique. If the value already exist in an other record I want a alert message to pop up. Here is the script I made but when I am testing it, nothing is triggered. Could you help me with it ?
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord', 'N/log', 'N/record', 'N/search'],
/**
 * @param{currentRecord} currentRecord
 * @param{log} log
 * @param{record} record
 * @param{search} search
 */
function(currentRecord, log, record, search) {
    

    /**
     * Function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @since 2015.2
     */
    function fieldChanged(scriptContext) {

        var currentRec = scriptContext.currentRecord;


        if (scriptContext.fieldId === 'otherrefnum') {

            try {
                var po = currentRec.getValue('otherrefnum');
                var customer = currentRec.getValue('entity');

                po = po.trim();

                log.error('saveRecord' , JSON.stringify(scriptContext));

                alert('un message important');

                log.error('currentRecord' + JSON.stringify(currentRec));

                var exist = searchPO(customer, po);

                if (exist) {
                    alert('The po "' + currentRec.getText('otherrefnum') + '" already exists for the sales order.');
                } else {
                    return true;
                }

            }
            catch(e) {
                console.log(e);
                log.debug({
                    title: '[Error while writing PO]',
                    details: 'Error while writing PO' + (e)
                });
            }
        }
    }
    function searchPO (entity, po){
        var exist = false;

        var searchPo = search.create({
            type: search.Type.SALES_ORDER,
            filters: [["entity","is",entity],  "AND", ["otherrefnum", "is" ,po]]
        });

        var searchResultCount = searchPo.runPaged(currentRecord).count;

        if (searchResultCount > 0) {
            exist = true;
        }

        return exist;


    }
  return {

        fieldChanged: fieldChanged,
        //validateField: validateField,
        //saveRecord: saveRecord
    };
    
});
e
Taking a quick look you are missing this at the top of the file:
/**
 
* @NApiVersion 2.x
 
* @NScriptType ClientScript
 
* @NModuleScope SameAccount
 
*/
s
yes sorry I did not write it but it is in the original file
I will add it
e
You also need to deploy your script on the record you expect to be triggered.
s
yes I deployed the script on the record but it is not triggered when I test it
but I didn't use a bundle to deploy it, is that could be a problem ?
e
No, you can do it manually. So if you have created your script and then deployed to the right record, put a log above of your first if statement just to see if this is displayed on the developer tools of your browser, if not I am guessing you have deployed it to the wrong record. Make sure your script and deployment are not inactivated as well.
s
ok thank you I will test to add a log and verify that all is activated
I had log and test it on my record. I had no trace of execution log. So I maybe I have deployed the script the wrong way. Is it possible I had to use a plug in instead of a scripted record ?
e
No, you do not have to. Just to be clear, when you add your log above the if statement, then you are changing any field value (dropdown, text, date) right? That will trigger the fieldChange event and you will get your log, if you are using log.debug that will be in the execution log tab , if you are using console.log (this is allowed since you are creating a client script) you will that in your developer tools of your browser. What record have you deployed and tested it?
s
yes I change some value in the field and I use the log.debug. I went to the execution log but it is empty. I verify and the script is well deployed but unfunctional. I am deployed it on sales order. I don't know why it won't work.
e
Hmm, it is a little hard to tell. Review this and try to see if you have missed something: https://stoic.software/effective-suitescript/1-your-first-script-for-non-devs/
👀 1
b
if you dont see your code running, usually its something wrong with the script deployment record
so thats what you should be sharing here
s
So I verifiy and I use the Netsuite scripted record module on Chrome. I see that the script is well available. So it seems that it is deployed. So I wondered what is wrong with my script so that it won't execute ? Is there any ohter check I can do ?
Hi ! I'm reposting this message since I don't resolve it and don't know how. I think something is wrong with my code but I don't know what. Is that because I am searching on saved records in the database and that I should not use N/Search but N/http ?
n
You are searching for a match using otherrefnum right? The otherrefnum according to the records browser, on a PO is Vendor #. On an SO otherrefnum is PO #. How are you going to find a matching SO that has a PO# when you're trying to match it to a vendor number? You probably need to search for the otherrefnum on the So that matches the tranid on the PO. Take a look at the values you're using
s
Ok so if I'm on the Sales Order and trying to check the value of the PO# I am probably using the wrong id name, that's it ?
I will have to look at the vendor PO to have the right id PO's name ?
n
👀 1
s
Ok so the ID of PO# in PO is tranid and in SO is otherrefnum. I want to check if when an user enter a new SO and field up the PO# of the SO, a SO is not already existing by checking the PO#. What id I have to use to do that ? the SO's id ? PO's id ?
c
@NElliott I think you're probably confusing things. The PO# here will be the purchase order number that is raised on the customers system, and they give it to the sales person as their reference to use on the Sales Order - POs in NetSuite don't have anything to do with it
s
how chek the unicity of PO# in SO ? is using the PO#'s id 'otherrefnum' is ok for that ?
c
Is what I said the actual requirement? You're wanting to check for uniqueness in that situation?
s
I want to check the unicity of the field value PO # in sales order. It can arrive that users create a new sales order on a customer with the same PO# and they don't want that to happen
so I want to add a control on this field to check if the PO# is not already existing on the customer and ensure its unicity
n
@CD could well, be I saw the issue and was headed in to a meeting, quick check to see if like for like was being checked and it looked off 🤷‍♂️ Figured it was worth checking at least in the absence of any other guidance 😉
b
if you arent sure if your script is running, you should make your fieldChanged entryPoint always log something to the console
if it doesnt log something to the console when a field value changes, then your script deployment is wrong
if you dont know how a script deployment is supposed to look, then you need to share how you set it up here
i personally recommend screenshots
s
Hello, thank you @battk for replying to me ! Here is some screenshots of my script deployment. As you suggest I log try to log something with the pageinit but nothing appear. Do you see something wrong in the parameters on this screenshots ?
b
there is a difference between a script and a script deployment
s
ok what I am not doing right ?
I am in the script deployment module
b
find the script deployment, start looking for problems there
common problems are wrong script deployment, context filtering, or event type
s
here is the script deployment I think
b
👀 1
which have you completed