web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Auto assign a lookup field with Test Account

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Verified answer
    Martyna Profile Picture
    470 on at

    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?

  • Praveen Kumar Ellappa Profile Picture
    160 on at

    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.

  • Suggested answer
    Adrian Begovich Profile Picture
    1,032 Moderator on at

    Hi Praveen Kumar Ellappa,

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

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    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();

    }

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

  • Praveen Kumar Ellappa Profile Picture
    160 on at

    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.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans