CRM Javascript
Simple field validation using RegEx (e.g. ABN):
Hiding fields:
Hiding tabs:
Disabling fields within a section:
Get Potential Customer's Primary Contact and set it to Customer Contact field:
Lock Potential Customer to 'Account' Only:
1 2 3 4 5 6 7 8 9 | var abn = crmForm.all.new_abn; var abnRegEx = /^\d{11}$/; // Verify that the shortname field exists and is not null. if (abn != null && abnRegEx.test(abn.DataValue) != true ) { alert( 'ABN must be 11 digits characters length' ); abn.DataValue = '' ; } |
Hiding fields:
1 2 | crmForm.all.new_invoicenumber_d.style.display = 'none' ; crmForm.all.new_invoicenumber_c.style.display = 'none' ; |
Hiding tabs:
1 2 | crmForm.all.tab1Tab.style.display = "inline" ; crmForm.all.tab2Tab.style.display = "none" ; |
Disabling fields within a section:
1 2 3 4 5 6 7 8 9 10 11 | //enable fields if Enquiry Group is not null, disable otherwise var section = crmForm.all.new_quotenumber_c.parentElement.parentElement.parentElement; for (i = 0; i < section.all.length; i++) { if (crmForm.all.new_enquirygroup.DataValue == null ) { section.all[i].Disabled = true ; } else { section.all[i].Disabled = false ; } } |
Get Potential Customer's Primary Contact and set it to Customer Contact field:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | var primaryContactID = '' ; var primaryContactName = '' ; get_primarycontact(); //Get shortname out of Potential Customer function get_primarycontact() { if (crmForm.all.customerid.DataValue != null ) { //Prepare variables for an enquiry to retrieve. var lookupItem = new Array; // Get the lookup attribute on the quality assurance checklist form. lookupItem = crmForm.all.customerid.DataValue; var potentialCustomer = lookupItem[0].id; var authenticationHeader = GenerateAuthenticationHeader(); //Prepare the SOAP message. var xml = "<?xml version='1.0' encoding='utf-8'?>" + authenticationHeader + "<soap:Body>" + "<entityname>account</entityName>" + "<id>" + potentialCustomer + "</id>" + "<q1:Attributes>" + "<q1:Attribute>primarycontactid</q1:Attribute>" + "</q1:Attributes>" + "</columnSet>" + "</Retrieve>" + "</soap:Body>" + "</soap:Envelope>" ; //Prepare the xmlHttpObject and send the request. var xHReq = new ActiveXObject( "Msxml2.XMLHTTP" ); xHReq.Open( "POST" , "/mscrmservices/2007/CrmService.asmx" , false ); xHReq.setRequestHeader( "Content-Type" , "text/xml; charset=utf-8" ); xHReq.setRequestHeader( "Content-Length" , xml.length); xHReq.send(xml); //Capture the result. var resultXml = xHReq.responseXML; //Check for errors. var errorCount = resultXml.selectNodes( '//error' ).length; if (errorCount != 0) { var msg = resultXml.selectSingleNode( '//description' ).nodeTypedValue; alert(msg); } //Store the retrieved value. else { if (resultXml.selectSingleNode( "//q1:primarycontactid" ) != null ) { primaryContactID = resultXml.selectSingleNode( "//q1:primarycontactid" ).nodeTypedValue; primaryContactName = resultXml.selectSingleNode( "//q1:primarycontactid" ).getAttribute( 'name' ); //Create an array to set as the DataValue for the lookup control. var lookupData = new Array(); //Create an Object add to the array. var lookupItem = new Object(); //Set the id, typename, and name properties to the object. lookupItem.id = primaryContactID; lookupItem.typename = 'contact' ; lookupItem.name = primaryContactName; // Add the object to the array. lookupData[0] = lookupItem; // Set the value of the lookup field to the value of the array. crmForm.all.new_customercontact.DataValue = lookupData; } } } } |
Lock Potential Customer to 'Account' Only:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | //Javascript Helper var LOGICAL_OPERATOR_AND = "And" ; var LOGICAL_OPERATOR_OR = "Or" ; var CONDITION_OPERATOR_LIKE = "Like" ; var CONDITION_OPERATOR_EQUAL = "Equal" ; var CONDITION_OPERATORNOT_EQUAL = "NotEqual" ; var JOINOPERATOR_INNER = "Inner" ; var JOINOPERATOR_LEFTOUTER = "LeftOuter" ; var JOINOPERATOR_NATURAL = "Natural" ; function CrmService(entityName, logicalOperator) { // Double check in case you pass a variable that hasn't been set // This error is hear to track down if (logicalOperator == null ) throw new Error( "Must specify non-null value for logicalOperator" ); if (entityName == null ) throw new Error( "Must specify non-null value for entityName" ); this .entityName = entityName; this .ColumnSet = new Array(); this .LogicalOperator = logicalOperator; this .Conditions = new Array(); this .Orders = new Array(); this .LinkedEntities = new Array(); } CrmService.prototype.getEntityName = function () { return this .entityName; } function Condition(field, value, operator) { this .Field = field; // Not null can't have a value if (operator != "NotNull" && operator != "Null" ) this .Value = CrmEncodeDecode.CrmXmlEncode(value); // Double check in case you pass a variable that hasn't been set // This error is hear to track down if (operator == null ) throw new Error( "Must specify non-null value for operator" ); this .Operator = operator; } function Order(attributeName, orderType) { this .AttributeName = attributeName; this .OrderType = orderType; } CrmService.prototype.setEntityName = function () { return this .entityName; } CrmService.prototype.AddColumn = function (columnName) { this .ColumnSet[ this .ColumnSet.length] = columnName; } CrmService.prototype.AddFilterCondition = function (field, value, conditionOperator) { this .Conditions[ this .Conditions.length] = new Condition(field, value, conditionOperator); } CrmService.prototype.AddOrder = function (attributeName, orderType) { this .Orders[ this .Orders.length] = new Order(attributeName, orderType); } /* for (orderNumber in this.Orders) { var order = this.Orders[orderNumber]; if (order.OrderType == null) throw new Error("Must specify non-null value for order OrderType"); if (order.AttributeName == null) throw new Error("Must specify non-null value for order AttributeName"); xmlSoapBody += "<q1:Order><q1:AttributeName>" + order.AttributeName + "</q1:AttributeName>"; xmlSoapBody += "<q1:OrderType>" + order.OrderType + "</q1:OrderType></q1:Order>"; } */ function LinkedEntity(linkFromEntityName, linkToEntityName, linkFromAttributeName, linkToAttributeName, joinOperator) { this .LinkFromEntityName = linkFromEntityName; this .LinkToEntityName = linkToEntityName; this .LinkFromAttributeName = linkFromAttributeName; this .LinkToAttributeName = linkToAttributeName; if (joinOperator == null ) throw new Error( "Must specify non-null value for operator" ); this .JoinOperator = joinOperator; this .Conditions = new Array(); this .FilterOperator = LOGICAL_OPERATOR_AND; } LinkedEntity.prototype.AddFilterCondition = function (field, value, conditionOperator) { this .Conditions[ this .Conditions.length] = new Condition(field, value, conditionOperator); return this .Conditions[ this .Conditions.length - 1]; } CrmService.prototype.AddLinkedEntityCondition = function (linkFromEntityName, linkToEntityName, linkFromAttributeName, linkToAttributeName, joinOperator) { this .LinkedEntities[ this .LinkedEntities.length] = new LinkedEntity(linkFromEntityName, linkToEntityName, linkFromAttributeName, linkToAttributeName, joinOperator); return this .LinkedEntities[ this .LinkedEntities.length - 1]; } function RetrieveMultipleResult(crmService) { this .Rows = new Array(); this .CrmService = crmService; } RetrieveMultipleResult.prototype.AddRow = function () { this .Rows[ this .Rows.length] = new Row(); return this .Rows[ this .Rows.length - 1]; } function Row() { this .Columns = new Array(); } function Column(columnName, value, dataType) { this .ColumnName = columnName; this .Value = value; this .DataType = dataType; } Row.prototype.AddColumn = function (columnName, value) { this .Columns[ this .Columns.length] = new Column(columnName, value); } Row.prototype.GetColumn = function (columnName) { for (columnNumber in this .Columns) { var column = this .Columns[columnNumber]; if (columnName.toLowerCase() == column.ColumnName.toLowerCase()) return column; } throw new Error( "Column " + columnName + " does not exist" ); } Row.prototype.GetValue = function (columnName) { var column = this .GetColumn(columnName); return column.Value; } CrmService.prototype.RetrieveMultiple = function () { //create SOAP envelope var xmlSoapHeader = "" + "<?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\">" ; var xmlAuthHeader = GenerateAuthenticationHeader(); var xmlSoapBody = "<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>" + this .getEntityName() + "</q1:EntityName> " + " <q1:ColumnSet xsi:type=\"q1:ColumnSet\"> " + " <q1:Attributes> " ; for (columnNumber in this .ColumnSet) { var column = this .ColumnSet[columnNumber]; xmlSoapBody = xmlSoapBody + " <q1:Attribute>" + column + "</q1:Attribute>" ; } xmlSoapBody = xmlSoapBody + " </q1:Attributes>" + " </q1:ColumnSet>" + " <q1:Distinct>false</q1:Distinct> " + " <q1:PageInfo> " + " <q1:PageNumber>0</q1:PageNumber> " + " <q1:Count>0</q1:Count> " + " </q1:PageInfo> " + " <q1:LinkEntities>" ; if ( this .LinkedEntities.length > 0) { for (linkedEntityNumber in this .LinkedEntities) { var linkedEntity = this .LinkedEntities[linkedEntityNumber]; xmlSoapBody += " <q1:LinkEntity> " ; xmlSoapBody += " <q1:LinkFromAttributeName>" + linkedEntity.LinkFromAttributeName + "</q1:LinkFromAttributeName> " ; xmlSoapBody += " <q1:LinkFromEntityName>" + linkedEntity.LinkFromEntityName + "</q1:LinkFromEntityName> " ; xmlSoapBody += " <q1:LinkToEntityName>" + linkedEntity.LinkToEntityName + "</q1:LinkToEntityName> " ; xmlSoapBody += "<q1:LinkToAttributeName>" + linkedEntity.LinkToAttributeName + "</q1:LinkToAttributeName> " ; xmlSoapBody += "<q1:JoinOperator>" + linkedEntity.JoinOperator + "</q1:JoinOperator> " ; xmlSoapBody += "<q1:LinkCriteria> " ; if (linkedEntity.FilterOperator == null ) throw new Error( "Must specify non-null value for FilterOperator" ); xmlSoapBody += " <q1:FilterOperator>" + linkedEntity.FilterOperator + "</q1:FilterOperator> " ; xmlSoapBody += " <q1:Conditions> " ; for (conditionLinkedNumber in linkedEntity.Conditions) { var conditionLinked = linkedEntity.Conditions[conditionLinkedNumber]; xmlSoapBody += " <q1:Condition> " ; xmlSoapBody += " <q1:AttributeName>" + conditionLinked.Field + "</q1:AttributeName> " ; xmlSoapBody += " <q1:Operator>" + conditionLinked.Operator + "</q1:Operator> " ; if (conditionLinked.Operator != "NotNull" && conditionLinked.Operator != "Null" ) { xmlSoapBody += " <q1:Values> " ; xmlSoapBody += " <q1:Value xsi:type=\"xsd:string\">" + conditionLinked.Value + "</q1:Value> " ; xmlSoapBody += " </q1:Values> " ; } xmlSoapBody += " </q1:Condition> " ; } xmlSoapBody += " </q1:Conditions> " ; xmlSoapBody += " <q1:Filters /> " ; xmlSoapBody += "</q1:LinkCriteria> " ; xmlSoapBody += "<q1:LinkEntities />" ; xmlSoapBody += "</q1:LinkEntity>" ; } } if ( this .LogicalOperator == null ) throw new Error( "Must specify non-null value for LogicalOperator" ); xmlSoapBody += "</q1:LinkEntities>" + " <q1:Criteria> " + " <q1:FilterOperator>" + this .LogicalOperator + "</q1:FilterOperator> " + " <q1:Conditions> " ; for (conditionNumber in this .Conditions) { var condition = this .Conditions[conditionNumber]; if (condition.Operator == null ) throw new Error( "Must specify non-null value for condition Operator" ); xmlSoapBody += " <q1:Condition> " + " <q1:AttributeName>" + condition.Field + "</q1:AttributeName> " + " <q1:Operator>" + condition.Operator + "</q1:Operator> " ; if (condition.Operator != "NotNull" && condition.Operator != "Null" ) { xmlSoapBody += " <q1:Values> " ; xmlSoapBody += " <q1:Value xsi:type=\"xsd:string\">" + condition.Value + "</q1:Value> " ; xmlSoapBody += " </q1:Values> " ; } xmlSoapBody += " </q1:Condition> " ; } xmlSoapBody += " </q1:Conditions> " + " <q1:Filters /> " + " </q1:Criteria> " ; xmlSoapBody += "<q1:Orders>" ; for (orderNumber in this .Orders) { var order = this .Orders[orderNumber]; if (order.OrderType == null ) throw new Error( "Must specify non-null value for order OrderType" ); if (order.AttributeName == null ) throw new Error( "Must specify non-null value for order AttributeName" ); xmlSoapBody += "<q1:Order><q1:AttributeName>" + order.AttributeName + "</q1:AttributeName>" ; xmlSoapBody += "<q1:OrderType>" + order.OrderType + "</q1:OrderType></q1:Order>" ; } xmlSoapBody += "</q1:Orders>" ; xmlSoapBody += " </query> " + " </RetrieveMultiple> " + " </soap:Body> " + " </soap:Envelope>" ; var xmlt = xmlSoapHeader + xmlAuthHeader + xmlSoapBody; 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" , xmlt.length); xmlHttpRequest.send(xmlt); if (xmlHttpRequest.responseXML == null || xmlHttpRequest.responseXML.xml == null || xmlHttpRequest.responseXML.xml == "" ) { if (xmlHttpRequest.responseText != null && xmlHttpRequest.responseText != "" ) throw new Error(xmlHttpRequest.responseText); else throw new Error( "Error returning response" ); } var xmlResponse = xmlHttpRequest.responseXML.xml; if (xmlHttpRequest.responseXML.documentElement.selectNodes( "//error/description" ).length > 0) { throw new Error(xmlResponse); } var objNodeList = xmlHttpRequest.responseXML.documentElement.selectNodes( "//BusinessEntity" ); var totalNodesCount = objNodeList.length; var result = new RetrieveMultipleResult( this ); var nodeIndex = 0; var fieldTextTemp = "" ; var fieldText = "" ; if (totalNodesCount > 0) { do { var row = result.AddRow(); for (columnNumber in this .ColumnSet) { var columnName = this .ColumnSet[columnNumber]; fieldText = "" ; var valueNode = objNodeList[nodeIndex].getElementsByTagName( "q1:" + columnName)[0]; if (valueNode != null ) { fieldTextTemp = valueNode.childNodes[0].nodeValue; if (fieldTextTemp != null && fieldTextTemp != "" ) { fieldText = fieldText + fieldTextTemp; } } row.AddColumn(columnName, fieldText); } nodeIndex = nodeIndex + 1; } while (totalNodesCount > nodeIndex) } return result; } //lock customer to account only // Blank out customerid if they didn't default in an account var LOGICAL_OPERATOR_AND = "And" ; var LOGICAL_OPERATOR_OR = "Or" ; var CONDITION_OPERATOR_EQUAL = "Equal" ; var JOINOPERATOR_INNER = "Inner" ; if ((crmForm.FormType == 1 || crmForm.FormType == 2) && crmForm.all.customerid.DataValue != null && crmForm.all.customerid.DataValue[0].type != "1" ) { // If contact then blank out the field and set to that account if (crmForm.all.customerid.DataValue[0].type == "2" ) { // Create object passing in the entity you are selecting from var crmService = new CrmService( "account" , LOGICAL_OPERATOR_OR); // Specify select columns crmService.AddColumn( "accountnumber" ); crmService.AddColumn( "accountid" ); crmService.AddColumn( "name" ); // Define linked entity - similar to SDK overload var entityLinked = crmService.AddLinkedEntityCondition( "account" , "contact" , "accountid" , "parentcustomerid" , JOINOPERATOR_INNER) // Set filter operator (AND, OR, Ect) entityLinked.FilterOperator = LOGICAL_OPERATOR_AND; // Add filter condition (can add as multiple) entityLinked.AddFilterCondition( "contactid" , crmForm.all.customerid.DataValue[0].id, CONDITION_OPERATOR_EQUAL); // Retrieve the result object var result = crmService.RetrieveMultiple(); // Loop through rows and select values (they return strings) for (rowsNumber in result.Rows) { var row = result.Rows[rowsNumber]; var ar = new Array(1); ar[0] = new Object; ar[0].id = row.GetValue( "accountid" ); ar[0].type = "1" ; ar[0].name = row.GetValue( "name" ); ar[0].typename = "account" ; crmForm.all.customerid.DataValue = ar; } } // After contact fix, double check that it isn't set to something else (lead) and blank it out if (crmForm.all.customerid.DataValue != null && crmForm.all.customerid.DataValue[0].type != "1" ) crmForm.all.customerid.DataValue = null ; } crmForm.all.customerid.setAttribute( "lookuptypes" , "1" ); |
hi,
ReplyDeleteI have a problem. i have created a custom entity Test which has a many to many relationship with Contact. the problem is some information entered in test are not populated in Contact. what can i do?? am i going to use mapping ? if yes how to do that ??