locked
Retrieve Values from Custom entities using CRM 4 plugin RRS feed

  • Question

  • Dear all,

    assuming the below CRM 4 plugin:

     

    using System;
    using System . Collections . Generic;
    using System . Linq;
    using System . Text;
    
    using Microsoft . Crm . Sdk;
    using Microsoft . Crm . SdkTypeProxy;
    using Microsoft . Crm . Sdk . Query;
    using System . Web . Services . Protocols;
    
    namespace UpdateCustomEntity
    {
     public class Update:IPlugin
     {
    
      #region IPlugin Members
    
      public void Execute ( IPluginExecutionContext context )
      {
       try
       {
        ICrmService service = context . CreateCrmService ( false );
        
        //retrieve from custom entity////////////////////////////////////////////////////////////////////////////////
        ConditionExpression con = new ConditionExpression ( );
        con . AttributeName = "name";
        con . Operator = ConditionOperator . Equal;
        con . Values = new string [ ] { "name1" };
        FilterExpression filter = new FilterExpression ( );
        filter . FilterOperator = LogicalOperator . And;
        filter . AddCondition ( con );
        QueryExpression query = new QueryExpression ( );
        query . EntityName = "new_entity";
        query . ColumnSet = new ColumnSet ( new string [ ] { "new_field" } );
    
        
    
        query . Criteria = filter;
        RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest ( );
        retrieve . Query = query;
        retrieve . ReturnDynamicEntities = true;
        RetrieveMultipleResponse retrieved = ( RetrieveMultipleResponse ) service . Execute ( retrieve );
    
        DynamicEntity entity = ( DynamicEntity ) retrieved . BusinessEntityCollection . BusinessEntities [ 0 ];
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        //update quote
        DynamicEntity myDEUpdate = new DynamicEntity();
    
        myDEUpdate.Name = "entity2";
    
        //Create a KeyProperty to hold the guid of the record to be updated
    
        KeyProperty myentity2Guid = new KeyProperty ( );
    
        myentity2Guid . Name = "entity2id";
    
        Key myentity2Key = new Key ( );
    
        myentity2Key . Value = new Guid ( "D38341FD-F45E-4AF1-92EE-64AD64E479D9" );
    
        myentity2Guid . Value = myentity2Key;
    
        myDEUpdate . Properties . Add ( myentity2Guid );
    
        //Create a StringProperty with the new updated value
    
        CrmMoneyProperty field2 = new CrmMoneyProperty ( );
        field2 . Name = "field2";
        field2 . Value = new CrmMoney ( );
        field2 . Value . Value = 0.13M;
    
        myDEUpdate . Properties . Add (field2 );
    
        service.Update(myDEUpdate);
       }
       catch ( Exception e )
       {
        throw new NotImplementedException ( e . Message , e );
       }
       //catch ( SoapException e )
       //{
       // throw new Exception ( e . Detail . InnerText );
       //}
      }
    
      #endregion
     }
    }
    

    this plugin should retrieve values from new_entity and use them to update ALL records of entity 2,

    Would you please help me get the retrieved value in a variable ??

    In addition I've tried to remove the below code block so I can update all records of entity2:

    //Create a KeyProperty to hold the guid of the record to be updated
    
        KeyProperty myentity2Guid = new KeyProperty ( );
    
        myentity2Guid . Name = "entity2id";
    
        Key myentity2Key = new Key ( );
    
        myentity2Key . Value = new Guid ( "D38341FD-F45E-4AF1-92EE-64AD64E479D9" );
    
        myentity2Guid . Value = myentity2Key;
    
        myDEUpdate . Properties . Add ( myentity2Guid );


    But it didn't work, could anybody please guide me to the correct way to do it

     

    thanks in advance...

     


    Thursday, June 16, 2011 5:20 PM

Answers

  • Dear Mostafa,

    First you can update all reacord in one go you have to use key attribute to update records one by one.

    you can access perticular property like below

      DynamicEntity entity = ( DynamicEntity ) retrieved . BusinessEntityCollection . BusinessEntities [ 0 ];
      if (entity .Properties.Contains("new_field"))
                     {

                           //fetch value here based on datatype
                     }

     

    for example if you field is of number type you can do it like below

      if (entity.Properties.Contains("new_field"))
                     {


                         int New_number = ((CrmNumber)entity.Properties["new_field"]).Value;
                                      }


    Mahain : My Dynamics CRM Blog
    Friday, June 17, 2011 5:09 AM
    Moderator

All replies

  • any suggestions ??
    Thursday, June 16, 2011 10:06 PM
  • Dear Mostafa,

    First you can update all reacord in one go you have to use key attribute to update records one by one.

    you can access perticular property like below

      DynamicEntity entity = ( DynamicEntity ) retrieved . BusinessEntityCollection . BusinessEntities [ 0 ];
      if (entity .Properties.Contains("new_field"))
                     {

                           //fetch value here based on datatype
                     }

     

    for example if you field is of number type you can do it like below

      if (entity.Properties.Contains("new_field"))
                     {


                         int New_number = ((CrmNumber)entity.Properties["new_field"]).Value;
                                      }


    Mahain : My Dynamics CRM Blog
    Friday, June 17, 2011 5:09 AM
    Moderator
  • Dear Mahendar,

     

    so how what you suggest to do with my bulk update problem ??

     

    best regards..

    Friday, June 17, 2011 10:03 AM
  • Call update statement in loop to update multiple records if you want to do it through SDK, or use bulk update tool.
    Mahain : My Dynamics CRM Blog

    Friday, June 17, 2011 10:10 AM
    Moderator
  • Dear Mahendar,

     

    I cannot use the Bulk Update Tool cause I need to update records at runtime, would you please help me developing this loop ??

    I have tried something like:

     


    field2 . Value . Value = 0.13M;
    
    while(myentity2.Value!=null)
    {
              field2 . Value . Value = "somevalue";
    
              myDEUpdate . Properties . Add ( field2 );
    
              service . Update ( myDEUpdate ); 
    }
    

    but it didn't work

     

    thanks in advance..

    Sunday, June 19, 2011 10:00 AM
  • any suggestions ??
    Sunday, June 19, 2011 1:25 PM
  • Dear Mostafa,

    Let's say you want to update address1_line1 field for multiple contacts, let's say 10, so need to use something like below, you should have array of guid that you want to update

    for(int i=0; i<10; i++){

    contact contact = new contact();
    contact.address1_line1 = "This is Test";
    contact.contactid = new Key();
    contact.contactid.Value = new Guid(GUIDArray[i]);
    service.Update(contact);

    }


    Mahain : My Dynamics CRM Blog
    Sunday, June 19, 2011 4:51 PM
    Moderator