Quote Form OnLoad Implement Add Leftnav, count Activities
//--Add Label Text
if(crmForm.all.esp_billto_fulladdress != null)
{
var html= document.createElement( "<LABEL>");
html.innerText = "(請將FullAddress修改至Street1等欄位,否則PI報表會無法顯示地址)";
crmForm.all.esp_billto_fulladdress.parentNode.appendChild(html);
}
//--------------------
if (crmForm.FormType==1 || crmForm.FormType==2)
{
crmForm.all.esp_approveby.Disabled = true;
crmForm.all.esp_approveon.Disabled = true;
crmForm.all.esp_approve.Disabled = true;
//----------------------------
var def_Remark = crmForm.all.description;
if(def_Remark.DataValue == null && crmForm.all.esp_remarktype.DataValue == false)
{
// Link to DataBase
var objdbConn = new ActiveXObject("ADODB.Connection");
var objrs = new ActiveXObject("ADODB.Recordset");
var strsql;
var strdsn = "Provider=SQLOLEDB.1;Persist Security Info=True;inital Catalog=Everspring_MSCRM;Data Source=172.16.6.22;User ID=CRM_Report;Password=Ever01";
objdbConn.Open(strdsn);
strsql = "select new_remark from accountextensionbase where accountid = '98EF6774-7505-DE11-85D1-001EC9B6EAFD'"
objrs.Open(strsql,objdbConn);
if (!objrs.EOF)
{
def_Remark.DataValue = objrs(0).value;
}
else
{
def_Remark.DataValue = "";
}
objrs.Close();
objdbConn.Close();
objrs = null;
objdbConn = null;
}
}
//----------------------------
//----------------------------
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE:
case CRM_FORM_TYPE_UPDATE:
//*TAX
var oSubInd = crmForm.all.esp_taxrate;
oSubInd.originalPicklistOptions = oSubInd.Options;
if (crmForm.all.esp_taxname.DataValue == null)
{
oSubInd.Disabled = true;
}
else
{
var currentValue = oSubInd.DataValue
esp_taxname_onchange0();
oSubInd.DataValue = currentValue
}
break;
}
//----------------------------
//------將單據匯率,美元匯率設為唯讀------//
crmForm.all.esp_formexchangerate.Disabled= true;
crmForm.all.esp_usdexchangerate.Disabled= true;
//--加入一個新的nav
var _oArea;
fetchObj=new Object();
fetchObj.loadUserDefinedArea=_loadUserDefinedArea;
fetchObj.ExecuteQuery=_ExecuteQuery;
fetchObj.myalert=_myalert;
fetchObj.getFetchXml=_getFetchXml;
fetchObj.getLayoutXml=_getLayoutXml;
leftNavRename ('WriteInProducts','ProductPriceLevel','Write-In Products','Product Price Level');
// ----- 交期 ----- //
var showTxt;
showTxt = "1) 交期確認欄,為啟動報價之控制欄位。";
showTxt += "\n2) 各產品ITEM可單獨填寫交期,請點入Existing(Write-In) Products的產品項目中。";
showTxt += "\n3) 交期填寫完成,記得將Task勾選Complete,以利WorkFlow向下進行。"
crmForm.all.esp_info.DataValue = showTxt ;
if (crmForm.all.esp_check02.DataValue == 1)
{
crmForm.all.esp_confirm.Disabled= false;
crmForm.all.esp_finish_date.Disabled= false;
crmForm.all.esp_ext_date.Disabled= false;
crmForm.all.esp_remark.Disabled= false;
crmForm.all.esp_check02.Disabled= true;
//已填寫交期
if (crmForm.all.esp_confirm.DataValue == 1)
{
crmForm.all.esp_confirm.Disabled= true;
crmForm.all.esp_finish_date.Disabled= true;
crmForm.all.esp_ext_date.Disabled= true;
crmForm.all.esp_remark.Disabled= true;
}
}
else
{
crmForm.all.esp_confirm.Disabled= true;
crmForm.all.esp_finish_date.Disabled= true;
crmForm.all.esp_ext_date.Disabled= true;
crmForm.all.esp_remark.Disabled= true;
crmForm.all.esp_check02.Disabled= false;
}
//------------------------ 材積計算 --------------------------
var nCount = 0;
var QID = crmForm.ObjectId;
// FormType and ObjectId properties.
switch (crmForm.FormType)
{
case 1:
nCount += 1;
break;
case 2:
nCount += 1;
break;
}
//Total CUFT
if (nCount != 0)
{
if (QID != null)
{
// Link to DataBase
var objdbConn = new ActiveXObject("ADODB.Connection");
var objrs = new ActiveXObject("ADODB.Recordset");
var strsql;
var strdsn = "Provider=SQLOLEDB.1;Persist Security Info=True;inital Catalog=Everspring_MSCRM;Data Source=172.16.6.22;User ID=CRM_Report;Password=Ever01";
objdbConn.Open(strdsn);
strsql = "select Isnull(round(SUM(esp_CUFT),2),0) from QuoteDetailBase A, QuoteDetailExtensionBase B where A.QuoteDetailId = B.QuoteDetailId and A.DeletionStateCode = 0 and A.quoteid = '"+QID+"'"
objrs.Open(strsql,objdbConn);
if (!objrs.EOF)
{
if (objrs(0) != 0)
document.all.esp_total_cuft.value = objrs(0) ;
else
document.all.esp_total_cuft.value = 0;
}
else
{
document.all.esp_total_cuft.value = 0 ;
}
objrs.Close();
objdbConn.Close();
objrs = null;
objdbConn = null;
}
}
//------------------------ 交期欄位管控 --------------------------
// security levels
var HIDDEN = 1;
var READ_ONLY = 2;
var EDIT = 3;
// object types
var FIELD = 1;
var TAB = 2;
SetPageSecurity();
// Main page security routine
function SetPageSecurity()
{
var formObj = document.all.crmForm;
var formType = formObj.FormType;
// Retrieve user roles and security configuration for the form type.
var userRoles = GetUserRoles();
var securityObjects = GetSecurityObjects(formObj.ObjectTypeName, userRoles);
// loop through returned security objects and set up form security
for ( var i = 0; i < securityObjects.length; i++ ) {
switch( securityObjects[i].objectType ) {
case FIELD:
if ( formType == 4 )
return;
SetFieldSecurity( securityObjects[i] );
break;
case TAB:
SetTabSecurity( securityObjects[i] );
break;
default:
alert( "Unhandled object type in SetPageSecurity()" );
break;
}
}
}
// Configures the form security for fields
function SetFieldSecurity( securityObject )
{
var fieldObj = document.getElementById( securityObject.objectName );
if ( fieldObj != null ) {
switch( securityObject.securityLevel ) {
case EDIT:
SecurityShowField( fieldObj.id, false );
fieldObj.Disabled = false;
break;
case READ_ONLY:
SecurityShowField( fieldObj.id, false );
fieldObj.Disabled = true;
break;
case HIDDEN:
SecurityShowField( fieldObj.id, true );
fieldObj.Disabled = true;
break;
default:
alert( "Unhandled Security Level in SetFieldSecurity." );
break;
}
}
else {
alert( "An error occurred while attempting to set security on " + securityObject.objectName );
}
}
// Configures the form security for tabs
function SetTabSecurity( securityObject )
{
//don't hide tab on quick create
if(crmForm.FormType > 0 && crmForm.FormType == 5)
return;
var tab = document.getElementById( securityObject.objectName );
if ( tab != null ) {
switch( securityObject.securityLevel ) {
case HIDDEN:
tab.style.display = "none";
break;
case EDIT:
case READ_ONLY:
default:
break;
}
}
else
alert( "An error occurred while setting security on " + securityObject.objectName );
}
// Method that displays or hides fields.
function SecurityShowField( fieldName, isHidden )
{
var fieldObjTcLabel = document.getElementById( fieldName + "_d" );
var fieldObjTcControl = document.getElementById( fieldName + "_c" );
if ( isHidden ) {
fieldObjTcLabel.style.display = "none";
fieldObjTcControl.style.display = "none";
}
else {
fieldObjTcLabel.style.display = "inline";
fieldObjTcControl.style.display = "inline";
}
}
// Helper method to return the CRM security roles of a user
function GetUserRoles()
{
// Define 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\">",
GenerateAuthenticationHeader(),
"<soap:Body>",
"<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",
"<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' ",
"xsi:type='q1:QueryExpression'>",
"<q1:EntityName>role</q1:EntityName>",
"<q1:ColumnSet xsi:type='q1:ColumnSet'>",
"<q1:Attributes><q1:Attribute>name</q1:Attribute></q1:Attributes>",
"</q1:ColumnSet>",
"<q1:Distinct>false</q1:Distinct>",
"<q1:LinkEntities>",
"<q1:LinkEntity>",
"<q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>",
"<q1:LinkFromEntityName>role</q1:LinkFromEntityName>",
"<q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>",
"<q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>",
"<q1:JoinOperator>Inner</q1:JoinOperator>",
"<q1:LinkCriteria>",
"<q1:FilterOperator>And</q1:FilterOperator>",
"<q1:Conditions>",
"<q1:Condition>",
"<q1:AttributeName>systemuserid</q1:AttributeName>",
"<q1:Operator>EqualUserId</q1:Operator>",
"</q1:Condition>",
"</q1:Conditions>",
"</q1:LinkCriteria>",
"</q1:LinkEntity>",
"</q1:LinkEntities>",
"</query>",
"</RetrieveMultiple>",
"</soap:Body>",
"</soap:Envelope>"
].join("");
var resultXml = executeSoapRequest("RetrieveMultiple",xml);
return getMultipleNodeValues(resultXml, "q1:name");
}
// Helper method to custom security objects
function GetSecurityObjects(entity, userRoles)
{
// Define 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\">",
GenerateAuthenticationHeader(),
"<soap:Body>",
"<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">",
"<query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">",
"<q1:EntityName>sonoma_securitybehavior</q1:EntityName>",
"<q1:ColumnSet xsi:type=\"q1:AllColumns\" />",
"<q1:Distinct>false</q1:Distinct>",
"<q1:LinkEntities><q1:LinkEntity>",
"<q1:LinkFromAttributeName>sonoma_securitybehaviorid</q1:LinkFromAttributeName>",
"<q1:LinkFromEntityName>sonoma_securitybehavior</q1:LinkFromEntityName>",
"<q1:LinkToEntityName>sonoma_securitybehaviorrole</q1:LinkToEntityName>",
"<q1:LinkToAttributeName>sonoma_securitybehaviorid</q1:LinkToAttributeName>",
"<q1:JoinOperator>Inner</q1:JoinOperator>",
"<q1:LinkCriteria><q1:FilterOperator>And</q1:FilterOperator>",
"<q1:Conditions><q1:Condition>",
"<q1:AttributeName>sonoma_rolename</q1:AttributeName>",
"<q1:Operator>In</q1:Operator>",
"<q1:Values>"
].join("");
for (var i = 0; i < userRoles.length; i++)
{
xml += "<q1:Value xsi:type=\"xsd:string\">" + userRoles[i] + "</q1:Value>"
}
xml +=
[
"</q1:Values></q1:Condition></q1:Conditions>",
"</q1:LinkCriteria></q1:LinkEntity></q1:LinkEntities>",
"<q1:Criteria><q1:FilterOperator>And</q1:FilterOperator>",
"<q1:Conditions><q1:Condition><q1:AttributeName>sonoma_entityname</q1:AttributeName>",
"<q1:Operator>Equal</q1:Operator><q1:Values>",
"<q1:Value xsi:type=\"xsd:string\">",
entity,
"</q1:Value></q1:Values></q1:Condition></q1:Conditions></q1:Criteria>",
"</query>",
"</RetrieveMultiple>",
"</soap:Body>",
"</soap:Envelope>"
].join("");
/*
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
*/
var resultXml = executeSoapRequest("RetrieveMultiple",xml);
var securityObjects = new Array();
if( (resultXml.xml) != null && (resultXml.xml.toString().length) > 0)
{
var objectNodes = resultXml.getElementsByTagName( "BusinessEntity" );
// create an array of SecurityObject objects to deserialize the xml returned from the webservice
for ( var i = 0; i < objectNodes.length; i++ )
{
securityObjects[i] = new SecurityObject();
securityObjects[i].objectName = objectNodes[i].selectSingleNode( "q1:sonoma_targetname" ).text;
securityObjects[i].objectType = Number(objectNodes[i].selectSingleNode( "q1:sonoma_targettype" ).text);
securityObjects[i].securityLevel = Number(objectNodes[i].selectSingleNode( "q1:sonoma_behaviortype" ).text);
}
}
return securityObjects;
}
// SecurityObject properties
function SecurityObject()
{
this.objectName;
this.objectType;
this.securityLevel;
}
// Helper method to execute a SOAP request
function executeSoapRequest(action, xml)
{
var actionUrl = "http://schemas.microsoft.com/crm/2007/WebServices/";
actionUrl += action;
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction",actionUrl);
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return resultXml;
}
// Helper method to return a single node value from XML
function getNodeValue(tree, el)
{
var retVal = null;
var e = null;
e = tree.getElementsByTagName(el);
if (e != null && e[0] != null) {
retVal = e[0].firstChild.nodeValue;
}
return retVal;
}
// Helper method to return a multiple node value from XML
function getMultipleNodeValues(tree, el)
{
var retVal = new Array();
var e = null;
e = tree.getElementsByTagName(el);
for(i = 0; i < e.length; i++) {
retVal[i] = e[i].firstChild.nodeValue;
}
return retVal;
}
//------------ Activity & History Show Number -------------
var buXml = GetRegardingActivity();
if(buXml != null)
{
var buNodes = buXml.selectNodes("//BusinessEntity/q1:statecode"); // CRM 4.0
var iActivity = 0;
var iHistory = 0;
if(buNodes != null )
{
/*get values*/
for( i = 0; i < buNodes.length; i++)
{
switch(buNodes[i].text)
{
case "Open" : iActivity++; break;
case "Scheduled" : iActivity++; break;
case "Completed" : iHistory++; break;
case "Canceled" : iHistory++; break;
}
}
if(document.getElementById('navActivities') != null)
{
document.getElementById('navActivities').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navActivities').getElementsByTagName('NOBR')[0].innerText + " (" + iActivity + ")";
}
if(document.getElementById('navActivityHistory') != null)
{
document.getElementById('navActivityHistory').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navActivityHistory').getElementsByTagName('NOBR')[0].innerText + " (" + iHistory + ")";
}
}
}
function GetRegardingActivity()
{
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\">" +
" <soap:Body>" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +
" <q1:EntityName>activitypointer</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>statecode</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>regardingobjectid</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">" + crmForm.ObjectId + "</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return resultXml;
}
function leftNavRename(oldleftNav, newleftNav, oldName, newName)
{
var navItem = document.getElementById('nav'+oldleftNav);
if(navItem != null)
{
navItem=navItem.parentElement;
var navItemNewParent=navItem.cloneNode(true);
var navItemNew=navItemNewParent.firstChild;
navItemNew.id='nav'+newleftNav;
navItemNew.innerHTML = navItemNew.innerHTML.replace(oldName, newName);
navItemNew.innerHTML = navItemNew.innerHTML.replace(oldName, newName);
navItemNew.innerHTML = navItemNew.innerHTML.replace(oldleftNav, newleftNav);
navItemNew.onclick= function ()
{
loadArea(newleftNav,null,prependOrgName("/isv/var_advfetch.aspx"));
var associatedViewIFrame = document.getElementById(newleftNav+ 'Frame');
if (associatedViewIFrame != null)
{
associatedViewIFrame.onreadystatechange = function LoadAdvanceFetch()
{
//完成狀態的話,才去把Button去掉
var oRender=associatedViewIFrame.contentWindow.document.body.all["resultRender"];
if (associatedViewIFrame.readyState == 'complete' && oRender )
{
associatedViewIFrame.contentWindow.document.body.all["resultFrame"].contentWindow.document.body.innerHTML = "<table height='100%' width='100%' style='cursor:wait'><tr><td valign='middle' align='center'><img alt='' src='_imgs/AdvFind/progress.gif'/><br></td></tr></table>";
fetchObj.ExecuteQuery(oRender);
}
}
}
}
navItem.parentElement.appendChild(navItemNewParent);
}
}
function _loadUserDefinedArea(sArea, sParams, sUrl, bIsvMode)
{
debugger;
if (!_oArea) _oArea = tdAreas.firstChild;
var o=null;
if (o)
{
if (o == _oArea) return false;
_oArea.style.display = "none";
o.style.display = "inline";
}
else
{
var sBaseUrl = "areas.aspx";
if (sUrl)
{
sBaseUrl = sUrl;
}
if (sParams)
{
sBaseUrl += sParams;
}
html = "<iframe allowTransparency='-1' src='" + sBaseUrl;
html += "' id='" + sArea + "Frame' scrolling='no' isArea='1' width='100%' height='100%' frameborder='0'></iframe>";
o = document.createElement("DIV");
o.id = sArea;
if(!IsNull(sParams))
{
o.params = sParams;
}
o.innerHTML = html;
_oArea.style.display = "none";
tdAreas.appendChild(o);
}
_oArea = o;
ResetBreadcrumb(event.srcElement);
var button = document.all("_MBdocumentallRelatedInformationPaneExpanddocumentallRelatedInformationPaneLoadContextDatafollowup");
if (button != null)
{
if (sArea == "areaForm")
{
button.disabled = false;
}
else
{
button.disabled = true;
}
}
}
function _ExecuteQuery(vDynamicForm)
{
vDynamicForm.FetchXml.value = fetchObj.getFetchXml();
vDynamicForm.LayoutXml.value = fetchObj.getLayoutXml();
vDynamicForm.EntityName.value = 'esp_product_modeltype';
vDynamicForm.DefaultAdvFindViewId.value = '{EF964A3C-18A8-43E2-8DC5-671DD6527DF3}';
vDynamicForm.ViewId.value='{86979DBA-2878-DE11-A5C7-001EC9B6EAFD}';
vDynamicForm.action="/AdvancedFind/fetchData.aspx";
vDynamicForm.ViewType.value = 4230;
vDynamicForm.submit();
}
function _myalert(message)
{
alert(message);
}
function _getFetchXml()
{
var lookupItem = new Array;
// Get the lookup for the primarycontactid attribute on the account form.
lookupItem = crmForm.all.pricelevelid.DataValue;
return '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="esp_product_modeltype"><attribute name="esp_product_modeltypeid"/><attribute name="esp_pricelevelid"/><attribute name="esp_productnumber"/><attribute name="esp_productid"/><attribute name="esp_uomid"/><attribute name="esp_amount"/><attribute name="transactioncurrencyid"/><order attribute="esp_pricelevelid" descending="false"/><filter type="and"><condition attribute="esp_pricelevelid" operator="in" > <value>{9439FF4D-8613-DE11-B063-001EC9B6EAFD}</value><value>'+lookupItem[0].id+ '</value></condition></filter></entity></fetch>';
}
function _getLayoutXml()
{
return '<grid name="resultset" object="10011" jump="esp_name" select="1" icon="1" preview="1"><row name="result" id="esp_product_modeltypeid"><cell name="esp_pricelevelid" width="100"/><cell name="esp_productid" width="100"/><cell name="esp_productnumber" width="100"/><cell name="esp_uomid" width="100"/><cell name="transactioncurrencyid" width="100"/><cell name="esp_amount" width="100"/></row></grid>';
}
1.加Leftnav
2.計算Activities的數量及Histories的數量
3.soap help method