Answered by:
Count number of child records using jscript

Question
-
Hi all,
Is there anyway to count number of child records of an entity using javacsript ??
thanks in advance..
Wednesday, May 25, 2011 1:05 PM
Answers
-
Hi,
You can execute the FetchXml using JScript to get the record counts, sample FetchXml is:
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='entity name'>
<attribute name='attribute name' aggregate='count' alias='alias name'/>
</entity>
</fetch>"
Thank You, Jehanzeb Javeed, http://worldofdynamics.blogspot.com Linked-In Profile | CodePlex Profile- Proposed as answer by Jehanzeb.Javeed Wednesday, May 25, 2011 1:26 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 1:36 PM
- Edited by Jehanzeb.Javeed Wednesday, May 25, 2011 1:49 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:45 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:22 PM -
Yes, it's not really pretty though. If you execute a retrievemultiple call on the records and then parse and count the results.
Here are some related posts for this sort of thing.
http://mileyja.blogspot.com/2011/03/crm-2011-retrievemultiple-calls-in.html (jscript retrievemultiple example)
http://mileyja.blogspot.com/2011/04/how-to-use-retrieve-messages-in-jscript.html (jscript retrieve example, you could use this to get all child records along with the main record data at the same time, just another option)
http://mileyja.blogspot.com/2011/03/microsoft-dynamics-crm-2011-parsing.html (parsing results using DOM parser in jscript)
http://mileyja.blogspot.com/2011/05/debugging-jscript-in-microsoft-dynamics.html (debugging jscript)
I hope this all helps!
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:45 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:25 PMModerator -
Use Aggregate in fetchxml
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:46 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:43 PM -
Hi Mostafa,
You cannot count without doing a RetrieveMultiple, but if you are only interested i nthe number of records you can really simplify your RetrieveMultiple
<fetch mapping='logical' count='25' returntotalrecordcount='true'> <entity name='account' > <filter> <condition attribute='parentaccountid' operator='equals' value='.....' /> </filter> </entity> </fetch>
Gonzalo | gonzaloruizcrm.blogspot.com
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:46 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:44 PMModerator -
You can use the following sample FetchXml for getting the total account in CRM:
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='accountid' aggregate='count' alias='ToalAccounts'/>
<filter>
<condition attribute='parentId' operator='equals' value='guid of parent record' />
</filter></entity>
</fetch>"
Thank You, Jehanzeb Javeed, http://worldofdynamics.blogspot.com Linked-In Profile | CodePlex Profile- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:46 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:51 PM -
Here is the full answer to this by the way including the jscript. It uses the fetch xml above but forms up the jscript retrievemultiple query with it also.
http://mileyja.blogspot.com/2011/06/getting-row-count-or-entity-count-in.html
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Thursday, June 2, 2011 1:26 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:45 PM
Thursday, June 2, 2011 1:26 PMModerator
All replies
-
Hi,
You can execute the FetchXml using JScript to get the record counts, sample FetchXml is:
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='entity name'>
<attribute name='attribute name' aggregate='count' alias='alias name'/>
</entity>
</fetch>"
Thank You, Jehanzeb Javeed, http://worldofdynamics.blogspot.com Linked-In Profile | CodePlex Profile- Proposed as answer by Jehanzeb.Javeed Wednesday, May 25, 2011 1:26 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 1:36 PM
- Edited by Jehanzeb.Javeed Wednesday, May 25, 2011 1:49 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:45 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:22 PM -
Here is a nice article to execute FetchXml using JScript.
http://crmbusiness.wordpress.com/2011/05/12/crm-2011-how-to-execute-fetch-xml-in-javascript/
Thank You, Jehanzeb Javeed, http://worldofdynamics.blogspot.com Linked-In Profile | CodePlex ProfileWednesday, May 25, 2011 1:23 PM -
Yes, it's not really pretty though. If you execute a retrievemultiple call on the records and then parse and count the results.
Here are some related posts for this sort of thing.
http://mileyja.blogspot.com/2011/03/crm-2011-retrievemultiple-calls-in.html (jscript retrievemultiple example)
http://mileyja.blogspot.com/2011/04/how-to-use-retrieve-messages-in-jscript.html (jscript retrieve example, you could use this to get all child records along with the main record data at the same time, just another option)
http://mileyja.blogspot.com/2011/03/microsoft-dynamics-crm-2011-parsing.html (parsing results using DOM parser in jscript)
http://mileyja.blogspot.com/2011/05/debugging-jscript-in-microsoft-dynamics.html (debugging jscript)
I hope this all helps!
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:45 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:25 PMModerator -
Dears,
I really appreciate all your fast responses, but I don't need to retrieve any data from related records, i just need to count i.e. i need to return a number....
thanks
Wednesday, May 25, 2011 1:30 PM -
Use Aggregate in fetchxml
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:46 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:43 PM -
Hi,
Using the FetchXml, RetrieveMultiple execution request you will be only able to retrieve the total number of records count.
Thank You, Jehanzeb Javeed, http://worldofdynamics.blogspot.com Linked-In Profile | CodePlex ProfileWednesday, May 25, 2011 1:43 PM -
Hi Mostafa,
You cannot count without doing a RetrieveMultiple, but if you are only interested i nthe number of records you can really simplify your RetrieveMultiple
<fetch mapping='logical' count='25' returntotalrecordcount='true'> <entity name='account' > <filter> <condition attribute='parentaccountid' operator='equals' value='.....' /> </filter> </entity> </fetch>
Gonzalo | gonzaloruizcrm.blogspot.com
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:46 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:44 PMModerator -
You can use the following sample FetchXml for getting the total account in CRM:
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='accountid' aggregate='count' alias='ToalAccounts'/>
<filter>
<condition attribute='parentId' operator='equals' value='guid of parent record' />
</filter></entity>
</fetch>"
Thank You, Jehanzeb Javeed, http://worldofdynamics.blogspot.com Linked-In Profile | CodePlex Profile- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 2:12 PM
- Unproposed as answer by Mostafa Moatassem Wednesday, May 25, 2011 3:46 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, May 25, 2011 5:58 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:44 PM
Wednesday, May 25, 2011 1:51 PM -
Gonzalo,
This will return all attributes from all selected records. Plus the header will contain the recordcount. You should use Aggregate.
Wednesday, May 25, 2011 1:51 PM -
Dear fellows-answerers,
I follow topics of this author for several months. Believe me - he would not try to develop any code or try something before you will provide full instructions and completed code (may be with screenshots ;) ). Something similar - doesn't work for him. So you should not waste time trying to answer... It is not worth. Just believe me.
UPD: bingo! http://jianwang.blogspot.com/2008/02/show-how-many-activitieshistory.html this worked! In this thread - http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/d049d805-d321-4086-a521-9f029e16e180/
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)Wednesday, May 25, 2011 2:06 PMModerator -
Dear all,
sorry for disturbing you, I've finally solved my problem.
Wednesday, May 25, 2011 2:06 PM -
I am curious, could you please post the answer?
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!Wednesday, June 1, 2011 1:13 PMModerator -
I am curious, could you please post the answer?
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!Hello Jamie,
It is impossible. The reason you can see here - http://social.microsoft.com/Forums/en-US/crm/thread/4cf18674-9081-44df-871d-863458592ff1 Mostafa is busy and he has time only for asking and not for marking ;)
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)Wednesday, June 1, 2011 1:55 PMModerator -
Here is the full answer to this by the way including the jscript. It uses the fetch xml above but forms up the jscript retrievemultiple query with it also.
http://mileyja.blogspot.com/2011/06/getting-row-count-or-entity-count-in.html
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Thursday, June 2, 2011 1:26 PM
- Marked as answer by Andrii ButenkoMVP, Moderator Friday, June 10, 2011 5:45 PM
Thursday, June 2, 2011 1:26 PMModerator