Principale utente con più risposte
JScript - autofill di campi di un'entità con i dati di una seconda (non parente)

Domanda
-
Buongiorno, ho la necessità di compilare in automatico dei campi da me creati nel form Offerte(Prodotti) con delle informazioni presenti sul record dell'entità Prodotto selezionata.
ho realizzato il seguente script ma funziona solo per il campo "name" e "id"gli altri vengono ignorati:
function dettagli_prodotto()
{
var dettagli=crmForm.all.productid.DataValue;
crmForm.all.new_nome_prodotto.DataValue = dettagli[0].name;
crmForm.all.new_id_prodotto.DataValue = dettagli[0].id;
crmForm.all.new_codice_prodotto.DataValue = dettagli[0].produktnumber;
crmForm.all.new_descrizione_prodotto.DataValue = dettagli[0].description;
}
Qualcuno mi può aiutare?
Risposte
-
dal codice che hai scritto presumo che sia un CRM 4.0 ma potrebbe anche essere CRM 2011.
il DataValue di un campo lookup contiene solo id, name e il tipo dell'entità.
Per recuperare altri campi devi fare una chiamata al webservice, puoi prendere spunto da questo codice:function test() { var prodotto = crmForm.all.productid.DataValue; var id = prodotto[0].id; var entityname = "product"; var attribute1 = "productnumber"; var attribute2 = "description"; 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>" + "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" + "<entityName>" + entityname + "</entityName>" + "<id>" + id + "</id>" + "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" + "<q1:Attributes>" + "<q1:Attribute>" + attribute1 + "</q1:Attribute>" + "<q1:Attribute>" + attribute2 + "</q1:Attribute>" + "</q1:Attributes>" + "</columnSet>" + "</Retrieve>" + "</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/Retrieve"); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.setRequestHeader("Content-Length", xml.length); xmlHttpRequest.send(xml); var result = xmlHttpRequest.responseXML; var error = result.selectNodes('//error').length; if (error == 0) { var attributeNode1 = result.selectSingleNode('//q1:' + attribute1); if (attributeNode1 != null) { alert(attributeNode1.text); } var attributeNode2 = result.selectSingleNode('//q1:' + attribute2); if (attributeNode2 != null) { alert(attributeNode2.text); } } }
recupera productnumber e description e lancia un alert se valorizzati.
- Proposto come risposta Guido PreiteMVP lunedì 21 gennaio 2013 11:38
- Contrassegnato come risposta Giorgio Garcia-AgredaModerator sabato 26 gennaio 2013 08:27
- Contrassegno come risposta annullato Alessandro Datef martedì 19 marzo 2013 17:16
- Contrassegnato come risposta Alessandro Datef martedì 19 marzo 2013 20:35
-
Ciao Alessandro,
puoi usare la funzione parseFloat in questo modo:Xrm.Page.getAttribute("new_prezzoacq").setValue(parseFloat(attributeNode1));
My blog: www.crmanswers.net
- Proposto come risposta Guido PreiteMVP martedì 19 marzo 2013 17:50
- Contrassegnato come risposta Alessandro Datef martedì 19 marzo 2013 20:35
- Modificato Guido PreiteMVP mercoledì 20 marzo 2013 07:25
Tutte le risposte
-
dal codice che hai scritto presumo che sia un CRM 4.0 ma potrebbe anche essere CRM 2011.
il DataValue di un campo lookup contiene solo id, name e il tipo dell'entità.
Per recuperare altri campi devi fare una chiamata al webservice, puoi prendere spunto da questo codice:function test() { var prodotto = crmForm.all.productid.DataValue; var id = prodotto[0].id; var entityname = "product"; var attribute1 = "productnumber"; var attribute2 = "description"; 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>" + "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" + "<entityName>" + entityname + "</entityName>" + "<id>" + id + "</id>" + "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" + "<q1:Attributes>" + "<q1:Attribute>" + attribute1 + "</q1:Attribute>" + "<q1:Attribute>" + attribute2 + "</q1:Attribute>" + "</q1:Attributes>" + "</columnSet>" + "</Retrieve>" + "</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/Retrieve"); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.setRequestHeader("Content-Length", xml.length); xmlHttpRequest.send(xml); var result = xmlHttpRequest.responseXML; var error = result.selectNodes('//error').length; if (error == 0) { var attributeNode1 = result.selectSingleNode('//q1:' + attribute1); if (attributeNode1 != null) { alert(attributeNode1.text); } var attributeNode2 = result.selectSingleNode('//q1:' + attribute2); if (attributeNode2 != null) { alert(attributeNode2.text); } } }
recupera productnumber e description e lancia un alert se valorizzati.
- Proposto come risposta Guido PreiteMVP lunedì 21 gennaio 2013 11:38
- Contrassegnato come risposta Giorgio Garcia-AgredaModerator sabato 26 gennaio 2013 08:27
- Contrassegno come risposta annullato Alessandro Datef martedì 19 marzo 2013 17:16
- Contrassegnato come risposta Alessandro Datef martedì 19 marzo 2013 20:35
-
-
OK Grazie! sono riuscito a farlo funzionare ho dovuto solo modificare alla riga 2 la sintassi dper il recupero dei dati del record "productid" a causa dell'incompatibilità dell'istruzione "crmForm" dopo l'utimo rollup.
var prodotto = Xrm.Page.getAttribute("productid").getValue();
Alessandro Cosi Datef SPA
- Modificato Alessandro Cosi martedì 29 gennaio 2013 15:10
-
Ciao Guido, ti ringrazio ho adattato e utilizzato con successo le righe di jscript che mi hai mandato,
ora ho la necessità di lavorare con un campo numerico che contiene un prezzo.
Il campo nell'entità esterna alla funzione è di tipo "prezzo" ma l'istruzione:
var attributeNode1 = result.selectSingleNode('//q1:' + attribute1);
mi ritorna un campo testo che non riesco a scrivere con la seguente istruzione in un campo numerico:
Xrm.Page.getAttribute("new_prezzoacq").setValue(attributeNode1);
come posso convertirlo?
- Modificato Alessandro Datef martedì 19 marzo 2013 17:51 modificato per maggiore chiarezza
-
Ciao Alessandro,
puoi usare la funzione parseFloat in questo modo:Xrm.Page.getAttribute("new_prezzoacq").setValue(parseFloat(attributeNode1));
My blog: www.crmanswers.net
- Proposto come risposta Guido PreiteMVP martedì 19 marzo 2013 17:50
- Contrassegnato come risposta Alessandro Datef martedì 19 marzo 2013 20:35
- Modificato Guido PreiteMVP mercoledì 20 marzo 2013 07:25
-
-
si tutto ok, contento che hai risolto!
My blog: www.crmanswers.net