14

Sto lavorando su alcune personalizzazioni CRM 2011 online e ho bisogno di ottenere un'entità utilizzando javascript.CRM 2011: Ottenere entità con Javascript

L'entità Ho bisogno sarà basato sul valore ID di un altro campo (un ente di contatto) - questo ID Contatto posso ottenere bene.

L'entità che desidero è un'entità personalizzata. Ci possono essere più corrispondenze in base all'ID di contatto quindi voglio solo per ottenere il primo della lista (ordine non è importante)

Finora ho guardato in un paio di modi per fare questo ...

  • OData - non riuscivo a trovare abbastanza esempi su questo su ciò che domanda espressioni posso creare, anche io non so se/come fare questo lavoro per entità personalizzate

  • FetchXML - posso creare un bella query FetchXML utilizzando anche la "ricerca avanzata" integrata e sarebbe felice di chiamarla da javascript se qualcuno può aiutarti? Ho trovato una risposta promettente here ma non sono riuscito a vedere come venivano restituiti i dati "risultati"

  • Richiesta SOAP: la prima cosa che ho provato è un metodo simile a quello che avrei potuto fare in CRM 4 ma questo non sembra funzionare. Sebbene la richiesta venga eseguita, i dati dei miei risultati sembrano essere vuoti. Questo è tutto ciò che ho codice per cui se qualcuno può individuare un problema con il codice qui sotto sarebbe fantastico.

EDIT: ho individuato alcuni dati di query ridondanti (avevo rimosso l'apertura tag link, ma lasciato i tag di chiusura) - dal momento che la rimozione di questo io ora ottenere dati di risultato XML ... tuttavia, la clausola in cui non lo fa sembra applicarsi (solo ottenere l'elenco di tutte le entità)

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>new_vehicle</q1:EntityName>" + 
    "<q1:ColumnSet xsi:type='q1:ColumnSet'>" + 
    "<q1:Attributes>" + 
    "<q1:Attribute>new_vehicleid</q1:Attribute>" + 
    "<q1:Attribute>new_primarydriver</q1:Attribute>" + 
    "<q1:Attribute>statuscode</q1:Attribute>" + 
    "<q1:Attribute>new_registration</q1:Attribute>" + 
    "</q1:Attributes>" + 
    "</q1:ColumnSet>" + 
    "<q1:Distinct>false</q1:Distinct>" + 

    "<q1:Conditions>" + 

        "<q1:Condition>" + 
        "<q1:AttributeName>new_primarydriver</q1:AttributeName>" + 
    "<q1:Operator>Equal</q1:Operator>" + 
    "<q1:Values>" + 
    "<q1:Value xmlns:q2='http://microsoft.com/wsdl/types/' xsi:type='q2:guid'>" + 
    customerID + 
    "</q1:Value></q1:Values></q1:Condition>" + 

    "</q1:Conditions>" + 

    "</query></RetrieveMultiple>" + 
    "</soap:Body></soap:Envelope>"; 


    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 result = xmlHttpRequest.responseXML.xml; 
    var doc = new ActiveXObject("MSXML2.DOMDocument"); 
    doc.async = false; 
    doc.loadXML(result); 

    var id = doc.selectSingleNode("//new_vehicleid"); 
    var registration = doc.selectSingleNode("//new_registration"); 

    if(id == null) 
     return null; 

    var vehicle = new Array(); 
        value[0] = new Object(); 
        value[0].id = id; 
        value[0].name = registration; 
        value[0].entityType = "new_vehicle"; 

    return vehicle; 

Mi dispiace per il grande codice postale, ma si spera che qualcuno che ha una migliore comprensione può aiutare

risposta

28

in primo luogo, grazie alla GlennFerrieLive per il suo posto risposta. I campioni che ho trovato con lo Dynamics CRM 2011 SDK (beh solo uno in particolare) mi hanno davvero aiutato e il parser JSON incluso era perfetto per il lavoro!

sto postando questa risposta per dare un esempio completo di come ho fatto con alcuni commenti importanti di prestare attenzione a che non può essere così evidente dagli esempi SDK.


selezionato valore di ID dal campo di ricerca

Lo scopo del mio compito era quello di usare javascript per ottenere impostare un campo di ricerca, sulla base dei dati selezionati di un'altra entità di ricerca. L'entità da impostare è "new_vehicle" e l'entità su cui eseguire la query è "customer".

Primo lavoro per ottenere il valore ID del campo di ricerca dei contatti ...

var customerItem = Xrm.Page.getAttribute("customerid").getValue(); 
var customerID = customerItem[0].id; 

Interrogazione di un'entità utilizzando un ID

Next è la parte in cui ho usato il valore customerID per trovare il veicolo che è attualmente assegnato a loro (l'ente che desidera utilizzare per imposta un campo di ricerca).

primo problema che ho trovato era che quando l'interrogazione con OData, il valore ID non sembra funzionare con parentesi graffe {} - così questi deve essere rimosso ...

customerID = customerID.replace('{', '').replace('}', ''); 

Avanti si ottiene l'oDataPath ...

var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc"; 

Poi possiamo costruire la query OData ...

var filter = "/new_vehicleSet?" + 
    "$select=new_vehicleId,new_Registration" + 
    "&$filter=new_PrimaryDriver/Id eq (guid'" + customerID + "')" + 
    "&$orderby=new_LastAllocationDate desc" + 
    "&$top=1"; 

NOTA: ci sono un paio di imb e di cose importanti da notare qui ...

  1. Quando si utilizza un valore GUID è necessario in modo esplicito dire che è un GUID utilizzando (guid'xxx')
  2. Quando il filtraggio da un ente di ricerca (per esempio new_PrimaryDriver) è necessario aggiungere il valore da interrogare (ad es Id) - questo si traduce in new_PrimaryDriver/Id

Una volta che abbiamo la configurazione richiesta siamo in grado di richiedere i dati come segue ...

var retrieveRecordsReq = new XMLHttpRequest(); 
retrieveRecordsReq.open("GET", oDataPath + filter, true); 
retrieveRecordsReq.setRequestHeader("Accept", "application/json"); 
retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
retrieveRecordsReq.onreadystatechange = function() { 
    if (this.readyState == 4) { 
     if (this.status == 200) { 
      var retrievedRecords = JSON.parse(retrieveRecordsReq.responseText).d; 
      if(retrievedRecords.results.length > 0) 
      { 
       var vehicle = retrievedRecords.results[0]; 
       SetLookup("new_replacedvehicle", vehicle.new_vehicleId, vehicle.new_Registration, "new_vehicle"); 
      } 
     } 
    } 
}; 
retrieveRecordsReq.send(); 

Nota che questa è una chiamata asincrona e la funzione onreadystatechange verrà elaborata al completamento, in questa funzione facciamo un paio di controlli per vedere se è stato un successo e analizziamo i dati JSON risultanti - la funzione JSON.Parse è stata inclusa in il fondo di questo post (ma è disponibile dal SDK)


Impostare un campo di ricerca utilizzando l'entità interrogato sopra

L'altra funzione di annotare qui è SetLookup che è solo una semplice funzione di supporto ho aggiunto per impostare un campo di ricerca. Questo è il seguente ...

function SetLookup(fieldName, idValue, textValue, typeValue) 
{ 
    var value = new Array(); 
    value[0] = new Object(); 
    value[0].id = idValue; 
    value[0].name = textValue; 
    value[0].typename = typeValue; 

    Xrm.Page.getAttribute(fieldName).setValue(value); 
} 

funzione di parsing di JSON

Questa è la funzione di supporto JSON che è stato utilizzato nel codice di cui sopra (JSON.parse), incollato come è stato trovato nel SDK ...

if (!this.JSON) { this.JSON = {}; } (function() { function f(n) { return n < 10 ? '0' + n : n; } if (typeof Date.prototype.toJSON !== 'function') { Date.prototype.toJSON = function (key) { return isFinite(this.valueOf()) ? this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z' : null; }; String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function (key) { return this.valueOf(); }; } var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, gap, indent, meta = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' }, rep; function quote(string) { escapable.lastIndex = 0; return escapable.test(string) ? '"' + string.replace(escapable, function (a) { var c = meta[a]; return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + string + '"'; } function str(key, holder) { var i, k, v, length, mind = gap, partial, value = holder[key]; if (value && typeof value === 'object' && typeof value.toJSON === 'function') { value = value.toJSON(key); } if (typeof rep === 'function') { value = rep.call(holder, key, value); } switch (typeof value) { case 'string': return quote(value); case 'number': return isFinite(value) ? String(value) : 'null'; case 'boolean': case 'null': return String(value); case 'object': if (!value) { return 'null'; } gap += indent; partial = []; if (Object.prototype.toString.apply(value) === '[object Array]') { length = value.length; for (i = 0; i < length; i += 1) { partial[i] = str(i, value) || 'null'; } v = partial.length === 0 ? '[]' : gap ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : '[' + partial.join(',') + ']'; gap = mind; return v; } if (rep && typeof rep === 'object') { length = rep.length; for (i = 0; i < length; i += 1) { k = rep[i]; if (typeof k === 'string') { v = str(k, value); if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v); } } } } else { for (k in value) { if (Object.hasOwnProperty.call(value, k)) { v = str(k, value); if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v); } } } } v = partial.length === 0 ? '{}' : gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : '{' + partial.join(',') + '}'; gap = mind; return v; } } if (typeof JSON.stringify !== 'function') { JSON.stringify = function (value, replacer, space) { var i; gap = ''; indent = ''; if (typeof space === 'number') { for (i = 0; i < space; i += 1) { indent += ' '; } } else if (typeof space === 'string') { indent = space; } rep = replacer; if (replacer && typeof replacer !== 'function' && (typeof replacer !== 'object' || typeof replacer.length !== 'number')) { throw new Error('JSON.stringify'); } return str('', { '': value }); }; } if (typeof JSON.parse !== 'function') { JSON.parse = function (text, reviver) { var j; function walk(holder, key) { var k, v, value = holder[key]; if (value && typeof value === 'object') { for (k in value) { if (Object.hasOwnProperty.call(value, k)) { v = walk(value, k); if (v !== undefined) { value[k] = v; } else { delete value[k]; } } } } return reviver.call(holder, key, value); } text = String(text); cx.lastIndex = 0; if (cx.test(text)) { text = text.replace(cx, function (a) { return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }); } if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { j = eval('(' + text + ')'); return typeof reviver === 'function' ? walk({ '': j }, '') : j; } throw new SyntaxError('JSON.parse'); }; } }()); 
+0

Dynamics CRM ?, non comprarlo se avete bisogno di personalizzazioni: D requisito semplice, complicato implementazione ... – Legends