Do not insert if record allready exists

Vineetha Rohra1
Giga Guru

I have a requirement where i do not want to get a record inserted into a table if it is allready present.

I have a form as follows:

find_real_file.png

For each project code i will have to enter 12 month's amount, meaning For '123' project code i need to enter amount for 123-january upto 123-december.

But i dont want to enter 2 entries for 123-january or any month.

I wrote a business rule for this but it doesn't seem to wotk:

BR:

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

  var amt = current.amount;

  gs.info(amt);

  var month = current.month;

  // Add your code here

  var gr = new GlideRecord("x_71260_pmo_develo_billing_details");

  //gr.addQuery("project_code", current.project_code);

  //gr.addQuery("project_status", current.project_status);

  //gr.addQuery("year", current.year);

  gr.query();

  if(gr.addQuery("month","==", month))

  {

  gs.info("inside IF");

  gs.info("You have allready entered PO amount for this month for this project.");

  gs.addErrorMessage("You have allready entered PO amount for this month for this project.");

  current.setAbortAction(true);

  }

})(current, previous);

1 ACCEPTED SOLUTION

vipinmathew
Mega Guru

Hi Vineetha,



It's easy to do by code( Business Rule).



var gr = new Gliderecord('your table name');


gr.addQuery('ProjectCode',current.projectcode);


gr.addQuery('ProjectName',current.projectname);


gr.addQuery('Projectstatus',current.projectstatus);


.........................................................................


...............// you can add what ever fields u have unique in field



gr.query();



if(gr.getRowCount() > 0)


{


current.setAbortAction(true);


}



This is generic script ,, modify as u want.



if it is correct answer for you , please mark this answer as CORRECT .




regards,


VIPIN MATHEW


View solution in original post

8 REPLIES 8

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Why let the user do this? if it always needs to create this, why not make it automatically?



//Göran


This i need for an application, where these values should be entered by a project manager.


Deepa Srivastav
Kilo Sage

You mean Amount field will contain Month name also ?


if yes then you can use split by '-' and the take second value of the result and use glideAggregate to take the count if records. if its more than 1 then abort action.



Untested code...



var gr = new GlideRecord("x_71260_pmo_develo_billing_details");


  gr.addQuery("project_code", current.project_code); // current.project_code returns sys_id or name? else modify to below


gr.addQuery("sys_id", current.project_code);


gr.query();


var test=gr.amount.split('-');


if (gr.next())


{


if (current.amount.split('-')[1]==test[1])


{


gs.addErrorMessage("You have allready entered PO amount for this month for this project.");



  current.setAbortAction(true);


}


}




Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


No amount field does not contain month,there's a separate field for month, scroll to the right of image that i attached.