Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Show value in lookup as default

(0) ShareShare
ReportReport
Posted on by

Hi, 

Looking for the solution to autofill lookup with value (If it has only one result) . I am expecting only one result in lookup in some cases and so would like to show that result as default instead of expecting it to be selected by end users.

I have tried below code but it is  not working as expected

var lookup = new Array();
lookup = Xrm.Page.getAttribute("new_account_lookup").getValue();
if (lookup != null) {
Xrm.Page.getAttribute("new_account_lookup").setValue([{ id: lookup[0].id, name: lookup[0].name, entityType: lookup[0].entityType}]);
}

Thank you,

Raj

*This post is locked for comments

  • Suggested answer
    Rutul Chauhan Profile Picture
    Rutul Chauhan 2 on at
    RE: Show value in lookup as default

    instead of writing jscript, you can achieve this through real time workflow. So if you want this lookup value needs to be selected at the time of record creation, create real time workflow with scope: organization and start when record create.

    Do not put any condition and directly select action as Update record. In set properties of record, select lookup value

    I hope this will help you

    Regards,

    Rutul

  • Verified answer
    RE: Show value in lookup as default

    Hi Rajnikant,

    I will try to help you as much as I can. 

    entityName : is the name of the entity that you want to retrieve your data from
    "?$filter=" after that expression you need to write your own filter code (in that example when new_gorusmepuani field's value is equal to parameter's value it returns the whole dataset and then you can use the fileds you need the fill your look up)
    retrieveRecord: is an another helper method for Odata.

    function SetLookup() {
    	 //get your parameters values here
     var oDataUrl = "?$filter=new_gorusmepuani /Value eq " + parameter + "";
            var oDataSetName = "entityNameSet";
            var result = retrieveRecord(oDataSetName, oDataUrl).results;
            if (result != null ) {
                var detail = result[0];
                if (detail != null) {
                    var rName = detail.new_name;
                    var rId = detail.new_gorusmenotuId;
                    var rEnt = "entityName"; 
                    Xrm.Page.getAttribute("fieldName").setSubmitMode("always");
                    Xrm.Page.getAttribute("fieldName").setValue([{ id: rId, name: rName, entityType: rEnt }]);
                    
                }
            }
    		
     }	

    function retrieveRecord(odataSetName, url) {
        if (!odataSetName) {
            alert("odataSetName is required.");
            return;
        }
    
        var serverUrl = "http://" + location.href.split("/")[2];
        var ODataPath = serverUrl + "/" + Xrm.Page.context.getOrgUniqueName() + "/XRMServices/2011/OrganizationData.svc/";
        var fullUrl = ODataPath + odataSetName + url;
    
        var result = null;
        jQuery.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            async: false,
            cache: false,
            url: fullUrl,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                result = data.d;
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                result = "error";
            }
        });
    
        return result;
    }


    Hope it gives you an idea about it.

    Regards

  • RE: Show value in lookup as default

    Hi Ezgi,

    I will be getting different value everytime. Can you please help me with the code for oData? I have just started working on CRM in last couple of days so don't know how it works and kind of scripts that are required.

    Regards,

    Rajnikant

  • Suggested answer
    RE: Show value in lookup as default

    Hi, 

    You can not fill your lookup in that way. At the time you get the value of the field it is empty so it won't work in the way you want. So I suggest you to use the code below. 

        var recordName = "Test Name"; 
        var recordId = "4168361C-675D-E611-80BE-005056B69D88";
        var recordEntityName = "product";
        Xrm.Page.getAttribute("fieldName").setSubmitMode("always");
        Xrm.Page.getAttribute("fieldName").setValue([{ id: recordId , name: recordName , entityType: recordEntityName }]);


    If the value you want to fill your lookup is always same then you can give the Name,Id and EntityTypeName static. Otherwise you need to use oData to retrieve the data you want to use.

    Hope it helps,

    Regards

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Show value in lookup as default

    This is not a correct way, if you want to fill your lookup, you need to write a query to the target entity and check if result set is just 1 record, read and then set it. it will be something like this: http://himbap.com/blog/?p=657

    You can change your query criteria accordingly.

    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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans