UI Action scripts to copy the current record

CCZMAX1
Mega Sage

Hi, I have created a client side UI action which copies the current record and opens a new record in classic view.

However, I have since discovered a neater way of doing it after seeing how it is done for the workspace and wondered if anyone could give any advise as to whether my original or shorter classic view version is better and would either cause any issues.

Thanks

Max

 

Original Version (classic)

 

function OnCopyReleaseClick() 
{ 
	var existingReleaseSysId = g_form.getUniqueValue();  //get the current record's sysid

	var grExistingRelease = new GlideRecord('rm_release'); //retrieve the current record to get the field values

	if (grExistingRelease.get(existingReleaseSysId)) //if the current records sysid set variables
	{
		var short_description = grExistingRelease.getValue('short_description');
               var description = grExistingRelease.getValue('description');
		var risk = grExistingRelease.getValue('u_risk');
		var cmdb_ci = grExistingRelease.getValue('cmdb_ci');
		var deployment_package = grExistingRelease.getValue('u_deployment_package');
		var assignment_group = grExistingRelease.getValue('assignment_group');
		var release_type = grExistingRelease.getValue('release_type');
	}

    var url = '/rm_release.do?sysparm_stack=rm_release.do&sys_id=-1&sysparm_query='; //base url

    url += 'short_description=' + encodeURIComponent(short_description) + '^description=' + encodeURIComponent(description) + '^u_risk=' + encodeURIComponent(risk)	+ '^cmdb_ci=' + encodeURIComponent(cmdb_ci) + '^u_deployment_package=' + encodeURIComponent(deployment_package) + '^assignment_group=' + encodeURIComponent(assignment_group) + '^release_type=' + encodeURIComponent(release_type);

    g_navigation.open(url); //go to the record opened with the value populated
}

 

Shorter Version (classic)

function Test() 
{ 
  var url = '/rm_release.do?sysparm_stack=rm_release.do&sys_id=-1&sysparm_query='; //base url 

  url += g_form.getValue('parent') + "^short_description=" + g_form.getValue('short_description') + "^description=" + g_form.getValue('description') + "^u_risk=" + g_form.getValue('u_risk') + "^cmdb_ci=" + g_form.getValue('cmdb_ci') + "^u_deployment_package=" + g_form.getValue('u_deployment_package') + "^release_type=" + g_form.getValue('release_type');

    g_navigation.open(url); //go to the record opened with the value populated
}

 

Workspace Version

function onClick(g_form) 
{   
g_aw.openRecord("rm_release", "-1", { query: "parent=" + g_form.getValue('parent') + "^short_description=" + g_form.getValue('short_description') + "^description=" + g_form.getValue('description') + "^u_risk=" + g_form.getValue('u_risk') + "^cmdb_ci=" + g_form.getValue('cmdb_ci') + "^u_deployment_package=" + g_form.getValue('u_deployment_package') + "^release_type=" + g_form.getValue('release_type')});

}

 

6 REPLIES 6

Community Alums
Not applicable

Hi @CCZMAX1 ,

I tried your problem in my PDI and I think there is no way to do GlideRecord on current table you get all the value without using GlideRecord. You can access them like g_form.getValue('field_name'); Please refer below optimized code 

 

function OnCopyReleaseClick() {
    var short_description = g_form.getValue('short_description');
    var description = g_form.getValue('description');
    var risk = grExistingRelease.getValue('u_risk');
    var cmdb_ci = grExistingRelease.getValue('cmdb_ci');
    var deployment_package = grExistingRelease.getValue('u_deployment_package');
    var assignment_group = grExistingRelease.getValue('assignment_group');
    var release_type = grExistingRelease.getValue('release_type');

    var url = '/rm_release.do?sysparm_stack=rm_release.do&sys_id=-1&sysparm_query='; //base url

    url += 'short_description=' + encodeURIComponent(short_description) + '^description=' + encodeURIComponent(description) + '^u_risk=' + encodeURIComponent(risk)	+ '^cmdb_ci=' + encodeURIComponent(cmdb_ci) + '^u_deployment_package=' + encodeURIComponent(deployment_package) + '^assignment_group=' + encodeURIComponent(assignment_group) + '^release_type=' + encodeURIComponent(release_type);


    g_navigation.open(url); //go to the record opened with the value populated
}

 

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

Hi Sarthak, thank you for your reply.  Are you saying that the GlidRecord version 'Original Version (classic)' is not suppose to work?  This works perfectly fine for me.

However, my question is should I use the 'Shorter Version (classic)' over 'Original Version (classic)'.  Both versions work but I want to know which is better or who one version cause issues.

 

Thanks

Max

Hi Sarthak, I see what you mean.  I can skip the GlideRecord bit. However, perhaps I should go with the shorter version where I construct the url with g_form to get the values of the current record.

Community Alums
Not applicable

Hi @CCZMAX1 ,

Yes you can go with shorter version it will work fine.

 

 var url = '/rm_release.do?sysparm_stack=rm_release.do&sys_id=-1&sysparm_query='; //base url 

  url += g_form.getValue('parent') + "^short_description=" + g_form.getValue('short_description') + "^description=" + g_form.getValue('description') + "^u_risk=" + g_form.getValue('u_risk') + "^cmdb_ci=" + g_form.getValue('cmdb_ci') + "^u_deployment_package=" + g_form.getValue('u_deployment_package') + "^release_type=" + g_form.getValue('release_type');

    g_navigation.open(url)

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak