Category Archives: Activity Parties

How to Flip Sender and Recipient in a Phone Call Record

Published by:

There is an annoying tendency in Microsoft Dynamics CRM 2011 for the system to set the Recipient of a Phone Call incorrectly. For example, when adding a Phone Call to a Case, it assumes the Recipient is the Account of the Case it is tied to and marks the call as Incoming.

preview flip sender (1)

It doesn’t make sense that an Incoming call would be directed to an Account. Rather, it should be a User.

The business rule I needed to implement was:  if the call is Incoming, the Recipient must be a User and conversely, if the call was Outgoing, the Sender must be a User.

Here is some scripting you can implement – based on Rajeev Pentyala’s blog based here: http://rajeevpentyala.wordpress.com/2012/04/02/get-and-set-partylist-fields-using-jscript/
It’s tricky to work this out since there are Activity Parties / Party Lists involved.

Note here that I have some utility functions that contain some Xrm.Page functions. This will make it easier to update encase there is another change in Microsoft’s syntax such as the 4.0 to 2011 transition.

Here are my definitions:

· getFieldValue returns Xrm.Page.data.entity.attributes.get(field).getValue()
· getAttribute returns Xrm.Page.getAttribute(field)
· getAttributeValue returns Xrm.Page.getAttribute(field).getValue()

First, the following function will return true if the field contains a record of the type you need. For example, if you want to see if Sender is a User, send it “from” and “8”. This just pulls the values from the array and if it finds one of the type you need, returns true.

function isPartyPresent(field, type)
{

      // Parameters :
      // Field is the field to look in
      // Type is the ObjectTypeCode of the thing you are looking for (eg 8 = User)
      var partyArray = getAttributeValue(field);
      if (partyArray == null)
      {  // If partyArray comes back null, there is nothing there, so there is no party present.
            return false;
      }
      for (var indxParties = 0; indxParties < partyArray.length; indxParties++) {
      if (partyArray[indxParties].type == 8) {
            return true;
      }
}
return false;
}

Next, implement a quick function that will tell you if there is an “inversion.”

function detectPartyInversion()

{
      var direction = getFieldValue("directioncode");
      // Direction -> Incoming = 0 (False) and Outgoing = 1 (true)
      if ((direction == false) && (isPartyPresent("to",8) == false ))
      {
            return true;
      }
      if ((direction == true) && (isPartyPresent("from",8) == false ))
      {
            return true;
      }
      return false;
}

This allows us to implement a pretty simple handler to detect the inversion and correct it.

function invertedPartyHandler()
{          
      if (detectPartyInversion())
      {
            var recipient = getAttribute("from");
            var sender = getAttribute("to");
            var recipientValues = getAttributeValue("from");
            var senderValues = getAttributeValue("to");
            sender.setValue(recipientValues);
            recipient.setValue(senderValues);
      }
}

Finally, we can run this check onLoad.

function phoneCallOnLoad()
{
      if (getFormType() == 1)
      {
            invertedPartyHandler();
      }
}

All that’s left to do is to add the phoneCallOnLoad() function to the onLoad event of your form.