locked
Get the the list of all entities which have "Notes" section enabled in JavaScript for CRM Online RRS feed

  • Question

  • Hi, I want to get the count of all attachments for per entity (e.g. account, contact, etc.) in JavaScript for CRM Online. For that, I need the the list of all entities which have "Notes" section enabled. 

    I have tried this using-

    SDK.Metadata.RetrieveAllEntities(SDK.Metadata.EntityFilters.Entity,
    false,
    successRetrieveAllEntities,
    errorRetrieveAllEntities,
    null);
    But, I did not find any property to check if "Notes" section is enabled or not.
    Is there any way to solve this problem?

    Thursday, April 30, 2015 11:35 AM

Answers

  • Check Guido's answer, it is C# but you can do the same with JS. In the successRetrieveAllEntities you could do something like:

    function successRetrieveAllEntities(entityMetadataCollection){
    		var hasNotesEnabledEntities = [];
    		
    		for(var i=0; i< entityMetadataCollection.length; i++){
    			SDK.Metadata.RetrieveEntity(
    				SDK.Metadata.EntityFilters.Relationships,
    				entityMetadataCollection[i].LogicalName,
    				null,
    				false,
    				function(entityMetadata){
    					if(hasNotesEnabled(entityMetadata))
    						hasNotesEnabledEntities.push({logicalName: entityMetadata.LogicalName});
    				},
    				errorRetrieveEntityRelationships
    			);
    		}
    	},
    	
    	function hasNotesEnabled(entityMetadata){
    		for(var i = 0; i< entityMetadata.OneToManyRelationships.length; i++){
    			if(entityMetadata.OneToManyRelationships[i].ReferencingEntity == "annotation")
    				return true;
    		}
    	}

    • Marked as answer by P H S Tuesday, May 5, 2015 12:35 PM
    Tuesday, May 5, 2015 8:53 AM

All replies

  • hi Purva,

    have you tried using a fetchxml to see if there are attachments?

    a query like this may return the attachments. Field objectid will be same across contact, account etc...

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
      <entity name="annotation">
        <attribute name="subject" />
        <attribute name="notetext" />
        <attribute name="filename" />
        <attribute name="annotationid" />
        <order attribute="subject" descending="false" />
        <link-entity name="account" from="accountid" to="objectid" alias="ad"></link-entity>
      </entity>
    </fetch>

    Thursday, April 30, 2015 11:10 PM
  • Thanks Jithesh.
    I am using the same fetch-xml. But, I want to pass the link-entity name (say "account") and "from attribute value (say"accountid") dynamically. For that I need the list of all such entities (like account) for which "Notes" section is enabled. Is there any way to get all such entities?
    Tuesday, May 5, 2015 6:01 AM
  • Hi,

    retrieve entity metadata and check with relationship annotation.

    Thanks

    Laxman P 

    Tuesday, May 5, 2015 6:44 AM
  • the same question has been asked on StackOverflow and got a nice answer

    http://stackoverflow.com/questions/29768264/is-it-possible-to-check-if-an-entity-is-activity-enabled-in-dynamics-crm-progr/

    Instead of checking a relationship to activitypointer, need to check a relationship with annotation


    My blog: www.crmanswers.net - Rockstar 365 Profile


    Tuesday, May 5, 2015 7:21 AM
  • Purva,

    You can create your fetch XML dynamically. Retrieve all entities metadata, and build the fetch xml dynamically with the logical name and the primary id attribute. Loop all entities and check for notes with your fetch xml.

    function retrieveAllEntitiesMetadata(){
    		SDK.Metadata.RetrieveAllEntities(
    			SDK.Metadata.EntityFilters.Entity,
    			false,
    			successRetrieveAllEntities,
    			errorRetrieveAllEntities
    		);
    	},
    	
    	function successRetrieveAllEntities(entityMetadataCollection){
    		var hasNotesCollection = [];
    		
    		for(var i=0; i< entityMetadataCollection.length; i++){
    			var logicalName = entityMetadataCollection[i].LogicalName;
    			var primaryIdAttribute = entityMetadataCollection[i].PrimaryIdAttribute;
    			
    			//Do Query with fetch xml to check if notes | get the fetch xml: notesFetchXML(logicalName,primaryIdAttribute)
    			
    			
    			hasNotesCollection.push({logicalname: logicalName //,...}
    		}
    		
    		alert("succes");
    	},
    	
    	function errorRetrieveAllEntities(){
    		alert("error");
    	},
    	
    	function notesFetchXML(logicalname, primaryidattribute){
    		return "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    				  <entity name='annotation'>
    				    <attribute name='subject' />
    				    <attribute name='notetext' />
    				    <attribute name='filename' />
    				    <attribute name='annotationid' />
    				    <order attribute='subject' descending='false' />
    				    <link-entity name='" + logicalname + "' from='" + primaryidattribute + "' to='objectid' alias='ad'></link-entity>
    				  </entity>
    				</fetch>"
    	},

    And Guido, your solution is in C# not Javascript.

    Kind Regards

    Tuesday, May 5, 2015 7:27 AM
  • Hi Jochen,

    first that FetchXML approach will not work, because if there isn't an note attached to a specific entity (let's say that there are no notes for accounts) the FetchXML will return 0 making the wrong assumption that the entity is not enabled for notes.

    second, the correct way is to query the metadata and check if there is a relationship with annotation, as stated in that StackOverflow answer (in StackOverflow example it checks for Activities to it uses activitypointer)

    Yes, it's in C# and not in JavaScript.


    My blog: www.crmanswers.net - Rockstar 365 Profile


    Tuesday, May 5, 2015 7:40 AM
  • Guido,

    You are right. I based my answer on the previous answers.

    Checked if an object in the EntityMetadataCollection got a property 'IsNotesEnabled' or something but no. The way to do it is your way via relationships. One remark, to check for notes on an entity, shouldn't you check the relationship with referencingentity annotation, not activitypointer? 

    kind regards

    Tuesday, May 5, 2015 8:31 AM
  • Hi Jochen,

    you are correct, it's necessary to check a relationship with annotation, the StackOverflow example checks for the Activities checkbox.

    I corrected my previous answers.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, May 5, 2015 8:44 AM
  • Thanks Jochen. After retrieving all the entities, is there any way to check if an entity has 1:N relationship with "Notes (annotation)" entity? Or does "Notes (annotation)" entity have N:1 relationship with the entity?
    Tuesday, May 5, 2015 8:49 AM
  • Check Guido's answer, it is C# but you can do the same with JS. In the successRetrieveAllEntities you could do something like:

    function successRetrieveAllEntities(entityMetadataCollection){
    		var hasNotesEnabledEntities = [];
    		
    		for(var i=0; i< entityMetadataCollection.length; i++){
    			SDK.Metadata.RetrieveEntity(
    				SDK.Metadata.EntityFilters.Relationships,
    				entityMetadataCollection[i].LogicalName,
    				null,
    				false,
    				function(entityMetadata){
    					if(hasNotesEnabled(entityMetadata))
    						hasNotesEnabledEntities.push({logicalName: entityMetadata.LogicalName});
    				},
    				errorRetrieveEntityRelationships
    			);
    		}
    	},
    	
    	function hasNotesEnabled(entityMetadata){
    		for(var i = 0; i< entityMetadata.OneToManyRelationships.length; i++){
    			if(entityMetadata.OneToManyRelationships[i].ReferencingEntity == "annotation")
    				return true;
    		}
    	}

    • Marked as answer by P H S Tuesday, May 5, 2015 12:35 PM
    Tuesday, May 5, 2015 8:53 AM
  • Thanks Jochen for the help, it worked.
    Tuesday, May 5, 2015 12:35 PM
  • Thanks Jochen for the help, it worked.
    And Guido, I based my answer on his :)
    Wednesday, May 6, 2015 11:23 AM