Create/Update record thru workflow script

hongsok
Tera Contributor

Dear all,

I created a catalog item to Create New or Update Existing record on the table name Company [core_company].

There are 3 variables:

  • Create New or Update Existing - Multiple Choice
  • Current Manufactory - reference to Company [core_company]
  • New Manufactory - Single Line Test

Note that: the field that need to update/create new in theCompany [core_company] table call name.

I created the following workflow script to update/create new record somehow it is not working.

var mft = current.current_manufacturer_name;
var gr = new GlideRecord('core_company');
gr.addQuery('name', mft);
gr.query();

if (gr.next())

{
current.setAbortAction(true);
gr.name = current.new_manufacturer_name;
gr.update();
}

I would appreciate your help.

 

Regards,

Hong

 

 

1 ACCEPTED SOLUTION

Hello,

Please see my last reply, above.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

11 REPLIES 11

Hi,

As I've mentioned above, you really should be adding logging statements your code otherwise you're just coding in the dark.

Please double-check the variable name you are using.

If the variable is a reference field, then you'd be getting the sys_id and instead need to use:

gr.addQuery('sys_id', mft);

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

I have removed that line and updated the script but it is working.

New Script:

var mft = current.variables.current_manufacturer_name;
var gr = new GlideRecord('core_company');
gr.addQuery('name', mft);
gr.query();

if (gr.next())

{

gr.name = current.variables.new_manufacturer_name;
gr.setWorkflow(false);
gr.update();
}

Hello,

Please see my last reply, above.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

You are right. It is working now.

Maik Skoddow
Tera Patron
Tera Patron

Hi

I cannot reproduce your code and therefore you hopefully have tested it in a background script before.

Two important things are to mention:

(1)

Remove current.setAbortAction(true);

This is nothing for workflows!

(2) 

Before calling gr.update(); you have to insert gr.setWorkflow(false); or you risk huge problems due to endless loops. Remember: Each update can start additional Workflows/Flows/Business Rules!

Kind regards
Maik