locked
changing lookup field value based on statecode RRS feed

  • Question

  • Hi,

    I am trying to change a lookup field value based on statecode of the activity.

    I've written this script but it is throwing undefined error on length.

    What could be the reason?

    function updatetaskip(){
    debugger;
    var taskStatus=Xrm.Page.data.entity.attributes.get("statecode").getValue();
    var guidTask=Xrm.Page.data.entity.getId();
     if (taskStatus != null && taskStatus.length > 0)
      {
       var taskstatecode = taskStatus[0].name;
      }
      if(taskstatecode=="Canceled")
      {
      var lookupValue=new Array();
    				lookupValue[0]=new Object();
    				lookupValue[0].id=guidTask.Id;
    				lookupValue[0].name = "8.0 Tech Work Complete";
                    lookupValue[0].entityType = "task";
                    Xrm.Page.getAttribute("new_taskissuephase").setValue(lookupValue);
      }
      else 
      return;
    }

    Thanks

    Monday, November 10, 2014 6:53 PM

All replies

  • statecode it's not a lookup, it's an optionset, you can check by the value or by the text

    and guidTask already is the Id, don't need to add .Id

    function updatetaskip(){
    debugger;
    var taskstatecode = Xrm.Page.getAttribute("statecode").getText();
    var guidTask = Xrm.Page.data.entity.getId();
      if(taskstatecode=="Canceled")
      {
      	var lookupValue=new Array();
    lookupValue[0]=new Object(); lookupValue[0].id=guidTask; lookupValue[0].name = "8.0 Tech Work Complete"; lookupValue[0].entityType = "task"; Xrm.Page.getAttribute("new_taskissuephase").setValue(lookupValue); } else return; }


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, November 10, 2014 7:07 PM
  • Thank you so much. It worked.

    Nice explanation. But the only thing I am confused is I am changing the lookup field in the record but it is not displaying the change in the subgrid section of another entity.

    The section where record gets displayed shows the old value itself. Am I missing something?

    Thanks


    • Edited by ReignFan Monday, November 10, 2014 9:31 PM
    Monday, November 10, 2014 7:24 PM
  • views needs to be refreshed to show the current value. Remember also that you can't update values when a record is on some state (like a closed quote, and I think also a cancelled task). Another thing to keep in mind is that if the field is read only, you need to set the setSubmitMode to always in order to save the value

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, November 11, 2014 4:54 AM
  • It is actually a subgrid where this record gets created. And when I am updating lookup field value in the record through JS it is not making change in the subgrid.

    But even though I refresh the whole form, I don't see the change in subgrid.

    What does that mean?

    Thanks

    Tuesday, November 11, 2014 1:08 PM