Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Auto Populate Fields in Case entity based on Lookup from Account entity

Posted on by Microsoft Employee

Hi experts,

I have a bunch of True/False fields in the account record:

Screen-Shot-2018_2D00_05_2D00_02-at-4.06.25-PM.png

I also have replicated the same fields in the case record:

Screen-Shot-2018_2D00_05_2D00_02-at-4.06.43-PM.png

I have a account lookup field in case record called customerid. Now when I load the case form, I would like to auto populate the values of these field from account to case. 

I tried field mapping, but that only works on create of record and doesn't update when values change. So, I used the following script to auto populate but it only works, if the fields are marked as True and doesn't work when they are False.

function getValues()
{
var contactId;
var res;
var lookupItem = Xrm.Page.getAttribute("customerid").getValue(); 
if (lookupItem != null)
{
contactId = lookupItem[0].id;

// Added new line
res = contactId.replace("{", "").replace("}", "");
}
else
{
return;
}
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts("+res+")?$select=new_redflag,new_dns", false); 
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 result = JSON.parse(this.response);
var redflag = result["new_redflag"]; 
var dns = result["new_dns"];

if(redflag)
{
Xrm.Page.getAttribute("new_redflag").setValue(redflag);
}
if(dns)
{
Xrm.Page.getAttribute("new_dns").setValue(dns); 
}
} 
else 
{
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(); 
}


How can I change my code for this to work?

Thanks,

Jon

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Populate Fields in Case entity based on Lookup from Account entity

    Hi Jon,

    In all honesty I would suggest another approach to your requirement. Not having looked at your code in detail I would suggest a no-code solution here. Because if you have an onload function it will only populate when you re-open your form after doing some changes to the checkbox fields.

    If you create a workflow that triggers on the changevent of those fields you could achieve what you would like and it will happen onsave of the record. In your workflow you define the onchange of all the checkboxes to either update individual fieldvalues based on if statements or all checkbox values after 1 of them changed.

    In my opinion this will lead to more accurate data in your Dynamics Environment.

    With kind regards,

    Joseph

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Populate Fields in Case entity based on Lookup from Account entity

    Hi everyone,

    Figured out that I will have to remove the if loops and it started to work.

    Thanks,

    Jon

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans