Hello there ! I have a script that displays button...
# suitescript
l
Hello there ! I have a script that displays buttons to print pdf files. I want to disable this button when a fiel d is ticked true on the parent record. Can I had something like:  if (record.createdfrom.custbody_checktrue = true) {}
Copy code
form.addButton({
                id: 'custpage_add_printbtnpack',
                label: 'Print Delivery Note',
                functionName: 'printDeliveryNote('+scriptContext.newRecord.id+')'
            });
e
Not exactly. You’ll need to use a fieldLookup to get that field from the createdfrom record… What record type do your buttons display on?
l
Thank you for your time ! It displays on the item fulfillment.
and I want to browse the Sales Order it was created from.
e
Copy code
var sales_order_id = record.getValue({
    fieldId: 'createdfrom'
});

var fieldLookUp = search.lookupFields({
type: search.Type.SALES_ORDER,
id: sales_order_id,
columns: 'custbody_checktrue' // whatever the field ID is
});
Assuming you’ve defined “record” as your item fulfillment record
l
Thank you again ! As I only have sys admin background, is it regular javascript or netsuite specific ?
e
I would log out fieldLookUp after this chunk of code… I beleive it would return true or false, but I’m not 100%
SuiteScript is a library built on JavaScript. So you can use “regular” javascript, but there are certain functions that NetSuite offers that are pre-built
l
It’s really nice from you to have taken the time to help. I will do my research to adapt the code that you provided to me.
Lastly, what do you mean by "log out fieldLookUp" ? giving a null value to it ?
e
Log out means to use the log.debug() function. This just allows you to look at the script log and see what value a variable returns ---- var test = 2 + 2; log.debug({ title: ‘What is TEST equal to?’, details: test }); If you have this in your code, when it runs - a script log would be created and it would show “What is TEST equal to?” and then show 4
c
@LRG if you're curious to learn a little more about suitescript, Matt's book is worth going through https://leanpub.com/u/mattdahse Not super helpful to your current thing that eric is helping with but may answer some questions about coding.
l
Ok I understand that ! Thank you
e
+1 for @creece’s suggestion of Matts book
l
Thank you for the suggestion, I’ll see if i find time to read it 😄
@ericbirdsall do I need to declare anything else than the following to have getValue working ? 
Copy code
define(['N/record', 'N/log','N/runtime','N/currentRecord'],
function(record, log, runtime,currentRecord) { ...
I have an error each time I try to use the function:
c
technically you don't need N/record as its loaded automagically most time. The issue is whatever you are calling .getValue on is probably null or not the right object type (most likely its null)
would have to see more code
l
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/log','N/runtime','N/currentRecord'],

        function(record, log, runtime,currentRecord) {
		     
    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {string} scriptContext.type - Trigger type
     * @param {Form} scriptContext.form - Current form
     * @Since 2015.2
     */
    function beforeLoad(scriptContext) {
        if (scriptContext.type == 'view') {
            var form = scriptContext.form ;
            log.audit('ItemFulFillmentId' ,scriptContext.newRecord.id);
            var scriptObj = runtime.getCurrentScript();
            log.debug("Script parameter: " +  scriptObj.getParameter({name: 'custscript_ax_clientscriptcertificateid'}));
            form.clientScriptFileId = scriptObj.getParameter({name: 'custscript_ax_clientscriptcertificateid'});
            form.addButton({
                id: 'custpage_add_printbtn',
                label: 'Print Certificate Of Conformity',
                functionName: 'printCertificate('+scriptContext.newRecord.id+')'
            });
            var salesOrderId = record.getValue({
    		fieldId: "createdfrom"
			});
/*			var fieldLookUp = search.lookupFields({
			type: search.Type.SALES_ORDER,
			id: sales_order_id,
			columns: 'custbody_checktrue' // whatever the field ID is
			});
*/
          form.addButton({
                id: 'custpage_add_printbtnpack',
                label: 'Print Delivery Note',
                functionName: 'printDeliveryNote('+scriptContext.newRecord.id+')'
            });
        }else{
            log.audit({title : "ContextType",details : "ContextType:" + scriptContext.type });
        }

    }

    return {
        beforeLoad: beforeLoad
    };

});
But, the related fieldId can’t be null, since it’s the parent record.
c
replace
Copy code
var salesOrderId = record.getValue({
    		fieldId: "createdfrom"
			});
with scriptContext.newRecord.getValue({fieldId: 'createdfrom'});
e
Like this:
Copy code
var salesOrderId = scriptContext.newRecord.getValue({
    		fieldId: "createdfrom"
			});
c
You're using record.getValue. "Record" is just a handle to the N/record scripting module so there's nothing there in terms of data. In order to get the value, you need an actual instance of the record which is what scriptContext.newRecord is (there's also a .oldRecord in some cases).
l
Ok I understand.
Thank you again !
30 minutes and a new question : here, fieldLookUp returns {custbody_checktrue=true} instead of the boolean true. Is that normal ? Do I need to convert it ? (I debugged by displaying the content of fieldLookUp into the button variable)
Copy code
var salesOrderId = scriptContext.newRecord.getValue({
    		fieldId: "createdfrom"
			});
			var fieldLookUp = search.lookupFields({
			type: search.Type.SALES_ORDER,
			id: salesOrderId,
			columns: 'custbody_bxt_downpayment_val' // whatever the field ID is
			});
           		form.addButton({
                id: 'custpage_add_printbtnpack',
                label: fieldLookUp,
                functionName: 'printDeliveryNote('+scriptContext.newRecord.id+')'
            	});
c
Either way you do a lookup, it returns a result w/ the field ID and value. You'll need to get the value using fieldLookUp.custbody_bxt_downpayment_val if its a list value, you'll need to access the 1st index of fieldLookUp.custbody_bxt_downpayment_val
🙏 1
l
You rock ! Now I’m ready to Take over the world 😄 many thanks to both of you, and sorry for being this much of a noob in suitescripts 😄