Monday, 20 February 2012

crm 2011 javascript/jscript

var objectlookup = Xrm.Page.getAttribute("new_lookup").getValue()
if(objectlookup){
objectlookup[0].id//entity id
objectlookup[0].type//entity logical name

}
escape(string) //to encode string/url
unescape(string) //to decode string/url

//working with two values optionset
Xrm.Page.getAttribute("new_enable").getValue();
Xrm.Page.getAttribute("new_enable").setValue(false);

Xrm.Page.ui.controls.get(" new_enable ").setDisabled(true);

Xrm.Page.ui.controls.get(" new_enable ").setVisible(false);


To open a browser window in maximized mode

var features = 'left=0,top=0,width=' + (screen.availWidth - 10) + ',height=' + screen.availHeight + ',toolbar=0,resizable=1';
var returnValue = window.open(url, 'mylink', features);


To resize the current browser

 parent.top.window.moveTo(0, 0);
 parent.top.window.resizeTo(screen.availWidth, screen.availHeight);


Xrm.Page.getAttribute("attribute_name").getValue();

Xrm.Page.getAttribute("attribute_name").setValue();

Xrm.Page.data.entity.save( null | "saveandclose" |"saveandnew" )




Cancel save event
event.returnValue = false;

To always save the value of attribute

Xrm.Page.getAttribute("new_attribute").setSubmitMode("always");


Xrm.Page.ui.getFormType();
1 –  Form context is Create
2 –  Form context is Update


LoadForm: function () {
        var Form1Id = "guid of create form";
        var Form2Id = "guid of update form";
        var currentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId();

        var formType = Xrm.Page.ui.getFormType();

        if (formType == 1 && Form1Id !=  currentFormId  ) {

            Xrm.Page.ui.formSelector.items.get(Form1Id).navigate();

        }
        else if (formType == 2 && Form2Id !=  currentFormId  ) {

            Xrm.Page.ui.formSelector.items.get(Form2Id).navigate();

        }
    }

////////////
new Date(year, month, day, hours, minutes, seconds, milliseconds)

new Date(targetdateStr.substring(0, 10).replace(/-/g, '/'));

new Date(todayDateTime.getYear(), todayDateTime.getMonth(), 

todayDateTime.getDay(), 0, 0, 0, 0);
////////////////////
if( date1 < date2){ // do something}

try javascript  at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday

http://gtcrm.wordpress.com/2011/03/16/java-script-referenceupdated-2/

enumeration in javascript

custom.namespace.Enum = {
  Option1: "option1 value",
 Option2: "option2 value"
}

to use enum
if(selectedText == custom.namespace.Enum.Option1){
...
}



LoadForm: function () {
        var formType = Xrm.Page.ui.getFormType();

        if (formType == EntityName.FormType.Create) {

            var attributeLookup = Xrm.Page.getAttribute("new_attribute1").getValue();

            var service = new crmSOAPService(orgUniqueName, server);

            var contract = service.Retrieve(attributeLookup [0].entityType, attributeLookup [0].id, ['customerid']);

            if (contract.attributes.customerid.type == "account") {
                var customer = contract.attributes.customerid;
                Xrm.Page.getAttribute("new_attribute2").setValue([{ id: customer.value, name: customer.name, entityType: customer.type}]);
                Xrm.Page.getAttribute("new_attribute2").setSubmitMode("always");
            }
        }
    }


ReLoadParentForm: function () {

        window.parent.opener.parent.location.reload();
    }

No comments:

Post a Comment