Populate field values in catalog form in service portal by clicking UI Action button

Anjaneyulu1
Giga Contributor

Hii All,

I have a requirement.

1. Create a new Category 'Service Request'.
2. Create Service Catalog with name 'Service Request for Incidents' with variables - Requestor(Type - Reference), Category (Type - Choice; with same options as in Incident), Short Description(Type - String), Description(Type - String), Currency( Type - Currency).
3. Create UI Action 'Çreate SR' which should be visible only when category is 'Service Request'.
4. On click of this button , it should redirect to catalog created in point no.2 and variables(Caller in Requestor, Category , Currency, SHort Description) should be auto- populated in catalog page with values available in Incident form.

 

-->I have done 1,2&3 points.

-->im struck at 4th point.

-->please help me in the 4th point how to sort it out.

 

Thanks,

anjaneyulu.

1 ACCEPTED SOLUTION

Ok thanks - so the reason requester is not getting populated is the name of your variable is actually 'requester', so I updated line 5 of your onLoad 'Incident details' catalog client script accordingly and it is now working:

g_form.setValue('requester', caller);

URL: https://dev89219.service-now.com/sp?id=sc_cat_item&sys_id=75732e4b1b564110dc0f40c6cc4bcb67&sysparm_caller=6fadf5f21bca0110dc0f40c6cc4bcb6b&sysparm_category=Service%20Request&sysparm_shortDescription=wert

Screenshot:

find_real_file.png

I also went back and set all the choices you created for your Category variable to have Inactive = true since they are no longer needed due to Choice table/field configuration, and this resolved the duplicate Service Request choice issue as well.

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

24 REPLIES 24

Ok so it is going to be a very similar solution to the mail script we worked on in your previous thread.

You can set up the UI action as below:

find_real_file.png

UI action script pasted below:

//replace sys_id value with the sys_id of your catalog item
var srLink = '/sp?id=sc_cat_item&sys_id=039c516237b1300054b6a3549dbe5dfc';
srLink += '&sysparm_caller=' + current.getValue('caller_id') + '&sysparm_category=' + current.getValue('category') + '&sysparm_shortDescription=' + current.getValue('short_description');
action.setRedirectURL(srLink + '');

Then you will need an onLoad catalog client script on your catalog item to parse the URL parameters if they're present and set the variable values accordingly:

function onLoad() {
    //Type appropriate comment here, and begin script below
    var caller = getParameterValue('sysparm_caller') ? getParameterValue('sysparm_caller') : false;
    if (caller) {
        g_form.setValue('requestor', caller);
    }
    var category = getParameterValue('sysparm_category') ? getParameterValue('sysparm_category') : false;
    if (category) {
        g_form.setValue('category', category);
    }
    var shortDescription = getParameterValue('sysparm_shortDescription') ? getParameterValue('sysparm_shortDescription') : false;
    if (shortDescription) {
        g_form.setValue('short_description', shortDescription);
    }
}

function getParameterValue(name) {
    name = name.replace(/[[]/, "\\[").replace(/[\]]/, "\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(this.location.href);

    return (results ? unescape(results[1]) : "");
}

There is no OOTB field on incident for Currency, so I'm not sure which field you're referring to there, but either way you can use the same methodology as I have demonstrated above to set additional variables from incident fields as necessary.

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hii Christopher Perry,

I have written everything as you written.

But in incident form i have clicked ui action button(create SR) it is not performing any kind of action and it is not redirecting to service portal.

 

Thanks,

Anjaneyulu.

Could you please copy and paste the script from your UI action?

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hi Perry,

As you written i have copied & pasted in ui action(create SR).

 

find_real_file.png

 

 

Hi Anjaneyulu,

The same code is working for me in my PDI - I'm not sure why your UI action is not doing anything:

find_real_file.png

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry