Useful CRM Tips

Useful CRM Tips...

Thursday, October 06, 2011

Sort Option Set (Picklist) on the CRM Form


/* Code to Sort Option List */

function sortList() {
    var lb = document.getElementById("fslr_custompicklist");
    arrTexts = new Array();
    arrValues = new Array();
    arrID = new Array();
    var len = lb.length;
    for (i = 0; i < len; i++) {
        arrTexts[i] = lb.options[i].text;
        arrValues[i] = lb.options[i].value;
        arrID[i] = lb.options[i].text;
    }
    arrID.sort();

    for (i = 0; i < lb.length; i++) {
        lb.options[i].text = arrID[i];
        for (j = 0; j < arrValues.length; j++) {
            if (arrTexts[j] == arrID[i]) {
                lb.options[i].value = arrValues[j];
                break;
            }
        }
    }
}

Display Loading Panel on Popup Loading

/* Code to Display Loading Panel for Popup Window */

crmForm.all.tab1Tab.onclick = function () {
    var iframe = document.getElementById("IFRAME_QueryXML");
    iframe.style.display = 'none';

    var AddedDiv = document.getElementById("MyDiv");
    if (AddedDiv == null) {
        var NewDiv = document.createElement("div");
        NewDiv.id = "MyDiv";
        NewDiv.style.position = "relative";
        NewDiv.style.left = "50%";
        NewDiv.style.top = "50%";
        var img = document.createElement("IMG");
        img.src = "/../ISV/Survey/Images/Progress.gif";
        NewDiv.appendChild(img);
        iframe.parentNode.appendChild(NewDiv);
    }
    crmForm.all.IFRAME_QueryXML.src = crmForm.all.IFRAME_QueryXML.src;
    iframe.onreadystatechange = function () {
        if (iframe.readyState == "complete") {
            document.getElementById("MyDiv").style.display = 'none';
            document.getElementById("IFRAME_QueryXML").style.display = 'inline';

        }
    }
}

Display a popup on crm form

/* Code to Display A Popup from CRM FORM */

var divid = document.getElementById("NewDiv");
if (divid == null) {
    var NewDivs = document.createElement('div'); NewDivs.id = "NewDiv"; NewDivs.innerHTML = "<div id='divCompanyInfo' align='center' style='width: 85%; height:auto;display:none; position: absolute; top:15%; left:10%; z-index: 10000;'>"
+ "<table border='0' bgcolor='#55729c' cellspacing='0' cellpadding='1' width='100%'>"
+ "<tr>"
+ "<td width='100%'>"
   + "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
        + "<tr>"
            + "<td style='cursor: pointer; font-size: 12px; font-weight: bold; background-color: #a4c0e7;' width='100%'>"
                + "<ilayer onselectstart='return false' width='100%'>"
       + "<layer width='100%'>"
    + "<div color='black' style='padding-left:10px;  height:25px; padding-top:5px;' face='Verdana'><div id = 'titleDiv'><div style='float:left; width:80%'>Address</div><div id= 'closediv' style='float:right; position:relative; right:3px; top:-3px; font-weight:bold; font-size:13px; background-color:#ffffff; padding:1px 3px 1px 3px; border:#666666 solid 1px; color:#666666;' >X</div></div></div></layer>"
   + "</ilayer>"
            + "</td>"
        + "</tr>"
        + "<tr>"
            + "<td bgcolor='#FFFFFF' colspan='2' width='100%'>"
                + "<div>"
                    + "<iframe id='frame_ViewComp' runat='server' frameborder='0' src='' style='width:100%;'></iframe>"
                + "</div>"
            + "</td>"
        + "</tr>"
        + "<tr>"
        + "<td>"
        + "<div style='background-color: #a4c0e7; height: 31px; text-align: right; border-top: #5771a2 silid 5px; padding: 5px 10px 0px 10px;'>"
            + "<div>"
            + "<asp:Button runat='server' ID='btncls' CssClass='crmbuttonLarge' style='float:left;' Width='120px' Text='Run Actions ' OnClientClick = 'CallIframe(); return false;' /></div>"
            + "<div id='footercloseDiv' style='width: 100px;'>"
            + "</div>"
        + "</div>"
        + "</td>"
        + "</tr>"
    + "</table>"
+ "</td>"
+ "</tr>"
+ "</table>"
+ "</div>";

    document.getElementById("tab0").appendChild(NewDivs);
    document.getElementById("closediv").onclick = function () { document.getElementById('divCompanyInfo').style.display = 'none'; };
}

   Use the below Code on CRM form Load Event to Create a Custom Button on the 
   CRMform:

  /* Code  to Create a Button on CRM FORM */

/* Convert TextBox to Button */
function create_button_from_textattribute(fieldname, buttontext, buttonwidth, clickevent) {
    functiontocall = clickevent;
    crmForm.all[fieldname].DataValue = buttontext;
    crmForm.all[fieldname].style.borderRight = "#3366cc 1px solid";
    crmForm.all[fieldname].style.paddingRight = "5px";
    crmForm.all[fieldname].style.borderTop = "#3366cc 1px solid";
    crmForm.all[fieldname].style.paddingLeft = "5px";
    crmForm.all[fieldname].style.fontSize = "11px";
    crmForm.all[fieldname].style.backgroundImage = "url(/_imgs/btn_rest.gif)";
    crmForm.all[fieldname].style.borderLeft = "#3366cc 1px solid";
    crmForm.all[fieldname].style.width = buttonwidth;
    crmForm.all[fieldname].style.cursor = "pointer";
    crmForm.all[fieldname].style.lineHeight = "14px";
    crmForm.all[fieldname].style.borderBottom = "#3366cc 1px solid";
    crmForm.all[fieldname].style.backgroundRepeat = "repeat-x";
    crmForm.all[fieldname].style.fontFamily = "Tahoma";
    crmForm.all[fieldname].style.height = "20px";
    crmForm.all[fieldname].style.backgroundColor = "#cee7ff";
    crmForm.all[fieldname].style.textAlign = "center";
    crmForm.all[fieldname].style.overflow = "hidden";
    crmForm.all[fieldname].attachEvent("onmousedown", push_button);
    crmForm.all[fieldname].attachEvent("onmouseup", release_button);
    crmForm.all[fieldname].attachEvent("onclick", functiontocall);
    crmForm.all[fieldname + '_c'].style.visibility = 'hidden';
}


function push_button() {
    window.event.srcElement.style.borderWidth = "2px";
    window.event.srcElement.style.borderStyle = "groove ridge ridge groove";
    window.event.srcElement.style.borderColor = "#3366cc #4080f0 #4080f0 #3366cc";
}

function release_button() {
    window.event.srcElement.style.border = "1px solid #3366cc";
}


/* tell the button what to do */
function Savetheform() {
    /* the function */
    document.getElementById('frame_ViewComp').src = "/isv/PostalCodeAddress/HTMLPage.htm";
    document.getElementById('divCompanyInfo').style.display = 'block';
}

create_button_from_textattribute('aa_addressbutton', 'Get Address', '100px', Savetheform);

Hide Left nav pane in Entity form

/* Code to Hide a Link in Left navigation Pane of a Entity */

var loadArea2 =  loadArea;

loadArea = function(area)
{
    loadArea2(area);
    if (area == 'fslr_fslr_survey_fslr_questionactionquery')
    {
        var iframe =
document.getElementById('fslr_fslr_survey_fslr_questionactionqueryFrame');

         iframe.onreadystatechange = function()
        {
            if (iframe.readyState == 'complete') {

document.frames['fslr_fslr_survey_fslr_questionactionqueryFrame'].document.getElementById('_MBtoplocAssocOneToMany10006fslrfslrsurveyfslrquestionactionquery').style.display = 'none';
            }

        }
    }
}

Call a On Demand Workflow with Javascript using Soap Call


/* Function to call an On Demand WorkFlow on a Button Click */

callworkflow = function () {
    crmForm.SaveAndClose();
    try {
        var PhoneCallId = crmForm.ObjectId;
        var authenticationHeader = GenerateAuthenticationHeader();
        //Prepare the SOAP message.
        var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:xsd="
http://www.w3.org/2001/XMLSchema">' +
authenticationHeader +
'<soap:Body>' +
'<Execute xmlns="
http://schemas.microsoft.com/crm/2007/WebServices">' +
'<Request xsi:type="ExecuteWorkflowRequest">' +
'<EntityId>' + PhoneCallId + '</EntityId>' +
'<WorkflowId>17E32F08-5D51-460E-B5A6-67AAEED92F20</WorkflowId>' +
'</Request>' +
'</Execute>' +
'</soap:Body>' +
'</soap:Envelope>'
        var xHReq = new ActiveXObject('Msxml2.XMLHTTP');
        xHReq.Open('POST', '/mscrmservices/2007/CrmService.asmx', false);
        xHReq.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/crm/2007/WebServices/Execute');
        xHReq.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
        xHReq.setRequestHeader('Content-Length', xml.length);
        xHReq.send(xml);

        var resultXml = xHReq.responseXML;
        if (resultXml) {
            return resultXml.selectSingleNode("//Response/Id").nodeTypedValue;
        }
        crmForm.ui.Close();
    }
    catch (e) {
        alert("Failed to Execute");
    }
    return null;
}

Alert User on Abnormal Close of CRM form Window


The Below Code will Popup the alert when you close the form without saving the changes made in that.

/* Code to Alert User On Abrupt Close of a Window */

window.onbeforeunload =
function () {
    if (document.getElementById("IFRAME_QueryXML").contentWindow.window.ispagedirty) {
        event.returnValue = "Changes made to Conditions will be lost!";
        return false;
    }
};