locked
CRM 2015 JavaScript unable to set PartyList field RRS feed

  • Question

  • I have some JS code on a phone call - On Change of the "Direction" field.

    I am trying to swap the values in the "to" and "from" fields when the "direction" field is changed.

    When I place alerts in my code I can see that the fields are changing as I expect but they are not changing visually on the form and when I change the direction field again it is seeing the original values no matter how often I change the field.

    Have I made a noddy mistake or am I missing something about the way the party list fields are set?

    Here is my code (still a bit of work to do on NULL handling) so am assuming both fields are populated at the moment:

    function directionChange_Onchange() {
        if (Xrm.Page.data.entity.getEntityName() === 'phonecall') {
            SwapLookups(Xrm.Page.getAttribute('to'), Xrm.Page.getAttribute('from'));
        }
    }
    
    function SwapLookups(to, from) {
    
        //START of debugging ALERT code
    
        var originalTo = null;
        var originalFrom = null;
    
        if (to.getValue() != null)
            originalTo = to.getValue()[0].name;
    
        if (from.getValue() != null)
            var originalFrom = from.getValue()[0].name;
    
        //END of debugging ALERT code
    
        // Copy TO into FROM
        var newFrom = null;
        if (to.getValue() != null) {
            newFrom = new Array();
            newFrom[0] = new Object();
            newFrom[0].id = to.getValue()[0].id;
            newFrom[0].name = to.getValue()[0].name;
            newFrom[0].entityType = to.getValue()[0].entityType;
        }
    
        // Copy FROM into TO
        var newTo = null;
        if (from.getValue() != null) {
            newTo = new Array();
            newTo[0] = new Object();
            newTo[0].id = from.getValue()[0].id;
            newTo[0].name = from.getValue()[0].name;
            newTo[0].entityType = from.getValue()[0].entityType;
        }
    
        to.setValue(newTo);
        from.setValue(newFrom);
    
        //START of debugging ALERT code
    
        alert(originalTo + " --> " + to.getValue()[0].name);
        alert(originalFrom + " --> " + from.getValue()[0].name);
    
        //END of debugging ALERT code
    }
    

    Thursday, October 1, 2015 10:49 AM

Answers

  • What an idiot I am.

    This was an upgrade from 2011 where the code WAS necessary.  However in 2015, the "to" and "from" fields swap on their own when the direction is toggled.  No code required.

    Because this is an upgrade and because I was being dense, I failed to realise I was double swapping the fields with the code in place.

    Doh!

    Monday, October 5, 2015 11:35 AM

All replies

  • hi,

    you can try with this function?

    function SwapLookups(to, from) { var toValue = to.getValue();

    var fromValue =from.getValue();

    to.setValue(fromValue);

    from.setValue(toValue); }


    My blog: www.crmanswers.net - CRM Theme Generator

    Thursday, October 1, 2015 11:17 AM
  • I did start off with such a function and ended up going more complicated because it wasn't working for me.

    Tried your sample and still no joy for me.  It makes me wonder if the issue is something else in the page or another piece of JavaScript which is blocking the change being reflected visually on the form.

    It is strange that I can interrogate the getValue after the change and see the new value is there but the form field is unchanged and subsequent executions continue to see the original value.

    Thursday, October 1, 2015 11:27 AM
  • uhm, let's try another approach, instead passing the xrm object to the functions, just pass the field names

    function directionChange_Onchange() {
        if (Xrm.Page.data.entity.getEntityName() === 'phonecall') {
            SwapLookups('to', 'from');
        }
    }

    function SwapLookups(to, from) { var toValue =Xrm.Page.getAttribute(to).getValue();

    var fromValue =Xrm.Page.getAttribute(from).getValue();

    Xrm.Page.getAttribute(to).setValue(fromValue);

    Xrm.Page.getAttribute(from).setValue(toValue); }


    My blog: www.crmanswers.net - CRM Theme Generator

    Thursday, October 1, 2015 11:30 AM
  • Tried that with the same result.

    My alerts show that the items have swapped but the form fields and the subsequent executions do not reflect the swap.

    Going to scan the full JS file to see if there is anything which might cause this behaviour and comment it out.

    Thursday, October 1, 2015 12:09 PM
  • What an idiot I am.

    This was an upgrade from 2011 where the code WAS necessary.  However in 2015, the "to" and "from" fields swap on their own when the direction is toggled.  No code required.

    Because this is an upgrade and because I was being dense, I failed to realise I was double swapping the fields with the code in place.

    Doh!

    Monday, October 5, 2015 11:35 AM
  • Hi Gordon,

    thanks for sharing this behavior change that happened in CRM 2015.


    My blog: www.crmanswers.net - CRM Theme Generator

    Monday, October 5, 2015 1:45 PM