Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting Parent Record ID during OnCreate (PreValidate)

Posted on by Microsoft Employee

Dynamics CRM 2015 On-Premise

Need to do some validations / calculations before a child record is created. Was thinking of a plugin during Create Message (Pre Validate). How can I get the parent ID of the child record being created. We will be using Quick Create form to create the child record (on click on + form the subgrid). Is plugin the right was or should we go for JScript?

Appreciate your help.

Regards

AbD

*This post is locked for comments

  • Verified answer
    Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    Hi
    This will helpful


    var objEntity = (Entity)context.InputParameters["Target"];

    //Item lookup in the itemservice create form.

    var objItem=(EntityReference)objEntity.getAttribute["ItemLookup"];

    var objItemid=objItem.Id;

    The same thing you have can also do it in java script on quick create form on save event.

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    Ah right - thanks for the clear description!

    You will be able to access the parent lookup value using the target image via the attribute logical name.

    Something like

    var entity = (Entity)context.InputParameters["Target"];

    var parentid = entity["new_parentid"];

    See msdn.microsoft.com/.../gg309673.aspx

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    Hello Scott, thanks for the answer. I am afraid I was not very clear with the requirement.

    I totally get the point that the record ID is not created until the record if created. My requirement is not to get "that" record ID, but the ID of the parent to which the child record is being created.

    I will try to explain the requirement. I have a custom entity called "item" which is being budgeted for the year. The item will be shared among one or more "services" (another custom entity). I need to allocate this item to one or more services for which I have an intermediate custom entity called "ItemServices" (similar to "Opportunity", "Product" and "OpportunityProduct" concept in OOB). ItemService records will be child to Item record where the service will be selected and a percentage allocated field need to be filled. The collective percentage should not exceed 100, which is the validation required. Quick Create forms are used to create ItemService record when + on the ItemService subgrid on item is pressed. So, before save I need to get the parent record ID and check all the other ItemServices record associated with that item and validate the percentage entered whether the total is equal to or below 100%. Hope this is clear. 

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    The record ID is not available (and you cannot create child records using it) until the post operation stage - if you throw an exception at that stage the transaction will not complete and the records will not be created and so it sounds like this is the stage you should use rather than the Pre-validation stage.

    Hope this answers your question.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    Thanks Yu Tian, that's exactly the plan - to throw an exception with clear message if the validation fails.

    The question is if there is a way to get the parent record ID of the record "being created". We will be using Quick Create form for creating child records,

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

     Based on my understanding, you can register the plugin on pre-operation, do your validation in your plugin and if the validation fails, throw the Invalid plugin exception with a user-friendly message such as "the creation is cancelled because the validation is failed." In this case, the child record will not create and you can get the parent ID from the Target or pre-image.

    FYI: the pre-validate plugin triggers outside the database transaction, the only situation I used the pre-validation is for cascade-delete. 

    You can check below link for more details 

    https://community.dynamics.com/crm/f/117/t/192970

    http://stackoverflow.com/questions/8336038/when-would-i-register-a-crm-2011-online-plugin-for-pre-validation-stage

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    Thanks Rafael for the input. The reason I need this on Pre-Validate is that I don't want the user to create the record if the validation fails. Post-Image, as I understand, will create the record before the plugin is run.

  • Suggested answer
    RE: Getting Parent Record ID during OnCreate (PreValidate)

    Hello,

    You can register a plugin an add a Post-Image to the step Message. This will gives the expected structure of the entity after creation. Use these information to build your validations/calculations.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans