Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

New Form With Multiple Lookup Parameters

(0) ShareShare
ReportReport
Posted on by 215

I am attempting to use the ribbon workbench and create a new "New" button and all is going fine except the JS.

So I am attempting to set several Lookup fields and cannot get it to work properly. If I do a single lookup it populates perfectly fine, I have tested with multiple fields but only one will fill in.

function newService_Button(){
    var parameters = {};
    parameters["formid"] = "GUID NUMBER GOES HERE";
    if(Xrm.Page.getAttribute("cyb_poc") != null){
        parameters["cyb_poc"] = Xrm.Page.getAttribute("cyb_poc").getValue()[0].id.slice(1,-1);
        parameters["cyb_pocname"] = Xrm.Page.getAttribute("cyb_poc").getValue()[0].name;
        parameters["cyb_poctype"] = "contact";
    };
    if(Xrm.Page.getAttribute("cyb_member") != null){
        parameters["cyb_member"] = Xrm.Page.getAttribute("cyb_member").getValue()[0].id.slice(1,-1);
        parameters["cyb_membername"] = Xrm.Page.getAttribute("cyb_member").getValue()[0].name;
        parameters["cyb_membernametype"] = "cyb_member";
    };
    if(Xrm.Page.getAttribute("cyb_loa") != null){
        parameters["cyb_loa"] = Xrm.Page.getAttribute("cyb_loa").getValue()[0].id.slice(1,-1);
        parameters["cyb_loaname"] = Xrm.Page.getAttribute("cyb_loa").getValue()[0].name;
        parameters["cyb_loatype"] = "cyb_loa";
    };
    Xrm.Utility.openEntityForm("cyb_service", null, parameters);
}

All the code works and pulls the information, but doesn't populate on the new form, just one of the lookup fields.

  • Bahalzamon Profile Picture
    Bahalzamon 215 on at
    RE: New Form With Multiple Lookup Parameters

    I was not aware of the ability to mapping fields, this helps so much, thank you!!! :D

    You have a new follower on your sites. :P

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: New Form With Multiple Lookup Parameters

    I believe there is no need to create any buttons. Just use the fields mapping and it'll do the job for you - www.marksgroup.net/.../

  • Bahalzamon Profile Picture
    Bahalzamon 215 on at
    RE: New Form With Multiple Lookup Parameters

    Sure, I have a Service (cyb_service) entity/form that has lookup fields to contact, cyb_loa, and cyb_member.

    I am attempting to use the Ribbon Workbench to create a new "New" button for the sub-grid's, basically hide the current New and add my own custom one.

    As the new button will be on sub-grid's on each of the contact, cyb_loa, and and cyb_member forms not all of the information/fields will be shown/available on those forms.

    Example: There is an lookup field POC (contact) on the Member form but not on the LOA (cyb_loa) form.

    Hence the Xrm.Page.getAttribute("field") != null scenarios, so if it cant find it currently on the form do not attempt to add it to the parameters.

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: New Form With Multiple Lookup Parameters

    Ok. Then I'm a bit lost.

    Can we put the code aside and talk about your scenario and not the implementation?

  • Bahalzamon Profile Picture
    Bahalzamon 215 on at
    RE: New Form With Multiple Lookup Parameters

     a33ik Thank you for the reply, but that is one of the try's I had tried in the past, but I did attempt to use it again just in case I had something different.

    Couple of things though, I have to have it as the Xrm.Page.getAttribute("cyb_poc") vs Xrm.Page.getAttribute("cyb_poc").getValue() due to the fact that that I am applying it to the sub-grid which is on different entities meaning that all those fields will be available to pass in as parameters. So if I call it the way I have it will return null if the field doesn't even exist on the form but return something if it is.

    Also, I did attempt p["cyb_poctype"] = Xrm.Page.getAttribute("cyb_poc").getValue()[0].entityType; for the type with no luck as what I had in there returns the same.

    Using either my code or the one you supplied it only populates the POC (contact) field, but if I remove that from the code it will populate the Member (cyb_member) field.

    For the lulz I even tried using how new records are made with the p["cyb_POC@odata.bind"] = "/contacts("+Xrm.Page.getAttribute("cyb_poc").getValue()[0].id.slice(1,-1)+")"; and that did not work either.

    I am definitely open to any other suggestions.

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: New Form With Multiple Lookup Parameters

    Hello,

    Try the following instead:

    function newService_Button(){
        var parameters = {};
        parameters["formid"] = "GUID NUMBER GOES HERE";
        if(Xrm.Page.getAttribute("cyb_poc").getValue() != null){
            parameters["cyb_poc"] = Xrm.Page.getAttribute("cyb_poc").getValue()[0].id.slice(1,-1);
            parameters["cyb_pocname"] = Xrm.Page.getAttribute("cyb_poc").getValue()[0].name;
        }
        if(Xrm.Page.getAttribute("cyb_member").getValue() != null){
            parameters["cyb_member"] = Xrm.Page.getAttribute("cyb_member").getValue()[0].id.slice(1,-1);
            parameters["cyb_membername"] = Xrm.Page.getAttribute("cyb_member").getValue()[0].name;
        }
        if(Xrm.Page.getAttribute("cyb_loa").getValue() != null){
            parameters["cyb_loa"] = Xrm.Page.getAttribute("cyb_loa").getValue()[0].id.slice(1,-1);
            parameters["cyb_loaname"] = Xrm.Page.getAttribute("cyb_loa").getValue()[0].name;
        }
        Xrm.Utility.openEntityForm("cyb_service", null, parameters);
    }

    You can find explanation why here - learn.microsoft.com/.../set-field-values-using-parameters-passed-form

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans