I had to add a new button on email form that when clicked will set check a custom checkbox on email form and send the email. I have some extra checks based on email status.
The function that I use looks like this:
function SendEmail() {
var formType = Xrm.Page.ui.getFormType();
var emailStatus = Xrm.Page.getAttribute("statecode").getValue();
var emailDirection = Xrm.Page.getAttribute("directioncode").getValue();
//Update mode && Status= Open (Draft or failed)
if (formType == 1 || (formType == 2 && emailStatus == "Open")) {
//outgoing
if (emailDirection == "1") {
// Mark the "Is Completed" field in the email
var completed = Xrm.Page.getAttribute("new_iscompleted");
if (completed != null) {
completed.setValue(true);
if (typeof send == 'function') {
//send email
send();
}
}
}
else {
alert('Action allowed only for "Outgoing" emails');
}
}
else {
alert("Action not supported for this email status");
}
}
Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts
Tuesday, March 26, 2013
CRM 2011 Set focus on email body javascript
I had an request that after email form is loaded I needed to set the focus on body field and not on Subject field like crm is doing.
Note that this is an unsupported customization so it might not work for you. You'll need jquery to be included (I used jquery-1.8.3.min).
function SetFocusOnMailBody() {
var ifrm = document.getElementById("descriptionIFrame");
ifrm.focus();
var contentbody = $(ifrm).contents().find("body");
var char = 0, sel; // character at which to place caret
contentbody.focus();
if (document.selection) {
sel = document.selection.createRange();
sel.moveStart('character', char);
sel.select();
}
}
I called this method after I was setting some fields in Email form at Onload.
The only way I could make it work was to put a timeout (because I could't figure out from where the focus is automatically set on subject field).
setTimeout(function () {
//set focus on mail body
SetFocusOnMailBody();
}, 100);
Hope this helps.
Note that this is an unsupported customization so it might not work for you. You'll need jquery to be included (I used jquery-1.8.3.min).
function SetFocusOnMailBody() {
var ifrm = document.getElementById("descriptionIFrame");
ifrm.focus();
var contentbody = $(ifrm).contents().find("body");
var char = 0, sel; // character at which to place caret
contentbody.focus();
if (document.selection) {
sel = document.selection.createRange();
sel.moveStart('character', char);
sel.select();
}
}
I called this method after I was setting some fields in Email form at Onload.
The only way I could make it work was to put a timeout (because I could't figure out from where the focus is automatically set on subject field).
setTimeout(function () {
//set focus on mail body
SetFocusOnMailBody();
}, 100);
Hope this helps.
CRM 2011 Show/Hide sections based on a specific optionset value - javascript
My request was that based on a optionset value I had to show only a specific section from a tab and hide the rest. Each optionset value had a specific section and the section label started with this optionset value. So my condition to show the section was - section name starts with "optionset value".
function ShowHideSectionsBySubject() {
var tabName = "tab_Name";
var sectionPicklistValue = Xrm.Page.data.entity.attributes.get("optionset_field").getValue();
var sections = Xrm.Page.ui.tabs.get(tabName).sections.get();
$(sections).each(function (index, section) {
//hide section
section.setVisible(false);
if (sectionPicklistValue != null) {
if (section.getLabel().startsWith(sectionPicklistValue.toString())) {
section.setVisible(true);
}
}
});
}
function ShowHideSectionsBySubject() {
var tabName = "tab_Name";
var sectionPicklistValue = Xrm.Page.data.entity.attributes.get("optionset_field").getValue();
var sections = Xrm.Page.ui.tabs.get(tabName).sections.get();
$(sections).each(function (index, section) {
//hide section
section.setVisible(false);
if (sectionPicklistValue != null) {
if (section.getLabel().startsWith(sectionPicklistValue.toString())) {
section.setVisible(true);
}
}
});
}
CRM 2011 Dependent optionsets - javascript
The idea is to filter optionset2 values based on selected value from optionset1. In my request I had a specific condition for filter but I'm sure you can use the same idea in your exact scenario.
function FilterOptionset2ByOptionset1() {
/* Get references to the related fields*/
var ParentField = Xrm.Page.data.entity.attributes.get("new_case_mainsubject");
var ChildField = Xrm.Page.data.entity.attributes.get("new_case_subsubjects");
/* Capture the current value of the child field*/
var CurrentChildFieldValue = ChildField.getValue();
var parentValue = ParentField.getValue();
/* Find the Options that corresponds to the value of the parent field. */
if (parentValue != null) {
var controls = ChildField.controls.get();
var bCurrentValueIsValid = false;
/*Enable the field and set the options*/
for (var ctrl in controls) {
controls[ctrl].setDisabled(false);
controls[ctrl].clearOptions();
var existingOptions = controls[ctrl].getAttribute().getOptions();
for (eo = 0; eo < existingOptions.length; eo++) {
//The condition for showing optionset2 value based on optionset1 value. The condition that I set bellow is just for test, you need to change it based on your needs.
if (parseInt(existingOptions[eo].value) == parseInt(parentValue)) {
controls[ctrl].addOption(existingOptions[eo]);
//if the current child selected value is in the filtered option list
if (CurrentChildFieldValue == existingOptions[eo].value) {
bCurrentValueIsValid = true;
}
}
}
}
/* If the value is valid, set it. If not, set the child field to null */
if (bCurrentValueIsValid) {
ChildField.setValue(CurrentChildFieldValue);
}
else {
ChildField.setValue(null);
}
ChildField.setSubmitMode("always");
ChildField.fireOnChange();
}
else {
//don't do any filter
var controls = ChildField.controls.get();
for (var ctrl in controls) {
controls[ctrl].setDisabled(false);
controls[ctrl].clearOptions();
var existingOptions = controls[ctrl].getAttribute().getOptions();
for (eo = 0; eo < existingOptions.length; eo++) {
if (existingOptions[eo].value != "null") {
controls[ctrl].addOption(existingOptions[eo]);
}
}
}
}
}
function FilterOptionset2ByOptionset1() {
/* Get references to the related fields*/
var ParentField = Xrm.Page.data.entity.attributes.get("new_case_mainsubject");
var ChildField = Xrm.Page.data.entity.attributes.get("new_case_subsubjects");
/* Capture the current value of the child field*/
var CurrentChildFieldValue = ChildField.getValue();
var parentValue = ParentField.getValue();
/* Find the Options that corresponds to the value of the parent field. */
if (parentValue != null) {
var controls = ChildField.controls.get();
var bCurrentValueIsValid = false;
/*Enable the field and set the options*/
for (var ctrl in controls) {
controls[ctrl].setDisabled(false);
controls[ctrl].clearOptions();
var existingOptions = controls[ctrl].getAttribute().getOptions();
for (eo = 0; eo < existingOptions.length; eo++) {
//The condition for showing optionset2 value based on optionset1 value. The condition that I set bellow is just for test, you need to change it based on your needs.
if (parseInt(existingOptions[eo].value) == parseInt(parentValue)) {
controls[ctrl].addOption(existingOptions[eo]);
//if the current child selected value is in the filtered option list
if (CurrentChildFieldValue == existingOptions[eo].value) {
bCurrentValueIsValid = true;
}
}
}
}
/* If the value is valid, set it. If not, set the child field to null */
if (bCurrentValueIsValid) {
ChildField.setValue(CurrentChildFieldValue);
}
else {
ChildField.setValue(null);
}
ChildField.setSubmitMode("always");
ChildField.fireOnChange();
}
else {
//don't do any filter
var controls = ChildField.controls.get();
for (var ctrl in controls) {
controls[ctrl].setDisabled(false);
controls[ctrl].clearOptions();
var existingOptions = controls[ctrl].getAttribute().getOptions();
for (eo = 0; eo < existingOptions.length; eo++) {
if (existingOptions[eo].value != "null") {
controls[ctrl].addOption(existingOptions[eo]);
}
}
}
}
}
Clear all checkboxes from a specific Tab in javascript - CRM 2011
I had an request that when an option field is changed to a specific value I had to clear all checkboxes found in a specific Tab. So I did this function:
function clearTabCheckboxes(tabName) {
//get tab
var tab = Xrm.Page.ui.tabs.get(tabName);
tab.sections.forEach(function (section, sectionIndex) {
section.controls.forEach(function (control, controlIndex) {
switch (control.getAttribute().getAttributeType()) {
case "boolean":
//for checkbox control, uncheck it
control.getAttribute().setValue(false);
break;
}
});
});
}
Hope this helps somebody else.
function clearTabCheckboxes(tabName) {
//get tab
var tab = Xrm.Page.ui.tabs.get(tabName);
tab.sections.forEach(function (section, sectionIndex) {
section.controls.forEach(function (control, controlIndex) {
switch (control.getAttribute().getAttributeType()) {
case "boolean":
//for checkbox control, uncheck it
control.getAttribute().setValue(false);
break;
}
});
});
}
Hope this helps somebody else.
CRM 2011: Populate email From field with a specific queue in javascript
I had to automatically set a specific queue in From field of an email when the form loads. So I created a function that I call it an Onload of email form for this. Note that I had extra checks (like email to be outgoing).
You need jquery and json2 scripts to be included on this form.
The code for GetServerUrl function you can find it other post of mine (or you can use your own method).
function PopulateFromAddressOnLoad() {
var formType = Xrm.Page.ui.getFormType();
var emailStatus = Xrm.Page.getAttribute("statecode").getValue();
var emailDirection = Xrm.Page.getAttribute("directioncode").getValue();
var defaultQueueEmail = "somequeueemails@email.com";
if (formType == 1 || (formType == 2 && emailStatus == "Open")) {
//check if it's outgoing
if (emailDirection == "1") {
$.ajax({
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: GetServerUrl() + "/xrmservices/2011/OrganizationData.svc/QueueSet?$select=Name,QueueId&$filter=EMailAddress eq '" + defaultQueueEmail + "'",
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
if (data != undefined && data.d != undefined && data.d.results[0] != undefined) {
var queue = data.d.results[0];
if (queue != null) {
var queueId = queue.QueueId;
var lookup = new Array();
var lookupItem = new Object();
lookupItem.id = queueId;
lookupItem.name = queue.Name;
lookupItem.typename = "queue";
lookup[0] = lookupItem;
Xrm.Page.getAttribute("from").setValue(lookup);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed '); }
});
}
}
}
You need jquery and json2 scripts to be included on this form.
The code for GetServerUrl function you can find it other post of mine (or you can use your own method).
function PopulateFromAddressOnLoad() {
var formType = Xrm.Page.ui.getFormType();
var emailStatus = Xrm.Page.getAttribute("statecode").getValue();
var emailDirection = Xrm.Page.getAttribute("directioncode").getValue();
var defaultQueueEmail = "somequeueemails@email.com";
if (formType == 1 || (formType == 2 && emailStatus == "Open")) {
//check if it's outgoing
if (emailDirection == "1") {
$.ajax({
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: GetServerUrl() + "/xrmservices/2011/OrganizationData.svc/QueueSet?$select=Name,QueueId&$filter=EMailAddress eq '" + defaultQueueEmail + "'",
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
if (data != undefined && data.d != undefined && data.d.results[0] != undefined) {
var queue = data.d.results[0];
if (queue != null) {
var queueId = queue.QueueId;
var lookup = new Array();
var lookupItem = new Object();
lookupItem.id = queueId;
lookupItem.name = queue.Name;
lookupItem.typename = "queue";
lookup[0] = lookupItem;
Xrm.Page.getAttribute("from").setValue(lookup);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed '); }
});
}
}
}
CRM 2011 Get the right server url in javascript
I used the method found on Daniel Cai's Blog and it worked for me, hopefully it will for for others too.
My function is a little modified (I removed some checks) but uses the same idea from Daniel's blog. Also the idea with prependOrgName that Andrii Butenko suggested worked for me.
My function looks like this:
function GetServerUrl () {
var context = Xrm.Page.context;
if (context.isOutlookClient() && !context.isOutlookOnline()) {
crmServerUrl = window.location.protocol + "//" + window.location.host;
} else {
crmServerUrl = context.getServerUrl();
crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
}
return crmServerUrl;
}
Update: With the Rollup 12 we can use getClientUrl that will solve all our problems. See bellow description taken from msdn
My function is a little modified (I removed some checks) but uses the same idea from Daniel's blog. Also the idea with prependOrgName that Andrii Butenko suggested worked for me.
My function looks like this:
function GetServerUrl () {
var context = Xrm.Page.context;
if (context.isOutlookClient() && !context.isOutlookOnline()) {
crmServerUrl = window.location.protocol + "//" + window.location.host;
} else {
crmServerUrl = context.getServerUrl();
crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
}
return crmServerUrl;
}
Update: With the Rollup 12 we can use getClientUrl that will solve all our problems. See bellow description taken from msdn
getClientUrl
Returns the base URL that was used to access the application.
This method is new in Microsoft Dynamics CRM 2011 Update Rollup 12 and the Microsoft Dynamics CRM December 2012 Service Update.
The values returned will resemble those listed in the following table.
| Client | Value |
|---|---|
Microsoft Dynamics CRM (on-premises)
|
http(s)://server/org
|
Microsoft Dynamics CRM Online
|
https://org.crm.dynamics.com
|
Microsoft Dynamics CRM for Outlook with Offline Access when offline
|
http://localhost:2525
|
- Return Value
- Type: String
Subscribe to:
Posts (Atom)