2
votes

Scenario:

Step 1: Am using MS CRM 2011.I write the javascript Function For Get the To Field Value(Email Entity) and set that Value into Regarding Field.It was Working Fine.

Step 2: Write the Process for Update the value in Regarding Field as clear.Because automatically the regarding Field Get some Value.So I need to change that as empty in Regarding field.It was Working fine.

Step 3: now I call the the javascript Function in onchange Event.

Problem: The Regarding Field value Get clear after the process Execute.which means the old value replace and new value get in the Regarding Field.It is Like a on change Event.The old value replaced as a Empty.

But the Function not trigger.If open the email form and change the regarding Field,It was working Fine which means the Javascript can work.

summary: The onchange event didnt not call when I change the value from Process.But I trying manually the javascript working Fine.

How to solve this problem.

**Refer the Following Screen Shots:

Using this Java Script:

Step 1 Coding function TofieldValuetoOtherField() {

var lookup = new Array();
lookup = Xrm.Page.getAttribute("to").getValue();
if (lookup != null) 
{

    var name = lookup[0].name;
    var id = lookup[0].id;
    var entityType = lookup[0].entityType;
    Xrm.Page.getAttribute("regardingobjectid").setValue([{ id: id, name: name, entityType: entityType }]);
     //Xrm.Page.data.entity.save();          

   }}

enter image description here

Call the Onchange like this Its not Fired.

enter image description here

2

2 Answers

6
votes

JavaScript code is called only when the operations (onload, onsave and onchange as in your case) are executed using the web interface.

If you change a field value using a workflow (or a plugin) the attached javascript is not executed, because the code is executed only inside a browser window.

0
votes

If you’re using CRM web interface to create emails then you should bind it all in the OnLoad event. OnLoad calls OnCrmPageLoad function (remove OnChange call since we’re doing it using addOnChange method).

function OnCrmPageLoad() {
   //Apply every time the To field changes
   Xrm.Page.getAttribute("to").addOnChange(TofieldValuetoOtherField); 
   //Apply when the from loads  
   TofieldValuetoOtherField(); 
}

TofieldValuetoOtherField = function()
{
   var lookup = Xrm.Page.getAttribute("to").getValue();
   if (lookup) 
   {
       var name = lookup[0].name;
       var id = lookup[0].id;
       var entityType = lookup[0].entityType;
       Xrm.Page.getAttribute("regardingobjectid")
        .setValue([{ id: id, name: name, entityType: entityType }]);
   }
}

If you need to enforce this behavior when creating emails outside of the web interface i.e. outlook or a webservice then you must use a plug-in to override the regarding field (as mentioned above)