
Conteggio degli impegni, post e note presenti nel Riquadro Social
- On 29 Maggio, 2019
- crm, dynamics, dynamics 365, itconsulting, pmi
se siete degli utilizzatori del Riquadro Social potrebbe tornarvi utile questa semplice funzione Javascript per visualizzare il conteggio degli Impegni, dei Post e delle Note nel vostro riquadro. Infatti per conoscere ad esempio gli impegni di un Contatto oppure di un Account, specie se numerosi, sareste costretti a scorrere verso il basso il form e, se gli impegni sono tanti, ci mettereste un bel po’. Se poi vi servisse avere anche il numero dei post e delle note dovreste spostarvi su tre tab diversi per ripetere la stessa operazione.
Vediamo invece come ottenere sul form, magari subito sopra il riquadro, la somma di ciascuno dei tre elementi visualizzati in un colpo solo, all’apertura della maschera.
Per effettuare il conteggio utilizzeremo delle fetchXML, una per ogni voce, utilizzando come chiave l’id del contatto. Partiamo da quella dei Post :
var fetchXmlCount = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'> "+
"<entity name='post'>" +
"<attribute name='postid' alias='recordcount' aggregate='count' /> "+
"<filter type='and'> "+
"<condition attribute='source' operator='eq' value='2' /> "+
"<condition attribute='regardingobjecttypecode' operator='eq' value='2' /> "+
"<condition attribute='regardingobjectid' operator='eq' value='" + contactId + "' /> "+
"</filter>" +
"</entity>" +
"</fetch>";
In questo esempio ho filtrato solo i post inseriti manualmente ( campo ‘source’= 2) secondo la seguente tabella :
Questa di seguito invece è la fetchXML delle Note :
var fetchXmlCount2 = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'> "+
"<entity name='annotation'>" +
"<attribute name='subject' alias='recordcount' aggregate='count' /> "+
"<link-entity name='contact' from='contactid' to='objectid' alias='ab'> "+
"<filter type='and'> "+
"<condition attribute='contactid' operator='eq' value='" + contactId + "' /> "+
"</filter>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
Infine di seguito, la fetchXML riferita al conteggio degli Impegni :
var fetchXmlCount3 = "<fetch distinct='false' aggregate='true' mapping='logical' output-format='xml-platform' version='1.0'> "+
"<entity name='activitypointer'> "+
"<attribute name='subject' alias='recordcount' aggregate='count'/> "+
"<filter type='and'> "+
"<condition attribute='statecode' value='0' operator='eq'/> "+
"</filter> " +
"<link-entity name='contact' alias='ad' to='regardingobjectid' from='contactid'> "+
"<filter type='and'> "+
"<condition attribute='contactid' value='" + contactId + "' operator='eq' uitype='contact' /> "+
"</filter> "+
"</link-entity> "+
"</entity> "+
"</fetch> ";
Per visualizzare i tre valori conteggiati in corrispondenza delle voci nel pannello Social, ho utilizzato un piccolo trucco : invece di creare tre campi diversi, ho creato un solo campo, e modificato la label del campo con i valori ottenuti. In questo modo evito che mi venga chiesto ogni volta di salvare il form (magari facendo scattare inutilmente dei plugin associati) e posso posizionare la label giusto nel punto dove mi serve. Infine ho impostato il campo in sola lettura in modo che anche con il tab non sarà possibile passarci sopra. L’istruzione in javascript per ottenere questo è la seguente :
Xrm.Page.getControl(‘new_numpostnote’).setLabel(newNumpostnote);
dove ‘new_numpostnote’ è il nome del campo creato nell’entità Contatto, e newNumpostnote è la variabile javascript impostata e formattata con i valori calcolati :
var newNumpostnote = FormatNumberLength(countPostValue,4)+"\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"+FormatNumberLength(countImpegniValue,4)+"\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"+FormatNumberLength(countNoteValue,4);
Ho utilizzato la funzione FormatNumberLenght(numero,4) per formattare il numero ed inserito dei caratteri di controllo per ottenere l’opportuna spaziatura.
Le funzioni javascript complete sono queste :
function contaPostNote() {
var contactId = Xrm.Page.data.entity.getId();
var fetchXmlCount = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’ aggregate=’true’> “+
“<entity name=’post’>” +
“<attribute name=’postid’ alias=’recordcount’ aggregate=’count’ /> “+
“<filter type=’and’> “+
“<condition attribute=’source’ operator=’eq’ value=’2′ /> “+
“<condition attribute=’regardingobjecttypecode’ operator=’eq’ value=’2′ /> “+
“<condition attribute=’regardingobjectid’ operator=’eq’ value='” + contactId + “‘ /> “+
“</filter>” +
“</entity>” +
“</fetch>”;
var postRecords = XrmServiceToolkit.Soap.Fetch(fetchXmlCount);
var countPostValue = (postRecords[0].attributes.recordcount.value);
var fetchXmlCount2 = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’ aggregate=’true’> “+
“<entity name=’annotation’>” +
“<attribute name=’subject’ alias=’recordcount’ aggregate=’count’ /> “+
“<link-entity name=’contact’ from=’contactid’ to=’objectid’ alias=’ab’> “+
“<filter type=’and’> “+
“<condition attribute=’contactid’ operator=’eq’ value='” + contactId + “‘ /> “+
“</filter>” +
“</link-entity>” +
“</entity>” +
“</fetch>”;
var noteRecords = XrmServiceToolkit.Soap.Fetch(fetchXmlCount2);
var countNoteValue = (noteRecords[0].attributes.recordcount.value);
var fetchXmlCount3 = “<fetch distinct=’false’ aggregate=’true’ mapping=’logical’ output-format=’xml-platform’ version=’1.0′> “+
“<entity name=’activitypointer’> “+
“<attribute name=’subject’ alias=’recordcount’ aggregate=’count’/> “+
“<filter type=’and’> “+
“<condition attribute=’statecode’ value=’0′ operator=’eq’/> “+
“</filter> ” +
“<link-entity name=’contact’ alias=’ad’ to=’regardingobjectid’ from=’contactid’> “+
“<filter type=’and’> “+
“<condition attribute=’contactid’ value='” + contactId + “‘ operator=’eq’ uitype=’contact’ /> “+
“</filter> “+
“</link-entity> “+
“</entity> “+
“</fetch> “;
var impRecords = XrmServiceToolkit.Soap.Fetch(fetchXmlCount3);
var countImpegniValue = (impRecords[0].attributes.recordcount.value);
var newNumpostnote = FormatNumberLength(countPostValue,4)+”\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0″+FormatNumberLength(countImpegniValue,4)+”\xa0\xa0″+
“\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0″+FormatNumberLength(countNoteValue,4);
Xrm.Page.getControl(‘new_numpostnote’).setLabel(newNumpostnote);
}
function FormatNumberLength(num, length) {
var r = "" + num;
while (r.length < length) {
r = " " + r;
}
return r;
}
Non resta che inserire le due funzioni in una libreria javascript, oppure aggiungerle alla vostra, e richiamare la funzione contaPostNote nell’OnLoad del form del Contatto :
Happy Coding con Dynamics CRM!
Davide Matichecchia
0 Comments