Alessandro Aguiar
Alessandro Aguiar

Reputation: 61

SuiteScript 2.0: How can I update the externalID through Script

I'm trying to create a script file on NetSuite using SuiteScript 2.0 to update the External Id of the classification records. For some reason, NetSuite updates all fields but externalId.

I want to create this script to update the externalIds because mostly of our CSV templates uses the externalIds to look for master data such as accounts and classes. The point is, NetSuite doesn't show the externalIds on the forms and all the records created via UI, has no information in this field. So the idea here is to schedule a script to fill this field automatically based on some other fields.

Here is my code

/**
 * @NApiVersion 2.0
 * @NScriptType ScheduledScript
 */
define(['N/runtime','N/log','N/record','N/search'], function(runtime,log,record,search) {
    
    var qtyProjects=0;
    
    function execute(context) {
        try
        {
            log.debug('Script Started');
            
            /********** Update Project (Classes) External ID ***********/
            var classificationSearchObj = search.create({
                   type: "classification",
                   filters:
                   [    
                        ["externalid","is","@NONE@"]
                        
                   ],
                   columns:
                    [
                        search.createColumn({name: "name",label: "Name"}),
                        search.createColumn({name: "custrecord_proj_full_name",label: "FullName"}),
                        search.createColumn({name: "custrecord_proj_manager",label: "ProjectManager"}),
                        search.createColumn({name: "externalid",label: "Externalid"})
                    ]
                });
                                        
                var prj_srch = classificationSearchObj.run().each(processProject);
                    
                log.debug('Quantity of projects: ',qtyProjects);
                
                    
                
                log.debug('Script Finished');
        }
        catch(e)
        {
            log.debug('Error',e.message);
        }           
    }
    
     function processProject(result) {
        var number = result.getValue({name: "name"});
        var fullName = result.getValue({name: "custrecord_proj_full_name"});
        var externalid = result.getValue({name: "externalid"});
        
        qtyProjects++;
        
        log.debug('Update Number|Name|ExternalId: ',number + " | " + fullName + " | " + externalid);
     
     record.submitFields({
            "type":'classification',
            "id": result.id,
            "values": {
                "externalId": externalid,
                "custrecord_proj_full_name": "Test Ale 2",
            }
        });
        
        /*
        var project_id = project.save({
                                        enableSourcing: true,
                                        ignoreMandatoryFields: true
                                        }); 
        */
        
        //return true;
     }
    
    
    return {
        execute: execute
    };
});

Upvotes: 3

Views: 2329

Answers (6)

Charlie Schliesser
Charlie Schliesser

Reputation: 8246

I've found that SuiteScript 2 syntax does not work for updating externalId. For me, this simply doesn't do anything:

instance.setValue({ fieldId: 'externalid', value: 'FOO1' })

While using the older syntax actually will update externalId:

instance.setValue('externalid', 'FOO1')

You can run this in your browser's console while viewing a record to try it out:

require(['N/record'], function (record) {
  const instance = record.load({ type: 'customerpayment', id: 5000 })
  instance.setValue('externalid', 'PYMT1234')
  instance.save()
})

Upvotes: 0

Addy
Addy

Reputation: 77

    /**
    * @NApiVersion 2.0
    * @NScriptType ScheduledScript
    */
    define(['N/runtime','N/log','N/record','N/search'], function(runtime,log,record,search) {

    function execute(context) {
    var so = record.load({
                    type: record.Type.SALES_ORDER,
                    id: 12345
                });
      so.setValue('external','6789');
    so.save();
    return true;
    }       

    return {
    execute: execute
    }
    });

Upvotes: 1

bknights
bknights

Reputation: 15447

Your code is searching for classes with no externalid and is updating the class's externalid with the empty value found in the search. It appears you are missing a lookup to get what the new externalid should be.

Upvotes: 0

Alex Shaw
Alex Shaw

Reputation: 1

Yes i tried loading the record and set external id , it worked for me . Please use record browser for internal id's of fields (externalid).

Upvotes: 0

bluehank
bluehank

Reputation: 158

You mispelled the id of ExtermalId, it is without Capital 'I' as externalid. This should work. As a rule of thumb native field id's don't have capital letters in it

Upvotes: 1

Rusty Shackles
Rusty Shackles

Reputation: 2840

Have you tried loadig the record, setting the external id then saving it?

Upvotes: 0

Related Questions