Example 2: Win Type & customized button
1: Window OnLoad: window type: create, edit, readonly form type: editable / disable all 2: Window OnSave: Field Mandatory submit 3: Field OnChange: set the value of the field including drop down list Retrieve Account info 4: btn_Submit : Apply for submit Update Account info
function form_OnLoad ( ) {
var formType= Xrm. Page. ui. getFormType ( ) ;
if ( formType== 1 ) {
Xrm. Page. getAttribute ( "new_applydate" ) . setValue ( new Date ( ) ) ;
} else if ( formType== 2 ) {
Xrm. Page. getControl ( "new_name" ) . setDisabled ( true ) ;
}
var formStatus= Xrm. Page. getAttribute ( "new_formstatus" ) . getValue ( ) ;
if ( formStatus== 2 ) {
form_disabledAll ( ) ;
}
}
function form_disableAll ( ) {
var controls = Xrm. Page. ui. controls. get ( ) ;
for ( var i in controls) {
var control = controls[ i] ;
if ( control. getControlType ( ) != "iframe" && control. getControlType ( ) != "webresource" && control. getControlType ( ) != "subgrid" && control. getDisabled ( ) == false ) {
control. setDisabled ( true ) ;
}
}
}
function form_OnSave ( ) {
Xrm. Page. getAttribute ( "new_creditcode" ) . setSubmitMode ( "always" ) ;
}
function new_account_id_OnChange ( ) {
var new_accountVal= Xrm. Page. getAttribute ( "new_account_id" . getValue ( ) ;
if ( new_accountVal!= null && new_accountVal[ 0 ] != null ) {
var obj= getAccountInfo ( new_accountVal[ 0 ] . id) ;
if ( obj!= null ) {
Xrm. Page. getAttribute ( "new_creditcode" ) . setValue ( obj. new_creditcode) ;
Xrm. Page. getAttribute ( "new_mobilephone" ) . setValue ( obj. new_mobilephone) ;
Xrm. Page. getAttribute ( "new_address" ) . setValue ( obj. new_address) ;
Xrm. Page. getAttribute ( "new_country_id" ) . setValue ( [ { "id" : obj[ "_new_country_id_value" ] , "name" : obj[ "_new_country_id_value@OData.Community.Display.V1.FormattedValue" ] , "entitytype" : "new_country" } ] )
Xrm. Page. getAttribute ( "new_province_id" ) . setValue ( [ { "id" : obj[ "_new_province_id_value" ] , "name" : obj[ "_new_province_id_value@OData.Community.Display.V1.FormattedValue" ] , "entitytype" : "new_province" } ] )
Xrm. Page. getAttribute ( "new_city_id" ) . setValue ( [ { "id" : obj[ "_new_city_id_value" ] , "name" : obj[ "_new_city_id_value@OData.Community.Display.V1.FormattedValue" ] , "entitytype" : "new_city" } ] )
}
else {
Xrm. Page. getAttribute ( "new_creditcode" ) . setValue ( null ) ;
Xrm. Page. getAttribute ( "new_mobilephone" ) . setValue ( null ) ;
Xrm. Page. getAttribute ( "new_address" ) . setValue ( null ) ;
Xrm. Page. getAttribute ( "new_country_id" ) . setValue ( null ) ;
Xrm. Page. getAttribute ( "new_province_id" ) . setValue ( null ) ;
Xrm. Page. getAttribute ( "new_city_id" ) . setValue ( null ) ;
}
}
}
function getAccountInfo ( id) {
id= id. replace ( "{" , "" ) . replace ( "}" , "" ) ;
var webUrl= Xrm. Page. context. getClientUrl ( ) + "/api/data/v9.0/" ;
var req= new XMLHttpRequest ( ) ;
req. open ( "GET" , encodeURI ( webUrl+ "accounts(" + id+ ")?$select=name,new_creditcode,_new_country_id_value,_new_province_id_value,_new_city_id_value,new_mobilephone,new_address" ) , true )
req. setRequestHeader ( "Accept" , "application/json" ) ;
req. setRequestHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
req. setRequestHeader ( "OData-MaxVersion" , "4.0" ) ;
req. setRequestHeader ( "OData-Version" , "4.0" ) ;
req. setRequestHeader ( "Prefer" , "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"" ) ;
req. send ( ) ;
if ( req. status== 200 )
return JSON . parse ( req. responseText) ;
else
throw new Error ( JSON . parse ( this . responseText) . error. message) ;
}