Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Add custom view

Posted on by

Dear All,
I have written down the following code to be able to add a custom view for my lookup.
I have a custom entity called hardware quote product.

In the hardware quote product, I have 2 lookup fields: pricelist and product. 
When I choose a certain pricelist, all the products that belong to this pricelist should appear in the product lookup like the built in quote product already existing in CRM.
When I am deploying the code on change of pricelist, seect the pricelist, and then press on product to choose a product, I am getting an error.
What is wrong in my code below? I searched the internet and found out that my requirement can only be done using addcustomview and not presearchlookup since I am searching for a related entity field.
Thank you.


function setCustomView() {
var lookupFieldObject = Xrm.Page.data.entity.attributes.get('edm_pricelist');
var pricelistid = lookupFieldObject.getValue()[0].id;

// add the randomly generated GUID for the view id
var viewId = '{00000000-0000-0000-0000-000000000001}';

//add the entity name
var entityName = "product";

// add the view display name
var viewDisplayName = "Product with Pricelist Lookup";

// fetch xml for filtered products
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
" <entity name='product'>"+
"<attribute name='name' />"+
"<attribute name='productid'/>"+
// "<attribute name='productnumber'/>" +
// "<attribute name='description' />" +
// "<order attribute='productnumber' descending='false' />" +
"<link-entity name='productpricelevel' from='productid' to='productid' alias='aa'>"+"<filter type='and'>"+" <condition attribute='pricelevelid' operator='eq' '"+pricelistid+"'/>"+
"</filter>"+
"</link-entity>"+
"</entity>"+
"</fetch>";

var layoutXml = "<grid name='resultset' object='1' jump='productid' select='1' icon='1' preview='1'>" +
"<row name='result' id='productid'>"+
"<cell name='name' width='150' />"+
// "<cell name='createdon' width='150'/>" +
"</row>"+
"</grid>";

// add the custom view for the lookup field
Xrm.Page.getControl('edm_product').addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}

*This post is locked for comments

  • Verified answer
    Nour Profile Picture
    Nour on at
    RE: Add custom view

    I was missing a space in my fetchXML./

  • Suggested answer
    AbiRami Profile Picture
    AbiRami 516 on at
    RE: Add custom view

    Hi,

    May be because of the format. Copy the below format for fetchxml and layout.

    <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='new_projecttemplateitem'><attribute name='new_name' /><filter type='and'><condition attribute='new_projecttemplate' operator='eq' value='" + projectTemplateId + "' /></filter></entity></fetch>

    <grid name='resultset' object='1' jump='productid' select='1' icon='1' preview='1'><row name='result' id='new_projecttemplateitemid'><cell name='new_name' width='150' /><cell name='createdon' width='150' /></row></grid>

    Thanks

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans