Search code examples
javascriptdynamics-crm

Create record of Type B when record of Type A is created [JS, D365 Online]


I am trying to implement an 'on record creation' event, with the following scenario.

  • User creates new record of type A

  • Upon first save of that record, automatically create a record via JavaScript of type B

I tried to create an on change event for the 'created on' field, but got the following error.

Xrm.Page.getAttribute("createdon").addOnChange(createOMTRecordUponCreatingPlan)

Error = Cannot read property 'addOnChange' of null


Solution

  • Using JavaScript to "detect" a record change on the form isn't the most viable idea as it'll not be reliable if the record is updated outside of the form window.

    Your best choice is using a server-side plugin, here's a Microsoft tutorial on how to begin creating the project (it's much simpler than it looks).

    You'll need (as you'll see in the tutorial) a Plugin Registration Tool - it's super easy to navigate around and create steps (events) for your plugins, you'll be looking for "Create" message for "Type A" entity.

    Another alternative (might not be a possibility) is by using a workflow.