How to set field id: projecttemplate via SuiteScript 2.0
Hello,
I have a client script deployed to Case records. I’m working on adding a “Create Project” button to the Case record. The button will create a project and log the newly created project id to the existing Case record. When creating the project, I’m gathering information from the Case record (and associated Customer) to set on the Project. One of the fields I’d like to set on the new is “Project Template”, a standard NetSuite field (field id: projecttemplate). I am not able to do so, via testing the script in the UI, using the debugger, or the console. I’ve tried both record.setValue and record.setText. I do not think I can use record.submitFields, since a record id does not exist at that point in time and the Project Template can only be set upon project creation. Any assistance would be greatly appreciated!
Console code to test setting the field:
require (['N/currentRecord'], function(currentRecord){ var curRec = currentRecord.get(); console.log('curRec: ', curRec); curRec.setValue({ fieldId: 'projecttemplate', value: 128 }); var projecttemplate = curRec.getValue({ fieldId: 'projecttemplate' }); console.log('projecttemplate: ', projecttemplate); } );
Entire Client Script function to create project code:
function createProject(){ //get data from Case/Client to send to New Project record var caseRecord = currentRecord.get(); var customer = caseRecord.getValue({ fieldId: 'company' }); if (!customer){ alert('Cannot create a project since "Client" is not selected.'); return; } var caseType = caseRecord.getValue({ fieldId: 'custevent_ee_case_issue_type' }); //format new Project name. Should be Customer Name Market Abbreviation "Service Work Order" Date var name = caseRecord.getText({ fieldId: 'company' }); var market = caseRecord.getValue({ fieldId: 'custevent_ee_case_market' }); //get date and format for title d = new Date(); mon = d.getMonth() +1; day = d.getDate(); year = d.getFullYear().toString().slice(-2); var today = mon +'/'+ day +'/'+ year; var projectName = name+' '+market+' Service Work Order '+today //switch to define which template to use var template ; var options = { title: 'Project Template Unknown', message: 'Select the Correct Project Template.', buttons: [ {label: 'Upgrade', value: 127122 }, {label: 'RMA', value: 127120 }, {label: 'Sub-Contractor', value: 127124 }, {label: 'Cancel', value: 'Cancel'} ] }; var userInput = dialog.create(options); if (userInput == 'Cancel'){ alert('Project not created, due to user not selecting a Project Template when prompted.'); return; } template = userInput; //create new project var newProject = record.create({ type: record.Type.JOB, isDynamic: true, //default is false }); newProject.setValue({ fieldId: 'custentity38', //Project Type value: 2 }); newProject.setValue({ fieldId: 'customform', value: 128 }); newProject.setValue({ fieldId: 'companyname', value: projectName }); newProject.setValue({ fieldId: 'parent', value: customer }); newProject.setValue({ fieldId: 'projecttemplate', value: template }); var newProjectId = newProject.save({ enableSourcing: true, //default is false ignoreMandatoryFields: true //default is false }); //set Project field on case record with the new project id record.submitFields({ type: record.Type.SUPPORT_CASE, id: caseRecord.id, values: { custevent_ee_case_associated_project: newProjectId }, options: { //enableSourcing: false, ///default is true ignoreMandatoryFields : true //default is false } }); alert('Done creating Project.'); //refresh current page window.open('https://XXX.app.netsuite.com/app/crm/support/supportcase.nl?id='+caseRecord.id, '_self'); //display newly created project in a new tab window.open('https://XXX.app.netsuite.com/app/accounting/project/project.nl?id='+newProjectId, '_blank'); }
dialog.create is asynchronous, it returns a Promise that eventually returns a value that you can use. Your code is creating the project before the Promise is fulfilled. Take a look at the N/ui/dialog Module examples and look into how Promises work.
Battk – Thank you so much for the assistance!
For other’s reviewing/posterities sake:
“dialog.create(options).then(success);” and moving the rest of the execution to the success function worked. It created a pause in the code to wait for the user’s input. I ended up reworking the script to wait for the user’s response, send all the inputs as url parameters to a new url in a new tab. A pagInit function then set the values on the new project then alerted the user to review and save the record.