trying to Cart API for record producer but showing error

Rahul Raja Sami
Tera Guru

hi this is my script 

(function executeRule(current, previous /*null when async*/) {

       var parser = new sn_impex.GlideExcelParser();
    var attachment = new GlideSysAttachment();
    // use attachment sys id of an excel file
    var attachmentStream = attachment.getContentStream(current.sys_id);
    gs.log('request2 ---------------' + current.sys_id);
    parser.parse(attachmentStream);

    //retrieve the column headers
    var headers = parser.getColumnHeaders();
    var header1 = headers[0];
    var header2 = headers[1];
    var header3 = headers[2];
    var header4 = headers[3];
    var header5 = headers[4];
    var header6 = headers[5];
    var header7 = headers[6];
    var header8 = headers[7];
    var header9 = headers[8];
    var header10 = headers[9];
    var header11 = headers[10];
    var header12 = headers[11];
    var header13 = headers[12];
    var header14 = headers[13];

    var mt = 0;
    while (parser.next() && mt == 0) {
        var row = parser.getRow();
        if (row[header3] != '') {
     gs.log('success 1');

         var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId); //calling the cart API  
            var item = cart.addItem('ff7fbd8c1bfd7d50fe087518dc4bcbb9'); //sysid of the record producer

     var gr2 = new GlideRecord('sys_user');
     gr2.addQuery('user_name', row[header1]); 
     gr2.query();
     if (gr2.next()) {
         cart.setVariable(item, 'email_address', gr2.email);
         cart.setVariable(item, 'requested_for', gr2.sys_id);
     } else {
         cart.setVariable(item, 'requested_for', '55061d071b8209105cf40f6cdc4bcb9a');
         cart.setVariable(item, 'email_address', 'emailaddress');
     }

     cart.setVariable(item, 'subcategory', 'others');
     cart.setVariable(item, 'description', 'please find attached');

    var rc = cart.placeOrder();
                    GlideSysAttachment.copy('incident', current.table_sys_id, 'incident', rc.sys_id);
     gs.log('success 2');

        }else {
            mt = 1;
        }
    }

})(current, previous);

here I am writing BR on attachment table when the Incident is added with an particular sheet. Once the sheet is added I want to create an Incident with the record producer.
My script is not working for th record producer but working for any catalog item.

please help

3 REPLIES 3

James Chun
Kilo Patron

Hi @Rahul Raja Sami,

 

Can you please elaborate on what sort of issue you are getting? Please check the system log as well.

For an Incident, there could be mandatory fields that must be populated which your script may not be doing.

 

Thanks

Hi James

this is the error on logs

Root cause of JavaScriptException: java.lang.NullPointerException
: java.lang.NullPointerException: com.glideapp.servicecatalog.RequestNew.createVariablePool(RequestNew.java:754)
com.glideapp.servicecatalog.RequestNew.createRequestItem(RequestNew.java:605)
com.glideapp.servicecatalog.RequestNew.processOneCartItem(RequestNew.java:1025)
com.glideapp.servicecatalog.RequestNew.copyCartForRequest(RequestNew.java:335)
com.glideapp.servicecatalog.RequestNew.copyCart(RequestNew.java:289)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:566)
org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:300)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2652)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160)
com.glide.caller.gen.sys_script_include_340b86140a0a0aa700d9f0aea1fb399a_script.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:22)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159)
org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2734)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2657)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160)
com.glide.caller.gen.sys_script_078696f21b148ad05cf40f6cdc4bcbae_script.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:22)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159)
org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2734)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2657)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$exec$1(InterpretedFunction.java:176)
com.glide.caller.gen.sys_script_078696f21b148ad05cf40f6cdc4bcbae_script.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:22)
org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:389)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:199)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:131)
com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:363)
com.glide.script.BusinessRule.runScript(BusinessRule.java:468)
com.glide.script.BusinessRule.runSyncBusinessRule(BusinessRule.java:344)
com.glide.script.BusinessRule.runBusinessRule(BusinessRule.java:227)
com.glide.script.BusinessRule.run(BusinessRule.java:196)
com.glide.script.GlideRecordBusinessRules.run(GlideRecordBusinessRules.java:264)
com.glide.script.GlideRecord.runScripts(GlideRecord.java:1448)
com.glide.script.GlideRecord.runScriptsAndEngines(GlideRecord.java:1386)
com.glide.script.GlideRecord.postChange(GlideRecord.java:5370)
com.glide.script.GlideRecord.postUpdate(GlideRecord.java:5338)
com.glide.script.GlideRecord.updateWithoutResettingOperation(GlideRecord.java:4866)
com.glide.script.GlideRecord.update(GlideRecord.java:4733)
com.glide.script.GlideRecord.update(GlideRecord.java:4652)
com.glide.ui.SysAttachment.setState(SysAttachment.java:3976)
com.glide.snap.AntiVirusOnDemandAdvisor.getAvailability(AntiVirusOnDemandAdvisor.java:115)
com.glide.snap.AntiVirusOnDemandAdvisor.jsStaticFunction_getAvailabilityAfterUpload(AntiVirusOnDemandAdvisor.java:101)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:566)
org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
org.mozilla.javascript.FunctionObject.doInvoke(FunctionObject.java:677)
org.mozilla.javascript.FunctionObject.call(FunctionObject.java:614)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2649)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160)
com.glide.caller.gen.sys_trigger_e20013761b90cad05cf40f6cdc4bcbb1.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:22)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:597)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3573)
org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:172)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:389)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:199)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:131)
com.glide.script.Evaluator.evaluatePossiblePrefixedString(Evaluator.java:217)
com.glide.job.RunScriptJob.evaluateScript(RunScriptJob.java:182)
com.glide.job.RunScriptJob.evaluateScript(RunScriptJob.java:157)
com.glide.job.RunScriptJob.execute(RunScriptJob.java:88)
com.glide.schedule.JobExecutor.lambda$executeJob$0(JobExecutor.java:169)
com.glide.schedule.JobExecutor.executeJob(JobExecutor.java:172)
com.glide.schedule.JobExecutor.execute(JobExecutor.java:155)
com.glide.schedule.JobExecutor.execute(JobExecutor.java:149)
com.glide.schedule_v2.SchedulerWorkerThread.executeJob(SchedulerWorkerThread.java:449)
com.glide.schedule_v2.SchedulerWorkerThread.lambda$process$1(SchedulerWorkerThread.java:318)
com.glide.worker.TransactionalWorkerThread.executeInTransaction(TransactionalWorkerThread.java:35)
com.glide.schedule_v2.SchedulerWorkerThread.process(SchedulerWorkerThread.java:318)
com.glide.schedule_v2.SchedulerWorkerThread.run(SchedulerWorkerThread.java:118)

Hi,

 

I think your approach is a little bit "off".

var rc = cart.placeOrder();
This will generate a Request (RITM with the record producer inside) - not generating an incident.
I just tested this:

 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId); //calling the cart API 
var item = cart.addItem('3f1dd0320a0a0b99000a53f7604a2ef9'); //sysid of the record producer

cart.setVariable(item, 'comments', 'test description');

var rc = cart.placeOrder();


Skærmbillede 2024-02-16 102601.png

 

I dont understand why you want to execute a record producer that generates an incident when you can create an incident from "new GlideRecord()"
You can make an incident from GlideRecord and parse the information from your excel directly into the incident record. - Theres no need of doing it through a record producer because the record producer just generates an incident anyway.