CRM2011 – Disable / enable fields, sections, tabs and the whole form

Today the problem is about enable and disable fields on form in CRM. When working with CRM, you often want to enable (set to read/write) or disable (set to read / only) selected fields, sections, tabs and the whole form depending on your business logic.

1) Enable / Disable a field

Xrm.Page.getControl("fieldname").setDisabled(false); 

2) Enable / Disable a Section

	function sectiondisable (sectionname, disablestatus)
	{
	    var ctrlName = Xrm.Page.ui.controls.get();
	    for(var i in ctrlName) {
	         var ctrl = ctrlName[i];
	         var ctrlSection = ctrl.getParent().getName();
	         if (ctrlSection == sectionname) {
	               ctrl.setDisabled(disablestatus);
	        }
	    }
	}  // sectiondisable

3) Enable / Disable a Tab

	function tabdisable (tabname, disablestatus)
	{
	 var tab = Xrm.Page.ui.tabs.get(tabname);
	 if (tab == null) alert("Error: The tab: " + tabname + " is not on the form");
	 else {
	     var tabsections =  tab.sections.get();
	     for (var i in tabsections) {
	         var secname = tabsections[i].getName();
	         sectiondisable(secname, disablestatus);
	     }
	  }
	}   // tabdisable

4) Enable / Disable a Form

	function formdisable(disablestatus)
	{
	    var allAttributes = Xrm.Page.data.entity.attributes.get();
	    for (var i in allAttributes) {
	           var myattribute = Xrm.Page.data.entity.attributes.get(allAttributes[i].getName());
	           var myname = myattribute.getName();          
	           Xrm.Page.getControl(myname).setDisabled(disablestatus); 
	    }
	} // formdisable

5) Disabling all Fields on a Form
Add this code to your web resource:

function doesControlHaveAttribute(control) {
    var controlType = control.getControlType();
    return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
}


function disableFormFields(onOff) {
    Xrm.Page.ui.controls.forEach(function (control, index) {
        if (doesControlHaveAttribute(control)) {
            control.setDisabled(onOff);
        }
    });
}

Call the function:
Generally, you will have another function that is triggered on-load that will determine if the form should be disabled. It may check a picklist value or it may check the current user’s role.

function setupForm(){
    if (Xrm.Page.ui.getFormType() == 2 &&
        Xrm.Page.getAttribute("incidentstagecode").getValue() != null &&
        Xrm.Page.getAttribute("incidentstagecode").getValue() == "200001") 
    {
           disableFormFields(true);
    }
}

- See more at: http://blog.avtex.com/2011/04/01/disabling-all-fields-on-a-form-in-crm-2011/#sthash.nfN8z64a.dpuf

CRM2011 – Associate and Disassociate Many to Many relationship records

In my Solution I needed to associate programmaticaly an N:N relationship in my entity.

If we have any N:N(Many to Many) relationship in Microsoft Dynamics CRM 2011, need to manually assign relationship between two entities using SDK.

In the next example we discuss about the example to associate and disassociate N:N(Many to Many) relationship records in CRM 2011 thru SDK.

In the example I have N:N relationship between Case (Incident) and Activity (task). Relationship name is ls_incident_activity_task.

// Creates the custom many-to-many relationship between the case and task.
public void AssociateRelationship(IOrganizationService service, EntityReference caseRef, EntityReference taskRef)
{
//If one of the ID's is null, do nothing
if (caseRef == null) return;
if (taskRef == null) return;
if (caseRef.LogicalName != "incident") return;
if (taskRef.LogicalName != "task") return;

var caseId = caseRef.Id;
var taskId = taskRef.Id;

//The relationship schema to create
string relationshipName = "ls_incident_activity_task";

//Create a query that will check to see if the relationship already exists between this account and contact
QueryExpression query = new QueryExpression(relationshipName)
{
NoLock = true,
ColumnSet = new ColumnSet(false),//only get the row ID, since we don't need any actual values
Criteria =
{
Filters =
{
new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
//Get the row for the relationship where the account and contact are the account and contact passed in
new ConditionExpression("incidentId", ConditionOperator.Equal, caseId.ToString()),
new ConditionExpression("activityId", ConditionOperator.Equal, taskId.ToString()),
},
},
}
}
};
var result = service.RetrieveMultiple(query);
//Check if the relationship was not found
if (result == null || result.Entities == null || result.Entities.Count < 1)
{
//The relationship was not found, so create it
service.Associate(caseRef.LogicalName, caseRef.Id, new Relationship(relationshipName), new EntityReferenceCollection() { caseRef });
}
}


public void DisassociateRelationship(IOrganizationService service, EntityReference caseRef, EntityReference taskRef)
 {
     //If one of the ID's is null, do nothing
     if (caseRef == null) return;
     if (taskRef == null) return;
     if (caseRef.LogicalName != "incident") return;
     if (taskRef.LogicalName != "task") return;
 
     var caseId = caseRef.Id;
     var taskId = taskRef.Id;
 
     //The relationship schema to create
     string relationshipName = "ls_incident_activity_task";
 
     //Create a query that will check to see if the relationship already exists between this account and contact
     QueryExpression query = new QueryExpression(relationshipName)
     {
         NoLock = true,
         ColumnSet = new ColumnSet(false),//only get the row ID, since we don't need any actual values
         Criteria =
         {
             Filters =
             {
                 new FilterExpression
                 {
                     FilterOperator = LogicalOperator.And,
                     Conditions =
                     {
                         //Get the row for the relationship where the account and contact are the account
                         new ConditionExpression("incidentId", ConditionOperator.Equal, caseId.ToString()),
                         new ConditionExpression("activityId", ConditionOperator.Equal, taskId.ToString()),
                     },
                 },
             }
         }
     };
     var result = service.RetrieveMultiple(query);
     //check if record exists
     if (result != null && result.Entities != null && result.Entities.Count > 0)
     {
         //Delete the N:N relation
         service.Disassociate(caseRef.LogicalName, caseRef.Id, new Relationship(relationshipName), new EntityReferenceCollection() { taskId });
     }
 }

MobileDev: panoramica sullo sviluppo Mobile e sulle possibili alternative .Net

FocusDayCiao a tutti,

volevo pubblicizzare il Focus Day di Overnet che si terrà a settembre. In particolare l’evento “MobileDev: panoramica sullo sviluppo Mobile e sulle possibili alternative .Net“. In questo evento di circa 4 ore discuteremo sul mondo Mobile (I mercati, i device ed inoltre vedremo una valida alternativa per riutilizzare codice .Net: XAMARIN). Naturalmente il relatore saro’ io.. Spero possa interessare e spero di vedervi numerosi. Di seguito il link dell’evento: http://overneteducation.it/DettaglioCorso.aspx?corso=EV030&v=1

 

L’evento si terrà a Milano. L’indirizzo è il seguente:

OverNet Education
Strada 4 Palazzo Q7, 1°piano
Centro Direzionale Milanofiori
20089 – Rozzano MI

 

CRM2011 – Default Regarding Lookup to PhoneCall in Activities Entity

Today I need to change the default View in my Regarding Lookup in the Task Entity.
I used the following code to set the Regarding Lookup to Phone Call and set the default view.

document.getElementById("regardingobjectid").setAttribute("defaulttype", "4210");
Xrm.Page.getControl("regardingobjectid").setDefaultView("a2d689d1-45a3-4c29-aswsd-802327e67a0d");

With the first line we set the default entity type to Phone Call for the Regarding Lookup dialog. In the second line we set the Regarding Lookup dialog’s view to the ‘Phone Call Lookup View’ using its guid value.

If you do not use this line of code it will default to the ‘My Active Phone Call View’ or whatever you have selected as the default Phone Call view.