Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Record Producer script issue

Jeanne Goodman
Giga Expert

This script works great except it doesn't change the type 

(function() {
    // Set the caller to the person who submitted the request
    current.caller_id = gs.getUserID();
   
    // Set the category and subcategory
    current.category = 'Telecom';
    current.subcategory = 'Abt Phone #';
   
    // Get the request type from the record producer
    var requestType = producer.request_type; // Ensure this matches the field name in the record producer
    gs.log('Request Type: ' + requestType);
   
    // Set the type based on the user's selection
    switch (requestType) {
        case '792783c247b59a108dda98d3616d43ec':
            current.u_type = 'Disable/Remove';
            gs.log('Set u_type to Disable/Remove');
            break;
        case '3a8c3d0e47319a108dda98d3616d43ad':
            current.u_type = 'New';
            gs.log('Set u_type to New');
            break;
        case 'f93c9d8f47759e108dda98d3616d4305':
            current.u_type = 'Retain';
            gs.log('Set u_type to Retain');
            break;
        case '3a8c3d0e47319a108dda98d3616d43af':
            current.u_type = 'Reissue';
            gs.log('Set u_type to Reissue');
            break;
        default:
            current.u_type = '-None-';
    }


// Set other fields
current.u_type_of_request = 'Abt Request';
current.impact = '3 - Low';
current.urgency = '3 - Low';
current.contact_type = 'self-service';
current.assignment_group = 'c70483890058300008e976c2ab56f959';

// Set Short description
current.short_description = requestType + ' request for ' + gs.getUserDisplayName();

// Set the description to capture all options selected by the user
var description = 'Request Type: ' + requestType + '\n' +
                  'Phone Number: ' + producer.u_phone_number + '\n' +
                  'Business Justification: ' + producer.business_justification + '\n' +
                  'Type: ' + producer.request_type + ' ' + current.u_type;

   
   
// if (requestType == producer.request_type) {
//   description += 'The values are loosely equal.\n';
// } else {
//   description += 'The values are different.\n';
// }
    // Add comments if they exist
    if (producer.comments) {
        description += '\nComments: ' + producer.comments;
    }
   
    current.description = description;
})();
9 REPLIES 9

It looks like your field is of the type String. Is that correct? And it is dependent on the subcategory field?

 

A couple of things you may want to consider: 

  1. In your script above, change the line that sets it to '-None-' to changing it to '' (null or empty string). That is the internal value for that choice. 
  2. You may want to change the "Incorrect #" choice to remove the special character from the internal value. That may be okay, but I've never tried that on an internal value. I've used the slashes before, so I think your 'Disable/Remove' option is okay. The same might be true for your subcategory of "Abt Phone #".

This ended up working, but I don't completely understand why. Another failed test was
case requestType == 'retain_phone_license' so I'm really quite baffled.

if (requestType == 'retain_phone_license') {
    testerVar = 1;
} else if (requestType == 'reinstate_teams_license') {
    testerVar = 2;
} else if (requestType == 'request_a_new_teams_phone_license') {
    testerVar = 3;
} else {
    testerVar = 4;
}


    switch (testerVar) {
        case 1:
            current.u_type = 'Retain';
            gs.log('Set u_type to Retain');
            testerVar = 'Retain';
            break;
        case 2:
            current.u_type = 'Reissue';
            gs.log('Set u_type to Reissue');
            testerVar = 'Reissue';
            break;
        case 3:
            current.u_type = 'New';
            gs.log('Set u_type to New');
            testerVar = 'New';
            break;
        case 4:
            current.u_type = 'Disable/Remove';
            gs.log('Set u_type to Disable/Remove');
            testerVar = 'Disable/Remove';
            break;
        default:
            current.u_type = '-None-';
  //          gs.log('Unknown request type: ' + producer.request_type +' ' +u_type );
            testerVar = 'bedbfdca47319a108dda98d3616d4370';
    }

 

What kind of variable is your producer.request_type and what are the available values for it? If it's a Select Box and the values are what you have in your if statement, then that part should work. 

 

Resetting the testerVar variable in the middle of a switch statement could potentially mess things up. 

 

I'm surprised the default choice is working since you are setting it to something that isn't a valid internal value.

It's radio button choices. I did take your advice earlier and changed the default to "", but didn't paste that version.

The code above did eventually work. But I'm not sure I understand why it worked. I can tell you that I couldn't get the cases to work. I don't know if you noticed, but I also had to make the operator a loose equivalent in order for it to work.

I had even tried switch(requestType)
case requestType == 'retain_phone_license' etc. but that didn't work either.

Jeanne

Jeanne Goodman
Giga Expert

Yes, It's dependent on the subcategory.
I've tried several combinations for the "none-of-the-above" option. They all seem to work, but I like your idea of the subcategory being the issue. I'm going to give that a shot and see if that changes anything. I'll be back in touch.