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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Using javascript web resource to store field value of a record

(0) ShareShare
ReportReport
Posted on by

Hi,

Please excuse my ignorance on this topic -- I'd like to be able to use a jscript web resource to store some particular field values (to be used later). I'm having trouble figuring out how to get certain values.

I have a "registration" entity and an "event/class" entity. When a user is registering someone for an event/class, I'd like to be able to use the jscript resource to find and store a field value on the chosen event/class. When user's open up a registration, there is no event/class chosen yet. I'd like an OnChange event to fire when the user picks an event/class (a lookup field), and that OnChange event looks up and stores the value of a field in the chosen event/class. 

I have a good idea on how to store values for the form you're currently using, but I am having trouble figuring out how to get at the values on this "next layer" form. 

If someone could point me in the right direction, that would be greatly appreciated. Thanks. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    Unfortunately, you can't store values in web resources. Any fields/values that you want to maintain need to be saved in CRM proper as attributes on entities.

    It sounds like you are trying to set a field that tracks the number of registrations. If that's the case, you should write a plugin on create and delete of the registration. The plugin will run, and if there's a class selected, query for the number of registrations for the class, then save it on the class.

    I might have missed your objective altogether, but I'll be happy to try again if you give me some more details.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Suggested answer
    Community Member Profile Picture
    on at

    If two entities are related this link will help you get(read)  what you have difficulty

    msdn.microsoft.com/.../mt607871.aspx

  • fishyjj Profile Picture
    on at

    Thanks for your reply. I am not trying to track registrations with this -- I'd like to be able to dynamically pull in registration questions that are associated with particular events. On the event form I have an "event category" field that acts as a bridge to the set of questions associated with that event. So, I need to be able to access that "event category" field with some code and use it to pull in the appropriate questions for a registration. Hope that makes sense. If you have any insight given that extra info, I'd greatly appreciate it. I will mark your response as having answered my question.

    Thanks.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    It does make sense - so are you trying to save the answers to the different questions? If that's the case, I'd create an entity "to hold those values.

    If your question text is in javascript, create an entity "Registration Question". This'll be associated with the registration and you'll create one record for each question you ask, so each record has one question and one answer.

    You could also have a meeting question entity, where the question is associated with the meeting. Your Registration Answer would have a lookup to the Registration and to the original question.

    Let me know if I misunderstood the requirement. I hope this helps! I'd appreciate if you'r mark this as Answering your question.

    Thanks,

     Aiden

  • Verified answer
    MilindP Profile Picture
    1,019 on at

    Create a web resource and on load of the web resource check the value of Eventid using window.parent.Xrm...... and load the question in the web resource as labels and create the html text fields to receive at the answers. On change of the Answers, set the value of answers on parent form(assuming the parent form is having hidden fields for answers to set the value).

    Refresh the web resource on change of event value lookup on parent form i.e. using

    var wrControl = Xrm.Page.ui.controls.get("webresourcename");

    wrControl.setSrc(wrControl.getSrc());

  • fishyjj Profile Picture
    on at

    Thanks for your reply, this is helpful. I was able to successfully write a script that checks the eventid onChange of the "Event Title" field on the registration form.

    Here's my script (alert added just for testing) --

    function checkGuid () {

           var eventId = Xrm.Page.data.entity.attributes.get("new_eventregistrationrelationshipid").getValue()[0].id;

    alert(eventId);

    }

    Now, is there a way to use that guid to check the value of a field on the event form? There is a field called "Event Category" that I'd like to be able to check and use to pull in registration questions (bridged using the same event category), if that makes sense.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    Now that you have the event ID, you can retrieve the record from CRM in javascript like this: https://msdn.microsoft.com/en-us/library/gg334427.aspx . When you retrieve the record you can get the attribute from the result.

    Thanks,

      Aiden

  • fishyjj Profile Picture
    on at

    Thanks for the link. I'm working my way through understanding it and watching some videos. This is a little outside my current ability.

    Question for you - to implement the Odata stuff, do I need to include certain js libraries as web resources? Like one for json for example?

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    You will need to include a javascript library but it's free from Microsoft. In that link I sent you, download the Microsoft Dynamics CRM SDK package. There's a ton of sample code and references, and the file you'll need is sample_/Scripts/SDK.REST.js

    To include the file, you'll need to add it to CRM as a web resource. In that example, it was named "Scripts/SDK.REST.js", so to include the file in code they're using:
    <script src="Scripts/SDK.REST.js" type="text/javascript"></script>

    When you make a REST call to CRM, everything happens asynchronously. In the Plugins, when you retrieve a record, on the next line you have the record to manipulate. With the REST service, you make the request and tell it the function to call if it's successful, or if it fails. Here's an example from arunpotti.wordpress.com/.../restretrieveexample

    function retrieveContact(ContactId) {
     SDK.REST.retrieveRecord(ContactId, "Contact", null, null, getDetails, errorHandler);
    }
    
    function getDetails(contact) {
     alert("Full Name : "+ contact.FullName + "\n" +
     "Company Name : " + contact.ParentCustomerId.Name + "\n" +
     "Guid : " + contact.ContactId +"\n" +
     "Preferred Method of Contact : " + contact.PreferredContactMethodCode.Value +"\n" +
     "Bulk Email : " + contact.DoNotBulkEMail +"\n"+
     "Birthday : " + contact.BirthDate
     );
    }
    
    function errorHandler(error) {
     alert(error.message);
    }

    SDK.REST.retrieveRecord is the call to get the contact record, getDetails is the function to call if the retrieve is successful, and errorHandler will be called if there's an error. In this code, when the retrieve is complete then the getDetails method pops up some information about the contact.

    In your case, you'd want to do something similar where you'r retrieving the event, and if the event is successfully retrieved, get the Event Type field and continue with your business logic.

    Hope this helps!

      Aiden

  • fishyjj Profile Picture
    on at

    Thanks! This is super helpful.

    I'm using CRM online, so I downloaded the SDK package for online. This package does not include the "json2" script file to add as a webresource as is referenced in the example you posted. Is there an important difference between using online vs. on-prem?

    I followed the example, but did not include the json2 web resource because it wasn't in the files, and I experienced a couple errors. I'm unsure if my errors are a result of mistakes I've made or the lack of the json2 web resource (or whatever else is necessary for crm online).

    Again, thanks for the help!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans