New records inserted whenever updated

neharao
Giga Contributor

Hi all,

I have a requirement where i need to populate 'Work Notes' on 'Change Request' form Affected CI's table. I am able to achieve this but i have a

problem i.e whenever i am trying to change 'State' field or do any changes on the form and save each time its creating the same set of Affected

CI's in the work notes.

Please see the below screen shot.

find_real_file.png

My code looks like below:

Business rule : Executes on 'Before' with 'Insert' and 'Update'

var look2=new GlideRecord('task_ci');

  look2.addQuery('task',current.sys_id);

  look2.query();

  while (look2.next())

  {

  current.work_notes = look2.ci_item.getDisplayValue();

  //look2.task = current.sys_id;

  look2.update();

  }

Kindly can anyone help me on this that it should not create the same record again and again each time i change something on form and update.

Thanks,

Neha

1 ACCEPTED SOLUTION

Thanks for the details Neha.



You have to create an AFTER business rule on task_ci table with insert checkbox set to true and with filter condition as task starts with CHG.


This means the BR will be triggered only for change request records and with script as.


(function executeRule(current, previous /*null when async*/) {



  // Add your code here


  var gr = new GlideRecord('change_request');


  gr.addQuery('sys_id', current.task);


  gr.query();


  if(gr.next())


  {


  gr.work_notes = current.ci_item.getDisplayValue();


  gr.update();


  }



})(current, previous);






Please let me know if you have any questions.


View solution in original post

18 REPLIES 18

neharao
Giga Contributor

Hi Pradeep,



Exact requirement is whenerver i add Affected CIs in the Affected CI tab it should populate the value in Work notes(Journal field). The problem i am facing is if i add Affected CIs and make some changes in the 'Change Request' form it gets added in the Work Notes field every time even though no new Affected CIs are added.



Please find the screen shot below:


find_real_file.png



In the above screen shot the same CIs are getting added every time when i make some changes on the change request form. I do not want it to be created again and again.



Please find the screen shot of my code below:



find_real_file.png




Kindly let me know how to handle this.



Thanks,


Neha


Thanks for the details Neha.



You have to create an AFTER business rule on task_ci table with insert checkbox set to true and with filter condition as task starts with CHG.


This means the BR will be triggered only for change request records and with script as.


(function executeRule(current, previous /*null when async*/) {



  // Add your code here


  var gr = new GlideRecord('change_request');


  gr.addQuery('sys_id', current.task);


  gr.query();


  if(gr.next())


  {


  gr.work_notes = current.ci_item.getDisplayValue();


  gr.update();


  }



})(current, previous);






Please let me know if you have any questions.


neharao
Giga Contributor

Hi Pradeep,



Thanks a lot . This is working fine now . But i am not able to mark this answer as correct answer as i don't find the option


Excellent. Thanks Neha for the update.


dan.bruhn..Can you please help. Thank you.