none
Default Phonecall "To" with javascript onload - CRM2011

    Question

  • New to javascript to forgive ignorance...

    Using CRM 2011 Online, I have a custom entity - Projects.  On the custom entity, there is a contact lookup field.  When a user selects Add..Phone Call from the ribbon of a Project, I want to default the Phone Call "To" field to the Project.Contact.  I just can't figure out how to set an ActivityParty entity field in javascipt.  My code is retrieving the contact entityreference and I can display as so:

        if (window.parent.opener.Xrm.Page.data.entity.getEntityName() == "bc_project") {
          var lookupValue = new Array;
          lookupValue = window.parent.opener.Xrm.Page.data.entity.attributes.get("bc_contact").getValue();
    
          alert(lookupValue[0].id)
          alert(lookupValue[0].name);
          alert(lookupValue[0].entityType);
    
    

    I want to do something like the following but of course this doesn't work as "to" is an ActivityParty.

          Xrm.Page.getAttribute("to").setValue(lookupValue);
          Xrm.Page.getAttribute("to").setSubmitMode("always");
     
    
    I have seen code that define the ActivityParty in javascript like below but OnLoad, I don't have an ActivityId to use.  I thought if I could get this to work, I would set "to" to "ap" as below but stuck.

          var ap = {
            ActivityId: {
              Id: ????,
              LogicalName: "phonecall"
            },
            PartyId: {
              Id: lookupValue[0].id, 
              LogicalName: "contact"
            },
            ParticipationTypeMask: {
              Value: 1
            }
          };
    
    


    Been "Bing-ing" all morning but can't figure out how to do this.  Is this possible?  ActivityParty's seem to be very painful to me!

     

     

     

    Saturday, August 20, 2011 11:42 AM

Answers

  • When working with activity parties on activities that have not yet been created, it is not necessary to declare an Activity ID for them.  By loading Activity Party references into the proper field, the system will automatically handle the creation and association of the secondary records.  That's from a C# standpoint, however.  In JavaScript, you're accessing these values sans the intermediary "activity party" record.  Notice that when you click on a record in the Lookup, it does not open an Activity Party form, but the form for the associated record directly.  This is because these fields present--as a special case--a direct association to the party records.

    There is no need or way to interface with the Activity Party record from that field.  For all intents and purposes, your code should work--at least, as far as I understand it.  It should be possible to assign, directly, a Lookup "object" representing a valid type (Account, Contact, User, Queue) into the array of the "To" field.  When I picked this field apart in CRM 4, that's how I found it to work.  In fact, in order to disassemble the values of the field and push them into the activity by JavaScript, I had to translate them into Activity Party objects... much like the code you have discovered.  This is because the form makes a special interpretation on these fields when it is submitted the traditional way.

    In short, I don't believe you're crazy.  The way you tried to do it should work.  What I'd worry about is what is causing it to fail.  On the surface, I can't see anything wrong.  But don't let that fool you.  Try this code to see if the performance is different:

    var remoteLookupValue = window.parent.opener.Xrm.Page.data.entity.attributes.get("bc_contact").getValue();
    
    var newLookupValue = [{id: remoteLookupValue[0].id, name: remoteLookupValue[0].name, entityType:remoteLookupValue[0].entityType}];
    
    Xrm.Page.getAttribute("to").setValue(newLookupValue);
    Xrm.Page.getAttribute("to").setSubmitMode("always");
    


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    • Marked as answer by Janet T Tuesday, August 23, 2011 5:00 PM
    Sunday, August 21, 2011 8:15 AM
    Moderator

All replies

  • When working with activity parties on activities that have not yet been created, it is not necessary to declare an Activity ID for them.  By loading Activity Party references into the proper field, the system will automatically handle the creation and association of the secondary records.  That's from a C# standpoint, however.  In JavaScript, you're accessing these values sans the intermediary "activity party" record.  Notice that when you click on a record in the Lookup, it does not open an Activity Party form, but the form for the associated record directly.  This is because these fields present--as a special case--a direct association to the party records.

    There is no need or way to interface with the Activity Party record from that field.  For all intents and purposes, your code should work--at least, as far as I understand it.  It should be possible to assign, directly, a Lookup "object" representing a valid type (Account, Contact, User, Queue) into the array of the "To" field.  When I picked this field apart in CRM 4, that's how I found it to work.  In fact, in order to disassemble the values of the field and push them into the activity by JavaScript, I had to translate them into Activity Party objects... much like the code you have discovered.  This is because the form makes a special interpretation on these fields when it is submitted the traditional way.

    In short, I don't believe you're crazy.  The way you tried to do it should work.  What I'd worry about is what is causing it to fail.  On the surface, I can't see anything wrong.  But don't let that fool you.  Try this code to see if the performance is different:

    var remoteLookupValue = window.parent.opener.Xrm.Page.data.entity.attributes.get("bc_contact").getValue();
    
    var newLookupValue = [{id: remoteLookupValue[0].id, name: remoteLookupValue[0].name, entityType:remoteLookupValue[0].entityType}];
    
    Xrm.Page.getAttribute("to").setValue(newLookupValue);
    Xrm.Page.getAttribute("to").setSubmitMode("always");
    


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    • Marked as answer by Janet T Tuesday, August 23, 2011 5:00 PM
    Sunday, August 21, 2011 8:15 AM
    Moderator
  • Thanks Dave - I have no idea but your code works and mine doesn't!  I'm sure there is a reason why but at the moment I'm just happy it is working!  with my code, the "To" field shows this strange icon and fails if I try to save. 

    Thanks for your help and one day I will figure out what I'm doing!

    Tuesday, August 23, 2011 5:00 PM
  • Here's what I know:  The "DataValue" construct from Lookups in CRM 4 can have additional, undocumented "extra" data.  I presume that CRM 2011 may have added additional undocumented data elements to possibly even the most basic Lookups.  If that's the case, then based on the behavior I've seen the "extra" data is established after a value is assigned, and covertly present in the "DataValue" object when referenced from alternate scripts.  The presence of this "extra" data may in fact thwart or otherwise break the value assignment to some other "DataValue" member.  Therefore, creating a clean object with strictly those data points necessary for assignment would be inexorably required, and in fact likely why my code works and yours does not.  I intend to perform some follow-up research on this and blog about my findings later on.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    Tuesday, August 23, 2011 11:03 PM
    Moderator
  • As promised, I've written this up on my blog with a neat function that should help take the guesswork out of copying Lookup values in the future.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    Thursday, August 25, 2011 7:59 PM
    Moderator
  • I'm getting the same error, and unfortunatey I can't use Dave Berry's work around because I'm not just copying a lookup.

    The bug goes away if you disable the Automatic Resolutions on the Party List field.  But in my case that's the To field of the email entity and it would be dumb to have to turn that off.

    I'm making a support case with Microsoft...

    Thursday, July 19, 2012 10:23 AM
  • Turns out the bug I had goes away when you turn off Compression on your IIS Server.  :(

    oh well...  At least there's a way around it.

    Thursday, August 09, 2012 4:14 AM
  • Hey guys,

    I've got a related question. I've had a look at your code. I've got an opportunity and related to it a custom Entity. On to onload event of the custom Entity i'm trying to get and set the Customer Lookup value from the Opportunity and set it in the appropriate lookup field on the custom entity with the following code:

                                                                      

    function setfield()
    {

    if ( Xrm.Page.ui.getFormType()==1)
    {
          //Get lookup value 
          var remoteLookupValue = window.parent.opener.Xrm.Page.data.entity.attributes.get("customerid").getValue();

        if (remoteLookupValue != null) 
        {  
           alert("remoteLookupValue is not null ");
           alert(remoteLookupValue[0].id);
           alert(remoteLookupValue[0].name);
           alert(remoteLookupValue[0].entityType);       
           var newLookupValue = [{id: remoteLookupValue[0].id, name: remoteLookupValue[0].name, entityType:remoteLookupValue[0].entityType}];

        Xrm.Page.getAttribute("Car_AccountId").setValue(newLookupValue);
        Xrm.Page.getAttribute("Car_AccountId").setSubmitMode("always");
        }
      }

    The thing is it keep on giving an error telling me that: Cannot get the value of the entity: The object is null or it isn't defined. The thing is it probably isn't a big thing but I fail to see it. Any ideas? Any help would be appreciated.

    Tuesday, September 18, 2012 8:44 PM