Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Record Producers creating duplicate records when using applyTemplate

jbutz
Kilo Expert

When I use a record producer and use the applyTemplate function on the current GlideRecord object I end up with two records with the same task number. The Record Producer has a template field, but the template I am using varies depending upon the options chosen during the wizard.

 

Does anyone know of a work around? I'm having troubles coming up with one.

 

Update:

 

I believe the problem is that the Record Producer and GlideRecord.applyTemplate does an Insert. So when I apply the template the current record gets inserted, and then inserted again when the record producer finishes running the code. If there is a way to specify at runtime what template the record producer should use this problem could be easily solved.

 

Message was edited by: Jason Butz

1 ACCEPTED SOLUTION

jbutz
Kilo Expert

I appears as though my assumption that Record Producers run a current.insert() after running the script may be correct. I looked through the Record Producers in the sandbox instance of ServiceNow at https://sandbox.service-now.com and found that the New LDAP Server Record Producer manually inserted the record and then called current.setAbortAction(true). That gave me the idea to run a current.update() and then abort the action. This ended up working, I no longer get duplicate records.


View solution in original post

7 REPLIES 7

Mahira
Tera Guru

Hi Jason,



Could you share the code you are using in the record producer?



Regards,


Mahira


Here is the code:



(function(){
          var uri = ('change_request.do?sysparm_query=sys_id=' + current.sys_id);

          wizard.redirect = uri;
          gs.addInfoMessage("Please review the Change Request below and click Request Approval");

          //set the change class
          var chgClass = getChangeClass();
          current.type = chgClass;


          current.short_description = wizard.short_description;
          current.u_reason_for_change = wizard.change_reason;
          current.u_potential_impact = wizard.potential_impact;
          current.u_likelihood_of_impact = wizard.likelihood_of_impact;
          current.start_date = wizard.change_start;
          current.end_date = wizard.change_end;
          current.u_peer_approval_group = wizard.peer_approval_group;
          if(wizard.peer_approval_group == "") current.u_peer_approval_group = current.assignment_group;
          current.requested_by = wizard.requested_by;

          current.parent = wizard.parent;

          if (wizard.external_ticket_system != '')
          {
                    current.u_external_ticket_system = 1;
                    current.u_external_ticket_number = wizard.external_ticket_system;
          }
          else
          {
                    current.u_external_ticket_system = 0;
          }

          if(wizard.customer_impact == 'yes')
          {
                    var wza = wizard.customer_approval;
                    if (wza == 'not_required') {
                              current.u_customer_approval = 9;
                    }
                    else if (wza == 'blast') {
                              current.u_customer_approval = 5;
                              current.u_customer_approval_by = "Blast Notification"
                    }
                    else if (wza == 'approved') {
                              current.u_customer_approval = 5;
                              current.u_customer_approval_by = wizard.customer_approval_by;
                    }
                    else if (wza == 'not_yet') {
                              current.u_customer_approval = 1;
                    }
          }
          else
          {
                    current.u_customer_approval = 9;
          }



          if(chgClass == 2) current.u_emg_manager = wizard.emg_manager; // Emergency Change
          if(chgClass == 6) current.applyTemplate(wizard.routine_change_template.name);
          if(chgClass != 6)
          {
                    current.assignment_group = wizard.assignment_group;
                    current.assigned_to = wizard.assigned_to;
          }



          function getChangeClass() {
                    //Change types
                    //1 = normal
                    //2 = emergency
                    //3 = MAC
                    //4 = Informational

                    var selectedClass = wizard.change_class;
                    //normal
                    //mac_application
                    //emergency
                    var selectedOrg = wizard.change_organization;
                    //current_user
                    //internal_cass
                    //internal_pso
                    //external_org


                    if (selectedOrg == 'external_org') {
                              return 4;
                    }
                    else if (selectedClass == 'normal') {
                              return 1;
                    }
                    else if (selectedClass == 'emergency') {
                              return 2;
                    }
                    else if (selectedClass == 'mac_application') {
                              return 3;
                    }
                    else if(selectedClass == 'informational') {
                              return 4;
                    }
                    else if(selectedClass == 'routine') {
                              return 6;
                    }
                    else if(selectedClass == 'comprehensive') {
                              return 5;
                    }
          }
}())

Hi Jason,



Can you try if the code creates only one   record if you hard code the template name in the script?



Regards,


Mahira


I hard coded a template name and it still created duplicate records.