locked
New user to crm, need some basic questions answered RRS feed

  • Question


  • I'm a fairly good user of C# but I'm new to CRM and the SDK. As a result, I'm on a mission to learn as much as I can to help me so I have these questions:


    1) How can I pass parameters into an event handler? I'm using .NET 1.1. The prob;em is having an event handler which calls a function which takes parameters.

    2) I need to retrieve columns from 2 entities - contact (multiple) and account (single). What I need to do is write these to a csv file. I can open the file but, as I have stored each column attribute into a string variable (all columns retrieved being of type string), the file just says system.string[] (the type).

    How can I actually write the real value of each attribute into the file (ie for phone nu mber, 020348384 or whatever it may be). As I store the columnset into a string variable I only get the methods and properties of a string. I'm fine with the streamwriter part of this, but not the retrieval.

    I know this is to do with using the businessentitycollection object and iterating through this but despite everything I've tried I've been unsuccessful.

    3) In a retrieve single, I need to set the GUID. Problem is, the ID for each entity is of type Key but I want a GUID. How can I successfully cast this into GUID and then pass it into the retrieve statement? I know it's all to do with casting but this is one of my weak areas.

    I keep getting object reference not set to an instance of an object errors. Do I also have to pass the var as a function parameter? Which brings me back nicely to point 1...

    I will post code samples up to aid these questions, soon.

    Thanks
    Friday, February 8, 2008 8:47 PM

Answers

  • Hi.

     

    foreach( BusinessEntity entity in entities.BusinessEntities )
    {
                contact Contact = entity as contact;
                //Print Lookup Elements Actual Values

                Console.WriteLine( Contact.parentcustomerid.name); // parent account name actual value

                //Print String Elements Actual Values.

                Console.WriteLine( Contact.fullname );

    }

     

    Cheers,

    Adi.
    Monday, February 11, 2008 6:00 PM

All replies

    1. This is a .net question but anyway….
      1. Create a class with public class level fields.
      2. Create your method inside that class.
      3. Create and instance of the class.
      4. populate the public fields with relevant values
      5. attach to the event handler using instance.method
      6. Now you can refer to the fields from your method.

     

    1. There is an easier way to do that witch does not include coding. You can simply Export to CSV from enterprise manager using the filtered views supplied in the company_mscrm db.

    1. key.Value is of type GUID. 

    Hope this helps.

    Adi

     

     

    Saturday, February 9, 2008 1:56 AM
  • 1. Is there an example of points 4, 5 and 6? I assume 4 is the overloading but an example would be great.

    2. This is a work project so has to be coded.

    3. In a retrieve single, how could I pass in accountid if it's already of type GUID? The object browser tells me it's not. Also, I can't specify account.value, I have to give the full path, so account.value.tostring().
    Saturday, February 9, 2008 1:38 PM
  • Hi.

     

    Please put your code examples here.

    It will be easier to debug and will give me better understanding of what you are trying to achieve

     

    Cheers,

    Adi

     

     

    Sunday, February 10, 2008 8:17 AM
  • I have a retrieve statement which retrieves an entity from crm and takes a Guid. Problem is, the unique ID for the entity is a Key field and I need to pass this as a Guid. The key field can be stored as a string variable and be written as string AcID = accountid.Value.ToString();

    The Guid object has an overload which accepts a string overload so I can pass the AcID variable in.

    But in the retrieve statement, which basically includes the entity, columns to retrieve, and UID, it won't take the string. I get errors such as cannot convert from one type to another, implicitly or non-implicitly, and so forth.

    I stored the field as a string var
    But what I need to do is convert object Key into Guid - how? Basically regardless of types, the accountid has to be passed into the retrieve single (contacts being retrieve multiple, and each contact belongs to one account, which is a retrieve single).

    Then I can call it in the retrieve statement



    The CSV file writing functionality has a function. I use a business entity collection which contains all the business entities. Properties include:

    BusinessEntities
    EntityName
    MoreRecords
    PagingCookie

    Casting looks like this:

    contact ContactsEC = (contact) BEC.BusinessEntities[Cntr];

    This code comes from the example of looping through the collection and casting to contact. What I need to do is retrieve all the contact attributes and write them to a file. I need a do while loop and to cast the collection to contact but I keep getting errors @ runtime. I have the methods to write to file.

    Example of code:


    //Populate the business entity collection, and then loop through it

    //Instantiate the BusinessEntity Collection here
    BusinessEntityCollection BEC = new BusinessEntityCollection();


    //Columnset is instantiated here
    ColumnSet cols = new ColumnSet();

    //Setup a counter
    int Incrementor = 0; Incrementor++;

    //Loop through the Business Entity Collection




    int Cntr = 0; Cntr++;

    do

    {




    CrmService serv = new HGCSV.mscrmredep.CrmService();
    serv.Credentials = System.Net.CredentialCache.DefaultCredentials;


    cols.Attributes = new string[] {"address1_addresstypecode", "address1_city", "address1_county", "address1_country", "address1_fax", "address1_line1", "address1_line2", "address1_line3", "address1_name", "address1_postalcode", "address1_postofficebox", "address1_primarycontactname",
    "address1_stateorprovince", "address1_telephone1", "address1_telephone2", "address1_telephone3",
    address1_upszone", "address2_addresstypecode", "address2_city", "address2_country"};

    ConditionExpression condition = new ConditionExpression();
    condition.AttributeName = "jobtitle";
    condition.Operator = ConditionOperator.Equal;
    condition.Values = new string [] {"CEO", "CFO"};
    QueryExpression query = new QueryExpression();
    query.EntityName = EntityName.contact.ToString();
    query.ColumnSet = cols;
    BEC = serv.RetrieveMultiple(query);




    }while(Incrementor <= BEC.BusinessEntities.GetUpperBound(0));
    contact ContactsEC = (contact) BEC.BusinessEntities[Cntr];

    I've been told to loop through the collection, cast to contact, do the retrieve (that's the cols and queryexpresion bit), and then store the results in a string variable (I have a string variable pointing to cols.attributes.tostring();


    Thanks for your help!
    Sunday, February 10, 2008 12:31 PM
  • Hi.

     

    Key entityKey = new Key();

    entityKey.Value = new Guid("{Your Guid Here}");

     

    Adi

     

    Sunday, February 10, 2008 11:45 PM
  • Here is how you traverse contacts retrieved as BusinessEntityCollection.

     

    Code Snippet

      

       CrmService service = new CrmService();
       service.Credentials = System.Net.CredentialCache.DefaultCredentials;

       //You may use ColumnSet instead, just make sure you get the names right.

       AllColumns allColumns = new AllColumns();
       ConditionExpression condition = new ConditionExpression();
       condition.AttributeName = "jobtitle";
       condition.Operator = ConditionOperator.Equal;
       condition.Values = new object[]{"CEO","CFO"};
       QueryExpression Query = new QueryExpression();
       Query.EntityName = EntityName.contact.ToString();
       Query.ColumnSet = allColumns;
       BusinessEntityCollection entities = (BusinessEntityCollection) service.RetrieveMultiple( Query );
       
       foreach( BusinessEntity entity in entities.BusinessEntities )
       {
                contact Contact = entity as contact;
                //Write Contact CSV information here.
       }

     

     

     

    Adi

    Monday, February 11, 2008 12:12 AM
  • I implemented the above code but all it gives me back is the full name of the column. IE solutionname.columname etc. Not the actual value of the attribute. What I'm struggling to get is the actual value of an attribute i.e. for phone number, the number such as 024848284 or whatever it may be.

    I can get the column attributes by using foreach to iterate through each column and writing it to the file, which forms a header.


    Also, I adapted your casting code snippet but that didn't work. I adapted it to the below, however, but get a runtime error of object reference not set to an object (on the line stating entityKey.Value...).




                                   
    //                                                                    Key entityKey = new Key();
    //                                                                    entityKey.Value = new Guid(ID);
    //                                                                    System.Guid aGuid = entityKey.Value;
    //                                                                                                       
    //
    //                                                                    // Retrieve the contact.
    //                                                                    // The EntityName indicates the EntityType of the object being retrieved.
    //                                                                    account TheAccount = (account)service.Retrieve(EntityName.contact.ToString(), aGuid, cols);


    Thanks for the above code snippet, however.
    Monday, February 11, 2008 1:27 PM
  • Hi.

     

    foreach( BusinessEntity entity in entities.BusinessEntities )
    {
                contact Contact = entity as contact;
                //Print Lookup Elements Actual Values

                Console.WriteLine( Contact.parentcustomerid.name); // parent account name actual value

                //Print String Elements Actual Values.

                Console.WriteLine( Contact.fullname );

    }

     

    Cheers,

    Adi.
    Monday, February 11, 2008 6:00 PM
  • Hi.

     

    I didn't get you!

    if you need to call Retrieve with a GUID do the following:

    Guid aGuid = new Guid( ID );

    account TheAccount = (account)service.Retrieve(EntityName.account.ToString(), aGuid , cols );

     

    That's it.

    Adi

     

    Monday, February 11, 2008 6:05 PM
  • //                                                                    Key entityKey = new Key();
    //                                                                    entityKey.Value = new Guid(ID);
    //                                                                    System.Guid aGuid = entityKey.Value;
    //                                                                                                       
    //
    //                                                                    // Retrieve the contact.
    //                                                                    // The EntityName indicates the EntityType of the object being retrieved.
    //                                                                    account TheAccount = (account)service.Retrieve(EntityName.contact.ToString(), aGuid, cols);


    Sorry I made a mistake in the above statement. I'm retrieving account but specify contact in entity name.

    Anyway in the first 3 lines with the key stuff, I pass in ID as a string. It refers to a string var of the accountID (Account.accountid.valueWink and then call the GUID I passed this into in the final retrieval statement.

    Didn't have luck with this today but then if I specify account and then contact, then this wouldn't help me with the integrity of the application. It would probably explain one or two errors, too. Carelessness...


    Is this statement fine?

    Account.accountid.value;

    Where Account is an account variable. Or do I have to specify the fullpath ie after a period there's a set of methods.


    And I assume the function to write to line will be in conjunction to the function you posted several posts ago of the retrieve. The key design issue here is that the columns I get back are in accordance to the condition, which you have done. I'll try this code very soon. Also, it has to retrieve the attributes specified in cols.attributes for a/the given entity.



    foreach( BusinessEntity entity in entities.BusinessEntities )
    {
                contact Contact = entity as contact;
                //Print Lookup Elements Actual Values

                Console.WriteLine( Contact.parentcustomerid.name); // parent account name actual value

                //Print String Elements Actual Values.

                Console.WriteLine( Contact.fullname );

    }



    How can this code automatically iterate through each attribute in cols.attributes? When I write using the streamwriter I need to pass in a string variable. Atm you're writing attributes 1 by 1 manually.


    entities.BusinessEntities - shouldnt that be entities.businessentitycollection? If so, why not?

    Thanks so much!

    Monday, February 11, 2008 9:56 PM
  • Hi.

     

    Use FilteredViews and retrieve the data into a datatable.

    Then you can use the DataTableObj.Columns and DataTableObj.Rows

     

    public string ContactsCmdText

    {
          get

          {
                 return @"SELECT …. FROM FILTEREDCONTACT WHERE PARENTCUSTOMERID = '{0}'";
          }
    }

     

    public void CreateCSV()

    {
          DataTable ContactsTable = GetContacts( "{Account Guid Here}");

         

          //Create CSV Headers

          foreach( DataColumn Column in ContactsTable.Columns )

          {

                 //Write Columns to file ..

          }

     

          // Fill CSV Data

          foreach( DataRow row in ContactTabel.Rows )

          {

      //Write Data to file...

          }
    }

     

    public DataTable GetContacts( Guid AccountId )

    {

         String ConnectionString = "Data Source=.;Initial Catalog=[Your Catalog];Integrated Security=SSPI";

         String CmdText = String.Format( ContactsCmdText , AccountId );

         DataTable tempTable = new DataTable();
         using( SqlConnection Conn = new SqlConnection( ConnectionString ) ){

         using( SqlCommand Comd = new SqlCommand( CmdText , Conn ){

                   SqlDataAdapter Adap = new SqlDataAdapter( Comd ):

       Adap.Fill( tempTable );

         }}

         Return tempTable;
    }

     

    Best Good Luck,

    Adi

    Tuesday, February 12, 2008 11:48 AM
  • Hi,

    I can now retrieve the data with the 2nd from last post you made (not using a dataset). All I need to do now is store all the data in a single stream and write it to crm because I'm having formatting issues.

    As for the key issue, the best progress I've made is in getting the runtime exception. I guess I'll ask someone at work about this problem.

    Thanks!
    Wednesday, February 13, 2008 12:55 AM