4

CRM 2013 customizer/developer here. I'm new to JavaScript and I need some help on the query I can use in an OnLoad event in CRM 2013.

On the 'Appointments' entity I need to set the value of a custom field (option set) based on entity type of it's related parent record (which is 'regardingobjectid').

Example;

-If 'regardingobjectid' Entity type = 'Account' then set value of "custom field" to "x",

-If 'regardingobjectid' Entity type = 'Contact' then set value of "custom field" to "y".

The custom field is an option set with 3 possible values (x,y,z) so if its possible to hide value "z" when 'regardingobjectid' Entity type = 'Contact' that would be awesome. Any help would be super appreciated.

user3871442
  • 45
  • 1
  • 4

1 Answers1

5

Add new function to OnLoad of Appointment entity.

function onLoadOfAppointment() {
if (Xrm.Page.ui.getFormType() == 2) {

    var regardingObject = Xrm.Page.getAttribute("regardingobjectid");
    if (regardingObject != null && regardingObject.getValue() != null)
    {
        var entityType = regardingObject.getValue()[0].entityType;
        if (entityType == "account")
        {
            //Add account logic here
        }
        else if (entityType == "contact")
        {
            //Add contact logic here
        }
    }
}
}

To Hide show OptionSet values. Follow below urls:

dynamically-change-option-set-values-in-crm

Add new Picklist Option using Javascript

Xrm.Page.ui control (client-side reference)

Community
  • 1
  • 1
Dot_NET Pro
  • 2,095
  • 2
  • 21
  • 38
  • Your code would throw an exception in case regarding object field would be empty. It would be correct to add a check like regardingObject.getValue() != null. – Andrew Butenko Sep 01 '15 at 10:28
  • Thanks guys! The code worked, the only thing is that the on load event is triggered only after a save. I think this is because the code doesn't recognize that there is anything in the 'regardingobject' at the time of the form loading. Any way around this? – user3871442 Sep 01 '15 at 23:14
  • @user3871442 the OnLoad event is triggered every time the form loads/a record is open in crm. `if (Xrm.Page.ui.getFormType() == 2)` //this will prevent script to be executed on the new record onload. if you need for new records also, then `add your function to onsave` and check `if (Xrm.Page.ui.getFormType() == 1)` the execute. – Dot_NET Pro Sep 02 '15 at 05:15
  • @user3871442 you should accept my answer as answer. This will help others looking for answer and the accepted answer thank you. – Dot_NET Pro Sep 02 '15 at 05:16