Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Auto assign a lookup field with Test Account

Posted on by 160

Hi All,

I have customized the Case entity and in no need of "Customer" field. However, I couldn't remove it from the form, as it's mandatory. So, I decided to hide it in the form and then auto assign the "Test Account" through Business Rule.

However, it failed in QA or PROD, cos the business rule is looking for the same "Test Account" based on the GUID and not by name.

I was told that, JavaScript is the only way to auto assign "Test Account", which will work in QA and PROD as well.

As I am not familiar with javascript, can someone help me with the code for this functionality?

Thanks,

Praveen.

*This post is locked for comments

  • RE: Auto assign a lookup field with Test Account

    Thanks All for your replies. Setting the Account value using javascript best works for our scenario and will be going with that approach.

    Thanks again,

    Praveen.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Auto assign a lookup field with Test Account

    Hi Praveen,

    If you are not using the customer field then you must be using some other field to capture the account or contact record. You can instead of setting the "Test Account" set the value of that Account/ Contact field.

    The other option is to create new test account records on QA & prod and use that in the workflow. But if you do solution import again, then it would overwrite with the record from dev.

    The other option is to create a test account record on QA & prod with the same guid value. There are various ways to do this but i think the below is the simplest-

    1) Connect to XRM Tool Box. Connect to You CRM instance QA or Prod

    2) Connect to plugin called "Code Now"

    3) Click Flie >> New Script. This will open the code editor to execute the code against your crm instance

    4) Replace the existing code with the below code, you need to add your guid value (without brackets e.g. D51B321F-8564-E811-A960-000D3AE05E60)

    ------------------------

    public static void CodeNow(){

       Entity newAccount = new Entity("account");

       newAccount["accountid"] = new Guid("<paste your guid here>");

       newAccount["name"] = "Test Account";

       Service.Create(newAccount);

    }

    ------------------------

    5) Run the code. This will create the new account record named "Test Account" with the same guid.

    ** You can test this on QA first and then on Prod

    Hope this helps.

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Auto assign a lookup field with Test Account

    Hi,

    You can migrate your record with the GUID or you can simply use following code on the form load to set the lookup based on the account name

    function GetAccountBasedOnName()

    {

    var accountname="Test Account";

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=accountid&$filter=name eq '+accountname+"'", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var accountid = results.value[i]["accountid"];

               var lookupValue = new Array();

              lookupValue[0] = new Object();

              lookupValue[0].id = accountid ;

              lookupValue[0].name = accountname;

              lookupValue[0].entityType = "account";

            Xrm.Page.getAttribute("FieldName").setValue(lookupValue);

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    }

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Auto assign a lookup field with Test Account

    Hi Praveen Kumar Ellappa,

    Read this article to learn how to Get and Set a Lookup Field Using JavaScript in Dynamics 365.

  • RE: Auto assign a lookup field with Test Account

    Hi Martyna,

    I didn't think from that angle. Will the same GUID be present when I export and import the Test Account in QA or PROD?

    Regards,

    Praveen.

  • Verified answer
    Martyna Profile Picture
    Martyna 470 on at
    RE: Auto assign a lookup field with Test Account

    hello, Have you considered importing the Test Account record with Data Import tool into your QA and PROD environments, so that it preserves the GUID?

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