Thursday, 1 February 2018

Enable +New button in the Lookup view of the Dynamics 365 portal.




Hello Everyone,

I have been seen a lot of people were finding a way or solution to add a custom button or 'New' button inside Lookup dialog in order to create a new record directly from Portal if not found in lookup Search result.

Here are the few links where it has been asked:

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

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


Here is the code I have written to achieve the same:

// Calling of Function

AddNewButton_InLookupDiaog('customerid','Select','customeridNewBtnId','Add New','~/create-account','Authenticated');


// lookupfieldScehmaName = schema name of lookup field
// NewButtonPosition = Position, where you want to place the 'New' Button inside Lookup Window
// NewButtonId = Id of New Button, Make sure this should be unique
// NewButtonText = text to display of New Button
// NewButtonActionURL = Provide webpage partial URL or the page where you want to redirect the user in order to create new record
// Webrole = Role to which 'New' button should be visible. If passed 'Authenticated' then 'New' would be visible to Authenticated Users only.

// Function Definition
function AddNewButton_InLookupDiaog(lookupfieldScehmaName,NewButtonPosition, NewButtonId,NewButtonText,NewButtonActionURL,Webrole)
{
var loggedInUserRole = '{{user.roles}}';

if(loggedInUserRole.indexOf(Webrole) == -1){
    return;
}
var elementId = lookupfieldScehmaName+"_lookupmodal";

$("#"+elementId).closest('table').on("loaded", function ()
{
var IsAddNewButtonAdded = $("#"+NewButtonId).length;

// If button is not added then add the button
if(IsAddNewButtonAdded == 0)
{
$("#"+elementId).find('button:contains("'+NewButtonPosition+'")').before('<button id="'+NewButtonId+'" aria-label="New" class="primary btn btn-primary" tabindex="0" title="New" type="button" onclick="NewButtonAction('+"'"+NewButtonActionURL+"'"+')">'+NewButtonText+'</button>');
}
});
}

function NewButtonAction(redirectionURL)
{
window.open(redirectionURL,'_blank');
}


Demo:

Log in to CRM Portal :






















We are going to add a 'Add New' button in 'Customer' Lookup Dialog:










Press F12 or Inspect to get the Schema name of the lookup field. You can check the schema name of the field from CRM also from Form Customization :















Add code to Add Add 'New' button in Customer Lookup Dialog :

Open Web page > Go to Advanced Tab > Custom Javascript and paste the given code as mentioned below:








Output :



Once clicked on 'Add New'




















Let's have a look all the Parameters one by one:

Parameter 1 - lookupfieldScehmaName:

For Example, It's value =  'customerid'








Parameter 2 - NewButtonPosition:

For Example, It's value =  'Select'











Parameter 3 - NewButtonId:

For Example, It's value =  'customeridNewBtnId'










Parameter 4 - NewButtonText:

For Example, It's value  = 'Add New'













Parameter 5 - NewButtonActionURL:

For Example, It's value =  '~/create-account'













Parameter 6 - Webrole:

For Example, It's value = 'Authenticated'











Note - 

  • To add 'New' button in multiple lookups on the form, you just need to call the function again with required parameters of each lookup. The Function definition will remain same.
  • Make sure you are providing the unique Id for each button ('New') for every lookup dialog.


Please do not forget to share your feedback:

Cheers 😎

6 comments:

  1. Is is possible to do this on Web client? rather than on portal

    ReplyDelete
  2. I am referring to https://community.dynamics.com/crm/b/debajitcrm/archive/2018/06/04/open-lookup-dialog-programmatically-using-xrm-utility-dynamics-v9-0

    I need to have addition +New button

    ReplyDelete
  3. Hiya, I am really glad I’ve found this information. Nowadays bloggers publish only about gossip and web stuff and this is really irritating. A good blog with interesting content, this is what I need. Thanks for making this web-site, and I will be visiting again. Do you do newsletters by email?
    start up firm

    ReplyDelete
  4. Good blog and it helps especially on the webforms where metadata configuration for lookup field doesn't work to show the new button.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete

Blogger Widgets