Skip to main content

Notifications

Microsoft Dynamics CRM forum
Suggested answer

Create Custom Button on sub-grid to open new record in a dialog with lookup fields auto-populated

editSubscribe (0) ShareShare
ReportReport
Posted on by 4

Scenario

I have two entities. One is a custom entity called Event Requests, which has a 1:N relationship (respectively) with the Appointment entity.
 
In the Event Requests form, I have a sub-grid that is connected to the Appointment entity, were I would like the user to create Appointment records that are under the created Event Request record. If the user clicks on the OOB (Out of Box) New button on the grid, it will open a main form (in full screen), with the Regarding field and Event Request lookup field pre-populated with the Event Request record. I believe this logic is already implemented through the 1:N relationship.
 
What I am trying to do is configure it so that when a user clicks on the new button in the sub-grid, it opens the Appointment form in a dialog window (instead of full screen) and the form will also have the Regarding field and Event Request lookup field pre-populated (and hidden) with the Event Request record (similar to how the OOB New button works). I first thought of using a quick create form as a possible solution, because it would auto-populate the desired lookup fields, but the quick create form has limitations. The limitations that I am facing is the general UI of the form as well as having additional sub-grids on the form.
 
What I have done so far is that I have created a custom button, where I have incorporated the following JS code to open the form in a dialog:

Custom Button Creation Method

Java Script to Open the Form

function OpenMbrEventRequestPage() {
 
// Centre Dialog
var pageInput = {
    pageType: /entityrecord/,
    entityName: /appointment/,
    formType: 2,
};
 
var navigationOptions = {
    target: 2,
    width: {value: 70, unit:/%/},
    position: 1
};
 
Xrm.Navigation.navigateTo(pageInput, navigationOptions);
}

Issue

The issue that I am facing is that the code above just opens a new appointment record, with none of the fields pre-populated. I have tried the following methods in order to have the desired Lookup fields populated with the Event Request record, but I could not get the fields to be populated:
 

Background Workflow

I created a background workflow to be triggered, when a new appointment record has been created. In the workflow, I have stated a condition that if the Event Request choice field is /Yes/, then the Regarding field and Event Request lookup field will be populated with the Event Request dynamic record (Entity: Event Request, Attribute: Event Request)
 
 
 

Mapping

I accessed the Mapping section, where I created new field mapping for the Regarding field and Event Request lookup field 
 
 

Incorporating Custom JavaScript

I am still fairly new to JavaScript, but I did some research online to see if anyone has had the similar issue and resolved it with creating custom JS code. I found a number of postings and tried to apply it to my scenario, with no success. I also tried reviewing the Microsoft documentation to see if there are any solutions, but I couldn't find anything that would help.   
 

Request

Any ideas on how to resolve this with a possible solution? If this solution requires a custom JavaScript code, can you please show me the code as I am fairly new to JS.
 
 
Any assistance that you can provide would be greatly appreciated.
 
Thank you
 
Sanjaya
  • SK-29040614-0 Profile Picture
    SK-29040614-0 4 on at
    Create Custom Button on sub-grid to open new record in a dialog with lookup fields auto-populated
    Correct. I want to pull the account that appears in the event request account field to either the regarding or account field. I was testing out the code to see if it would work if it was copied to the regarding field. 

    Regarding the link that you have provided, it looks as if the code is for populating fields on the same form. What if you have fields that happen to be on different forms/entities? What part of the code shows the entity? Is it EntityType?
     
     
     
  • Suggested answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    Create Custom Button on sub-grid to open new record in a dialog with lookup fields auto-populated
    Hi,
    1.stored the GUID for the account record
    There is a lookup field on Event Requests that points to the account entity.
    What you're trying to get should be the account lookup field value on the 'event request' record,Right?
    So:
    How to get lookup field value.
     
    2.Update Regarding field.
    Table name should be account?
     
  • SK-29040614-0 Profile Picture
    SK-29040614-0 4 on at
    Create Custom Button on sub-grid to open new record in a dialog with lookup fields auto-populated
    Hi Leah.
     
    I tested the following script, where I stored the GUID for the account record (i.e. the account I want to populate in the regarding field) in a single line text field in the event request entity. When I uploaded the script, and clicked on the custom button, nothing was happening. Any ideas? 
     
    function OpenMbrEventRequestPage(executionContext) {
     
        var formContext = executionContext.getFormContext();
    {
     
        var formParameters = {};
     
    //Pass the regarding value
        var regardingObjectId = formContext.getAttribute("sksysdev_AccountRecordID").getValue();
        var regardingName = formContext.getAttribute("sksysdev_account_ercrm").getValue();
     
    // Set lookup column
            formParameters["regardingobjectid"] = regardingObjectId; // ID of the user.
            formParameters["regardingobjectidname"] = regardingName; // Name of the user.
            formParameters["regardingobjectidtype"] = "sksysdev_membereventrequest"; // Table name.
    // End of set lookup column
     
    // Define the entity name to open the form
        var entityFormOptions = {};
            entityFormOptions["entityName"] = "appointment";
     
    // Open the form
    Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
        function (success) {
        console.log(success);
        },
        function (error) {
        console.log(error);
        });
        }
    }
  • Suggested answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    Create Custom Button on sub-grid to open new record in a dialog with lookup fields auto-populated
    Hi,
    Maybe you can try to use Xrm.Navigation.openForm().
    You can set height and width if you don't want to open full screen.
    For passing value to the new opened form,please refer to the following blog:
     

Helpful resources

Quick Links

Take the Community feedback survey!

Answer this brief 15-question survey about your Community experience…

Demystifying Copilot: Service Edition with Sundar Raghavan

Sundar answers more questions about Copilot for Service...

Dynamics 365 Business Central vs Finance and SCM

Take a look at the key differences between Business Central and…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,361 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,155 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans