Tag: crm javascript

How to set PartyList in Microsoft Dynamics CRM using Javascript

Let make it a smallest blog possible.

below code i have been using without any issue when it comes to setting a partylist or multi lookup in CRM. In the below code i am retrieving the contact id or the id of entity which is there in the regarding field & setting it on the “From” field.

function setCallFrom() 
{
 var lookup = new Array();
 lookup = Xrm.Page.getAttribute("regardingobjectid").getValue();
 if (lookup != null) {
 var name = lookup[0].name;
 var id = lookup[0].id;
 var entityType = lookup[0].entityType;
 if (entityType == "contact") {

var value = new Array();
 value[0] = new Object();
 value[0].id = id;
 value[0].name = name;
 value[0].entityType = "contact";

Xrm.Page.getAttribute("from").setValue(value);
 }

}
}

With slight modifications you can use the code on any other activity if you need to set partylist.

Call this function on Onload of the record or on onchange of the regarding field.

hope this helps!

Cheers!

Show entity form based on a field value in Microsoft Dynamics CRM

Having multiple forms for an entity is definitely amazing since you can control visibility of fields without having to write business rule or Javascript to hide hundreds of fields based on some value.

Now out of the box you can control CRM forms visibility/preference using security roles. However it is not you would always want to to do; sometimes you might have to control form preferences based on field value.

here is the sample code that i am using – on load of create/update/all forms i am retrieving a value from owner of that record – since on creation only owner gets filled up.  I have create a field “new_form” as a whole number on system user :

1

i have currently have it a value as “2”. lets see how i am going to retrieve this value in my code and set the correct form :

//do a webapi action to retrieve the form value from systemuser.

var formnewvalue = ' ';
function retrieveFromValue() {
 debugger;
 var userid = Xrm.Page.getAttribute("ownerid").getValue();
 var newid = userid[0].id.slice(1, -1);
 var req = new XMLHttpRequest();
 req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + newid + ")?$select=new_form", true);
 req.setRequestHeader("OData-MaxVersion", "4.0");
 req.setRequestHeader("OData-Version", "4.0");
 req.setRequestHeader("Accept", "application/json");
 req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
 req.onreadystatechange = function () {
 if (this.readyState === 4) {
 req.onreadystatechange = null;
 if (this.status === 200) {
 var result = JSON.parse(this.response);
 var new_form = result["new_form"];
 var new_form_formatted = result["new_form@OData.Community.Display.V1.FormattedValue"];
formnewvalue = new_form_formatted;

 showForm();

 } else {
 Xrm.Utility.alertDialog(this.statusText);
}
 }
 };
 req.send();
}
function showForm() {
 debugger;
var lblForm;
var relType = formnewvalue

switch (relType) {
case "1":
lblForm = "Portal Contact";
break;

case "2":
lblForm = "Profile Web Form";
break;

default:
lblForm = "Contact";

}
 //check if the current form is form need to be displayed based on the value
 if (Xrm.Page.ui.formSelector.getCurrentItem().getLabel() != lblForm) {
 var items = Xrm.Page.ui.formSelector.items.get();
 for (var i in items) {
 var item = items[i];
 var itemId = item.getId();
 var itemLabel = item.getLabel()
 if (itemLabel == lblForm) {
 //navigate to the form
 item.navigate();
 } 
 } 
 }
}

if you notice my switch statement you will see i m assigning the name of the forms to the values that i will be setting on the user profile.

Once it gets the form name – it than runs through all the forms for this entity untill it fins the correct form and then navigates to it.

i would say this script is majorly needed for first time or for a new system – since Internet Explorer or Chrome keep the form preference , so once our code the job and opened the required form -next time even if we remove the script -it will opening that form.

Another good is – this script prevents user to switch to other form as whenever  a user try to switch the form – on load event triggers and again check the value from owner id and change it back to the required one.

so since i have assigned value “2” to my user i should see “Profile Web Form” , lets see if this works :

2.jpg

woo , it did work.

point to note is you will have to attach this script to all the forms in the entity and call the function on onload for all forms.

hope this helps!

cheers!

Lock all fields on a form using Javascript- Microsoft Dynamics CRM

Hi Guys,

We all get requirements to lock the whole form to stop people from editing the record.

There are some out of the box options available to achieve this :

  1. Create a business rule : you can easily create a business and use lock field feature however you will have to select all the field on the form one by one – for .e.g if you have 50 fields on the form, you will have to add action for all of them. Moreover another limitation is if a field is removed from the form – you business rule will stop working.
  2. We all know if record is status is set to inactive – form become uneditable but in this scenarios we want to keep the record but uneditable.

Therefore, we come down to our final easy solution to tackle all problems above, the Javascript. Have a look on the below code :

 function disableFormFields()

{

       Xrm.Page.ui.controls.forEach(function (control, index) {

           var controlType = control.getControlType();

           if (controlType != “iframe” && controlType != “webresource” && controlType != “subgrid”)

           {

               control.setDisabled(true);

           }

       });

}

above code takes the control of the form directly rather then of each field & locks all of them. Just call the function on Onload , I have used this on my form and it looks like below:

lockfield

 

Now you can definitely add your own logic into this code for .e.g you put a if condition to check some value based on which you want to lock all fields and another things if you need to leave some fields unlocked you can again you a if condition , check the value and use below

just add the below code to above code :

if (crmfield = abc)

{

Xrm.Page.ui.controls.get(“CrmFieldShcemaname1”).setDisabled(false);

Xrm.Page.ui.controls.get(“CrmFieldShcemaNam2”).setDisabled(false);

Xrm.Page.ui.controls.get(“CrmFieldShcemaNam2”).setDisabled(false);

}

 

hope this helps you!

cheers

how to retrieve lookup value and set it on another lookup field in Dynamics CRM 2016 Javascript

In this blog i am going to show how you can retrieve a lookup value and set it.

for e.g if you need to set one contact which in a regarding field on an activity to another contact field.

use the below simple JavaScript:

function yourFunctionName()

var lookup = new Array();  // create a new array
lookup = Xrm.Page.getAttribute("regardingobjectid").getValue(); // get field value
if (lookup != null) 
{
var name = lookup[0].name; // get the name of the record
var id = lookup[0].id; // get the  id of the record
var entityType = lookup[0].entityType; // get the entitytype
now we have retrieved all information that we needed ; its time we set it 🙂

if (entityType == "contact")
{ // ignore this step if its not a multilookup field
var value = new Array(); //create a new object array
value[0] = new Object();
value[0].id = id; // set ID to ID
value[0].name = name; //set name to name
value[0].entityType = "contact"; //optional
Xrm.Page.getAttribute("from").setValue(value); //set the value.
}
}
}
}

Call this function on Onload of the required form.

hope it helps !!

cheers