jimminybob
jimminybob

Reputation: 1442

Deleting a record in javascript

I need to delete some records related to the current record when it is deactivated. I can get the the event when the record is deactivated but I have looked around for some time on Google and this site for the code to delete records in javascript but I can't find any, though I know there must be some out there.

Can anyone help?

Thanks

Upvotes: 0

Views: 833

Answers (2)

James Wood
James Wood

Reputation: 17562

If you really want to delete a record with JavaScript there is a sample on the MSDN.

Its a little long winded (its a CRUD example - create, retrieve, update & delete). But it should contain the information you need.

Note there is also an example on that page which doesnt use jQuery (if using jQuery is a problem).

That said I think for this operation would will find it easier to implement, test and maintain with a plugin (so I would go for Greg's answer).

Additionally a plugin will apply in all contexts, e.g. if you deactivate the record in a workflow your JavaScript will not run, but a plugin will.

Upvotes: 3

Greg Owens
Greg Owens

Reputation: 3878

I would be alright with doing this with a plugin, all I would need to know is how to pick up that the record has been deactivated

You can register a plugin on the SetState and SetStateDynamic messages (recommend the Pre event in your scenario). Each of these messages will pass an EntityMoniker in the InputParameters property bag which refers to the record that is being deactivated.

In your code you will need to:

  1. Check that the new state in the SetState request is deactivated (since of course a record can usually be reactivated and you don't want to try deleting things then too, presumably)
  2. Pick up the EntityMoniker from IPluginExecutionContext.InputParameters
  3. Run your query to identify and delete related records
  4. Exit the plugin to allow the SetState transaction to complete

Upvotes: 3

Related Questions