Last active
July 18, 2016 19:52
-
-
Save ivan-marquez/14d5614efedf5c1eb7dd207b3481caf6 to your computer and use it in GitHub Desktop.
Namespace example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#region "namespace fwpr.controls" | |
(function () { | |
this.fwpr = this.fwpr || {}; | |
this.fwpr.controls = this.fwpr.controls || {}; | |
var ns = this.fwpr.controls; | |
ns.getControl = function (fieldName) { | |
try { | |
return Xrm.Page.ui.controls.get(fieldName); | |
} | |
catch (error) { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + fieldName); | |
} | |
}; | |
ns.getTab = function (tabName) { | |
try { | |
return Xrm.Page.ui.tabs.get(tabName); | |
} | |
catch (error) { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + tabName); | |
} | |
}; | |
ns.getSection = function (tabName, sectionName) { | |
var tab = ns.getTab(tabName); | |
return tab.sections.get(sectionName); | |
}; | |
ns.setVisibility = function (fieldName, visibility) { | |
var ctrl = ns.getControl(fieldName); | |
if (ctrl) { | |
ctrl.setVisible(visibility); | |
} | |
else { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + fieldName); | |
} | |
}; | |
ns.setTabVisibility = function (tabName, visibility) { | |
var ctrl = ns.getTab(tabName); | |
if (ctrl) { | |
ctrl.setVisible(visibility); | |
}; | |
}; | |
ns.setSectionVisibility = function (tabName, sectionName, visibility) { | |
var section = ns.getSection(tab, sectionName); | |
if (section) { | |
section.setVisible(visibility); | |
} | |
}; | |
ns.setDisabled = function (fieldName, disabled) { | |
var ctrl = ns.getControl(fieldName); | |
if (ctrl) { | |
ctrl.setDisabled(disabled); | |
} | |
else { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + fieldName); | |
} | |
}; | |
})(); | |
// #endregion | |
//#region "namespace fwpr.fields" | |
(function () { | |
this.fwpr = this.fwpr || {}; | |
this.fwpr.fields = this.fwpr.fields || {}; | |
var ns = this.fwpr.fields; | |
ns.getValue = function (fieldName) { | |
try { | |
return Xrm.Page.getAttribute(fieldName).getValue(); | |
} | |
catch (error) { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + fieldName); | |
} | |
}; | |
ns.setValue = function (fieldName, value) { | |
try { | |
return Xrm.Page.getAttribute(fieldName).setValue(value); | |
} | |
catch (error) { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + fieldName); | |
} | |
}; | |
ns.setSubmitMode = function (fieldName) { | |
try { | |
return Xrm.Page.getAttribute(fieldName).setSubmitMode("always"); | |
} | |
catch (error) { | |
console.log('Something went wrong: ' + error); | |
console.log('Description: Unable to find attribute with id: ' + fieldName); | |
} | |
}; | |
ns.addOnChange = function (fieldName, functionName) { | |
return Xrm.Page.data.entity.attributes.get(fieldName).addOnChange(functionName); | |
}; | |
ns.addOnSave = function (functionName) { | |
return Xrm.Page.data.entity.addOnSave(functionName); | |
}; | |
}()); | |
//#endregion | |
//#region "namespace fwpr.helpers" | |
(function () { | |
this.fwpr = this.fwpr || {}; | |
this.fwpr.helpers = this.fwpr.helpers || {}; | |
var ns = this.fwpr.helpers; | |
ns.showWindow = function (resourceName) { | |
if (typeof Custom == "undefined") { | |
Custom = { | |
OpenDialog: function (webresource) { | |
var $v_0 = new Mscrm.CrmDialog(Mscrm.CrmUri.create(webresource), window, 380, 240); | |
$v_0.show(); | |
}, | |
__namespace: true | |
} | |
} | |
Custom.OpenDialog(resourceName); | |
} | |
ns.currentTime = function () { | |
return new Date(); | |
}; | |
ns.formState = function () { | |
return Xrm.Page.ui.getFormType(); | |
}; | |
ns.getCurrentId = function () { | |
return Xrm.Page.data.entity.getId(); | |
}; | |
ns.associateRecords = function (entity1Id, entity1, entity2Id, entity2, relationshipName) { | |
XrmServiceToolkit.Rest.Associate(entity1Id, entity1, entity2Id, entity2, | |
relationshipName, function () { | |
//console.log("Record associated"); | |
}, | |
function (error) { | |
console.log(error.message); | |
}, true) | |
}; | |
ns.validatePhone = function (context) { | |
var oField = context.getEventSource().getValue() | |
var field = context.getEventSource().getName() | |
var sAllNumeric = oField | |
if (typeof (oField) != "undefined" && oField != null | "") { | |
sAllNumeric = oField.replace(/[^0-9]/g, "") | |
switch (sAllNumeric.length) { | |
case 10: | |
sValidatePhone = "(" + sAllNumeric.substr(0, 3) + ") " + sAllNumeric.substr(3, 3) + "-" + sAllNumeric.substr(6, 4) | |
Xrm.Page.getControl(field).clearNotification() | |
AlertRequired = false | |
context.getEventSource().setValue(sValidatePhone) | |
break | |
case 11: | |
sValidatePhone = sAllNumeric.substr(0, 1) + " (" + sAllNumeric.substr(1, 3) + ") " + sAllNumeric.substr(4, 3) + "-" + sAllNumeric.substr(7, 4) | |
Xrm.Page.getControl(field).clearNotification() | |
AlertRequired = false | |
context.getEventSource().setValue(sValidatePhone) | |
break | |
default: | |
//alert("Please verify phone.") | |
context.getEventSource().setValue("") | |
Xrm.Page.getControl(field).setNotification("Format: (123) 456-7890") | |
Xrm.Page.ui.controls.get(field).setFocus(true) | |
AlertRequired = true | |
break | |
} | |
} | |
}; | |
ns.validateEmail = function (context) { | |
var oField = context.getEventSource().getValue(); | |
var field = context.getEventSource().getName(); | |
var emailPat = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; | |
var matchArray = field.match(emailPat); | |
if (matchArray == null) { | |
Xrm.Page.getControl(field).setNotification("You must enter a valid email address."); | |
Xrm.Page.ui.controls.get(field).setFocus(true); | |
AlertRequired = true; | |
return false; | |
} else { | |
Xrm.Page.getControl(field).clearNotification(); | |
AlertRequired = false; | |
} | |
}; | |
ns.validateSSN = function (context) { | |
var oField = context.getEventSource().getValue(); | |
var field = context.getEventSource().getName(); | |
var sAllNumeric = oField; | |
if (typeof (oField) != "undefined" && oField != null) { | |
sAllNumeric = oField.replace(/[^0-9]/g, ""); | |
switch (sAllNumeric.length) { | |
case 9: | |
sValidateSSN = sAllNumeric.substr(0, 3) + "-" + sAllNumeric.substr(3, 2) + "-" + sAllNumeric.substr(5, 4); | |
Xrm.Page.getControl(field).clearNotification(); | |
AlertRequired = false; | |
context.getEventSource().setValue(sValidateSSN); | |
break; | |
default: | |
/*alert("Please verify Social Security Number."); | |
context.getEventSource().setValue("");*/ | |
Xrm.Page.getControl(field).setNotification("Format: 123-45-6789"); | |
Xrm.Page.ui.controls.get(field).setFocus(true); | |
AlertRequired = true; | |
break; | |
} | |
} | |
}; | |
ns.enableConvertButton = function () { | |
if (ns.formState() != 2) { | |
return false; | |
} | |
}; | |
ns.copyAddress = function (street1, street2, city, state, zip) { | |
var mailAddress = { | |
mailStreet1: street1, | |
mailStreet2: street2, | |
mailCity: city, | |
mailState: state, | |
mailZip: zip | |
}; | |
return mailAddress; | |
}; | |
ns.formatDate = function (date) { | |
return new Date(parseInt(date.replace("/Date(", "").replace(")/", ""), 10)); | |
}; | |
var setStateResponse = function (req, successCallback, errorCallback) { | |
if (req.readyState == 4) { | |
if (req.status == 200) { | |
if (successCallback != null) { | |
successCallback() | |
} | |
} | |
else { | |
errorCallback(SDK.SAMPLES._getError(req.responseXML)) | |
} | |
} | |
} | |
var _getServerUrl = function () { | |
var OrgServicePath = "/XRMServices/2011/Organization.svc/web" | |
var serverUrl = "" | |
if (typeof GetGlobalContext == "function") { | |
var context = GetGlobalContext() | |
//context.getServerUrl() | |
serverUrl = Xrm.Page.context.prependOrgName("/XRMServices/2011/Organization.svc/web") | |
} | |
else { | |
if (typeof Xrm.Page.context == "object") { | |
serverUrl = Xrm.Page.context.prependOrgName("/XRMServices/2011/Organization.svc/web") | |
} | |
else { throw new Error("Unable to access the server URL") } | |
} | |
if (serverUrl.match(/\/$/)) { | |
serverUrl = serverUrl.substring(0, serverUrl.length - 1) | |
} | |
//+OrgServicePath | |
return serverUrl | |
}; | |
})(); | |
//#endregion | |
//#region "namespace fwpr.constants" | |
(function () { | |
this.fwpr = this.fwpr || {}; | |
this.fwpr.constants = this.fwpr.constants || {}; | |
var ns = this.fwpr.constants; | |
ns.devUrl = function () { | |
return "http://crm-web-srv.cloudapp.net:9543/"; | |
}; | |
ns.ODataPath = function () { | |
return Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc"); | |
}; | |
ns.transactionCurrencyId = { | |
Id: "{c4449cf5-e006-e411-80be-000d3a100c9d}" | |
} | |
ns.contractTemplateId = { | |
devId: "{57564d02-0d69-e411-80d3-000d3a100c9d}", | |
prodId: "57564d02-0d69-e411-80d3-000d3a100c9d" | |
} | |
})(); | |
//#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment