Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript to open a related quick-create form

Posted on by 19

Hello,

I've been working on a javascript snippet that when you click on a button from the ribbon, it opens new quick create form for a related record. It is a custom entity, and I've created a 1:N relationship, where the Opportunity can have many of these "contact attempt" records tied to it. I'm super close with the code, but I cannot get the related opportunity to populate on the form. When you click the button on an opportunity's ribbon, it should populate that opportunity into the form, but It opens without that value every time. I've worked through a couple of suggestions here in the forum, including adding an onload action (based on this: https://butenko.pro/2020/09/15/js-how-to-get-the-originating-record-reference-in-the-form-script-of-the-quick-create-form/) and using the XRM open form utility (following this: https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg334375(v=crm.8)) But it's just not working. 

Below is my code snippet. Any help/ideas would be appreciated. (aaaaa = replacing the company name in the code)

function contactAttemptQC() {    var entityFormOptions = {};
entityFormOptions["entityName"] = "aaaaa_contactattempt";
entityFormOptions["useQuickCreateForm"] = true;

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();

var formParameters = {};
formParameters["name"] = "Contact Attempt";
formParameters["aaaaa_contactdate"] = today;

// Set lookup column
formParameters["aaaaa_attemptsid"] = "parentRecordReference.id";
formParameters["aaaaa_attemptsidname"] = "parentRecordReference.name";


// Open the form.
Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
});
}
  • HandyK Profile Picture
    HandyK 10 on at
    RE: Javascript to open a related quick-create form

    I have spent hours on this, foolishly following the Microsoft docs. You are an absolute champion - the lookup as an array saved my sanity. Thank you, Toni G!

  • tonigcrm Profile Picture
    tonigcrm 19 on at
    RE: Javascript to open a related quick-create form

    Ok, figured it out. You have to pass an array of info to do a set regarding. Below is my code in case anyone else wants to do something similar!

    function contactAttemptQC(primaryControl) {

       var formContext = primaryControl;

       var recordGuid = formContext.data.entity.getId().replace("{", "").replace("}", "");

       var recordName = formContext.getAttribute("name").getValue();

       var recordType = "opportunity"

       var entityFormOptions = {};

       entityFormOptions["entityName"] = "aaaaa_contactattempt";

       entityFormOptions["useQuickCreateForm"] = true;

       var today = new Date();

       var dd = String(today.getDate()).padStart(2, '0');

       var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!

       var yyyy = today.getFullYear();

       // Set default values for the Contact form

       var formParameters = {};

       formParameters["name"] = "Contact Attempt";

       formParameters["aaaaa_contactdate"] = today;

       // Set lookup column

       formParameters["aaaaa_attempts"] = [{ id: recordGuid, name: recordName, entityType: recordType }];

       // Open the form.

       Xrm.Navigation.openForm(entityFormOptions, formParameters).then(

           function (success) {

               console.log(success);

           },

           function (error) {

               console.log(error);

           });

    }

  • tonigcrm Profile Picture
    tonigcrm 19 on at
    RE: Javascript to open a related quick-create form

    Yes, that was from an on create web resource that didn't work. I've now reworked it without it. below is my new code:

    function contactAttemptQC(primaryControl) {

    var formContext = primaryControl;
    var recordGuid = formContext.data.entity.getId().replace("{", "").replace("}", "");
    var recordName = formContext.getAttribute("Topic").getValue();

    var entityFormOptions = {};
    entityFormOptions["entityName"] = "aaaaa_contactattempt";
    entityFormOptions["useQuickCreateForm"] = true;


    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();


    // Set default values for the Contact form
    var formParameters = {};
    formParameters["name"] = "Contact Attempt";
    formParameters["aaaaa_contactdate"] = today;


    // Set lookup column
    formParameters["aaaaa_attemptsid"] = recordGuid;
    formParameters["aaaaa_attemptsidname"] = recordName; 


    // Open the form.
    Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
    function (success) {
    console.log(success);
    },
    function (error) {
    console.log(error);
    });
    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript to open a related quick-create form

    Hi,

    I think the problem here:

    formParameters["aaaaa_attemptsid"] = "parentRecordReference.id";

       formParameters["aaaaa_attemptsidname"] = "parentRecordReference.name";

    because I can't see this value (parentRecordReference) coming from where?

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,946 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,420 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans