CRM 2011 Field,Form and Controls

0 comments


CRM Enity Properties

 Xrm.Page.data.entity.getEntityName()   //Entity Name
 Xrm.Page.data.entity.getId()                  //Record GUID
 Xrm.Page.ui.getFormType()                  //CRM Form Type (Integer)

**Create (1), Update (2), Read Only (3), Disabled (4), Bulk Edit (6)

– Get & Set Text Field—

Xrm.Page.data.entity.attributes.get(“fld_name”).getValue()
Xrm.Page.data.entity.attributes.get(“fld_name”).setValue(“First”)

–Get Lookup–

Xrm.Page.data.entity.attributes.get(“primarycontactid”)
Xrm.Page.data.entity.attributes.get(“primarycontactid”).getValue()[0].id
Xrm.Page.data.entity.attributes.get(“primarycontactid”).getValue()[0].name

–Set Lookup–

var lookUpValue = new Array();
lookUpValue[0] = new Object();
lookUpValue[0].id = idValue;
lookUpValue[0].name = textValue;
lookUpValue[0].entityType = ‘{Entity_Name}’;
Xrm.Page.getAttribute(“primarycontactid”).setValue(lookUpValue);

–Option Set Properties–

Xrm.Page.data.entity.attributes.get(“new_country”)
Xrm.Page.data.entity.attributes.get(“new_country”).getOptions()    // All Options
Xrm.Page.data.entity.attributes.get(“new_country”).getText()         // Option Label value
Xrm.Page.data.entity.attributes.get(“new_country”).getValue()       // Selected option value

–Hide/Show controls–

Xrm.Page.ui.controls.get(“name”).setVisible(true/false)

–Disable/Enable controls–

Xrm.Page.ui.controls.get(“name”).setDisabled(true/false)

–Set Focus–

Xrm.Page.ui.controls.get(“name”).setFocus();

–Set field’s Requirement Level–

Xrm.Page.getAttribute(“name”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“name”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“name”).setRequiredLevel(“recommended”);

–To Set the label for the control–

Xrm.Page.ui.controls.get(“name”).setLabel(“Label Text”);

–To set URL for IFrams/WebResource–

Xrm.Page.ui.controls.get(“IFrame/WebResource Name”).setSrc(“URL”);

– Get Parent of current control –

Xrm.Page.ui.controls.get(“name”).getParent();

– “Current User” & “Organization” details–

Xrm.Page.context.getUserId()                     //Current User ID
Xrm.Page.context.getOrgUniqueName()     //Organization Name
Xrm.Page.context.getServerUrl()                //Server URL
Xrm.Page.context.getUserRoles()               //Role ID’s of current user

– Check isDirty

Xrm.Page.data.entity.getIsDirty()  // Form is IsDirty
Xrm.Page.data.entity.attributes.get(“new_name”).getIsDirty()  //Field is IsDirty

–Save Form

function save(){Xrm.Page.data.entity.save();}

–Save&close Form

function saveandclose(){Xrm.Page.data.entity.save(“saveandclose”);}

–Close Form

function close(){Xrm.Page.ui.close();}


//To hide “New” button

crmForm.all.attr_name.AddParam(“ShowNewButton”, 0);

//To disable “View Selection” dropdown

document.getElementById(“{attr_name}”).setAttribute(“disableViewPicker”, “1″);

//To hide “Search” textbox

document.getElementById(“{attr_name}”).setAttribute(“disableQuickFind”, “1″)

Expand/Collapse Tabs–

var myTab = Xrm.Page.ui.tabs.get(“{tab_name}”);
myTab.setDisplayState(“expanded”); // To expand tab
myTab.setDisplayState(“collapsed”); // To collapse tab


CRM 2011 Validation On Field Length/ Max Length

0 comments


Scenario:

Validation needed when certain field exceed amount of character or length.
This is what i coded for the field validation using javascript.

Code:


// Validate Length for Field's eg. ValidateFieldLength(field, 3)
function ValidateFieldLength(attributeName, MaxLength) {

    var MaxLength = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    //Convertion to String
    var StringMax = MaxLength.toString();
    //Convertion to Int
    var IntMax = parseInt(StringMax.length);

    if (IntMax > MaxLength) {
        alert("Exceeded the maximum length");
        //Set Value Null when exceeded
        Xrm.Page.data.entity.attributes.get(attributeName).setValue(null); 
    }
}

Validation will prompt and the field value will be clear.

CRM 2011 Update/ Custom Subgrid View/ FetchXML Subgrid

0 comments


Scenario: 

I need to filter the subgrid to act as view.
Decided to create a custom subgrid view to filter based on all the records in the subgrid.
This is what i code for javascript and put it on onload form function.

Code:


function updateSubGrid() {

    var relatedProducts = document.getElementById("SubgridProjects");
    var lookupfield = new Array;
    lookupfield = Xrm.Page.getAttribute("vw_projectnoid").getValue();

    if (lookupfield != null) { var lookupid = lookupfield[0].id; }
    else { return; }

    if (relatedProducts == null || relatedProducts.readyState != "complete") { setTimeout('updateSubGrid()', 2000); return; }

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXml += "<entity name='vw_project'>";
    fetchXml += "<attribute name='vw_projectid' />";
    fetchXml += "<attribute name='vw_projectno' />";
    fetchXml += "<attribute name='vw_titlestatus' />";
    fetchXml += "<attribute name='vw_landtenure' />";
    fetchXml += "<order attribute='vw_projectno' descending='false' />";
    fetchXml += "<filter type='and'>";
    fetchXml += "<condition attribute='vw_projectid' operator='eq' value='" + lookupid + "' />";
    fetchXml += "</filter>";
    fetchXml += "</entity>";
    fetchXml += "</fetch>";

    relatedProducts.control.setParameter("fetchXml", fetchXml);
    relatedProducts.control.refresh();
    document.getElementById("SubgridProjects_span").disabled = true; //Disable the subgrid
}   

As u can c there no layout xml. Most of the custom view use fetch xml.  U can download ur fetch xml from advanced find.