Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Create a new record on a different table, pre-populate reference field?

Robert_Cartwrig
Tera Expert

Hi all,

I'm kinda stuck on something and am looking for a little advice.   I'm getting that I might be stuck needing to run both server and client script in the same UI Action and that I may not be taking the proper steps to make this work.

The Use Case: A Tier 1 Agent is looking at a Consumer Record and needs to Create a New Case from a UI Action.   The new Case needs to be populated with a number of pieces of data from the Consumer record.   How do I make this happen?

The Issue:

  • The OOB (with CSM)   "Create a New Case" UI action will create a new Case and populate the Consumer field - but using this, the normal option I use for passing data on UI Actions ("ccc.newfield = current.oldfield", and "setValue("fieldX", '42')") don't work when added to the OOB script.
  • If I use custom code I've used in other situations to create a record and pass/set values, I can't seem to populate the Consumer and Company fields on the Case - but maybe I'm just passing the wrong data (email and a sys_is, respectively). I know I can type in an email address to the Consumer field and it resolves to a Consumer but this does not seem to happen from a UI Action, but passing Name doesn't work either - so I don't know what else to pass.   Same issue with Company, only I'm passing a sys_ID.

The Examples:

Here is an example of the OOB code that Creates a new Case and pre-populates Consumer:

var query = '/sn_customerservice_case.do?sys_id=-1&sysparm_view=case&sysparm_query=';

var subQuery = '';

if(!gs.nil(current.getValue("sys_id"))){

      if(query.length > 0)

              subQuery+='^consumer='+current.getValue("sys_id");

      else

              subQuery+='consumer='+current.getValue("sys_id");

}

action.setRedirectURL(query+subQuery);  

Here is an example of the code I would typically use in this situation:

createCase();

function createCase(){

var cs = new GlideRecord("sn_customerservice_case");

      cs.setValue("priority", '4');

      cs.setValue("u_corp_or_product", '2');

      cs.setValue("assignment_group",'f67899164fe66600ccee42fd0210c78e');

      cs.setValue("contact_type",'phone');

      cs.setValue("state",'10');

      cs.u_participant_id = current.u_participant_id;

      cs.u_incontact_id = current.u_incontact_id;

      cs.assigned_to = gs.getUserID();

      cs.consumer = current.email; //DOES NOT WORK

      cs.company = current.u_company_affiliation; //DOES NOT WORK

var sysID = cs.insert();

current.sn_customerservice_case_id = sysID;

var mySysID = current.update();

action.setRedirectURL(cs);

action.setReturnURL(current);

}

Lines 13 and 14 above are what I cannot seem to make happen.   I've verified both are passing data by passing to a string field.

Any advice or suggestions here would be greatly appreciated.

Thank you,

Robert

1 ACCEPTED SOLUTION

sb1186
Kilo Guru

Hi Robert,



I tried to replicate the scenario on my instance by trying to copy the value from a custom COMPANY field (Reference type) from an Incident to another custom COMPANY (Reference type) field on a Change via a UI action (Create Change) on the incident, through the following line of code:



changeRequest.setValue("u_rfc_company", current.u_inc_company);



The above piece of code copied the value from the company field on Incident to the company field on the change when I clicked the UI action.



Did you already try this? Did it not work in your case?



Regards
Supriya Bisht


View solution in original post

5 REPLIES 5

James_Neale
Mega Guru

Hey Robert,



You can see in the first code example you've posted from the OOB macro that the consumer field is being set by current.getValue('sys_id').


Line 13 needs to be:


cs.consumer = current.sys_id;



As for cs.company, that field doesn't exist on the table which is why you won't see it. It may be cs.u_company in your instance if you've added it as a custom field to point to your custom table for 'Company Affiliation'.



Cheers,


James


Regarding sys_id: LOL - well that just makes too much sense. But the script still doesn't work to pre-populate Consumer (the reply below from sb1186 does).



As for company, that is on our Task table as an OOB field. Company Affiliation is a custom field.



Thanks for pointing out the sys_ID!


sb1186
Kilo Guru

Hi Robert,



I tried to replicate the scenario on my instance by trying to copy the value from a custom COMPANY field (Reference type) from an Incident to another custom COMPANY (Reference type) field on a Change via a UI action (Create Change) on the incident, through the following line of code:



changeRequest.setValue("u_rfc_company", current.u_inc_company);



The above piece of code copied the value from the company field on Incident to the company field on the change when I clicked the UI action.



Did you already try this? Did it not work in your case?



Regards
Supriya Bisht


This did the trick.   Thanks Supriya!