Plugin - Retrieved values don't work RRS feed

  • Question


    Hello All,

    Just wanted to ping you guys here to see if you knew why this code is breaking. I'm doing a normal retrieve on the account, and getting 3 values - name, customertypecode, and accountnumber. After the retrieve, I can access the name, but whenever I touch the customertypecode or the accountnumber the code breaks. Here, I have just put the accountnumber into a string variable, and the code breaks, although that wasn't the original way I was using those values in the code. Why can I access the name but not the other two values?

    Thanks in advance,



    Code Snippet

    public string RetrieveCustFive(IPluginExecutionContext context, string accountid)
                //Start the service
                ICrmService service = context.CreateCrmService(context.InitiatingUserId);

                string[] split = new string[10];
                string name = "";
                string finalname = "";
                string tempname = "";

                //Make the Retrieve GUID
                Guid Accountguid = new Guid(accountid);

                //Retrieve the Account's Customer Type and Name
                //Create the columnset
                string[] cols = new string[] { "accountid", "customertypecode", "name", "accountnumber" };

                //Create the Target
                TargetRetrieveAccount target = new TargetRetrieveAccount();

                //Set Properties of target
                target.EntityId = Accountguid;

                //Create the request object
                RetrieveRequest retrieve = new RetrieveRequest();

                //Set the properties of the request object
                retrieve.Target = target;
                retrieve.ColumnSet = new ColumnSet(cols);
                retrieve.ReturnDynamicEntities = false;

                //Execute the request
                RetrieveResponse retrieved = (RetrieveResponse)service.execute(retrieve);

                //Put this info in a business entity
                //account account1 = (account)retrieved.BusinessEntity;

                //put name into an array
                split = account1.name.Split(' ');
    //The above line works fine, I can get the account1.name value in a WriteToFile.
                string testacctnum = account1.accountnumber.ToString();
    //This line breaks. It will also break if I do this same thing with the customertypecode value.



    Wednesday, May 28, 2008 6:06 PM


  • The issue ended up being that the value was null. Even though I expected it to be null, if I tried to access it outside of a try/catch it threw an error.


    Thanks for the help,



    Monday, June 2, 2008 5:24 PM

All replies

  • Hi


    To me it looks like to have much more code then you need. You can replace most of you code with this:


    Code Snippet

    ICrmService service = context.CreateCrmService(context.InitiatingUserId);


    ColumnSet cols = new ColumnSet()
    cols.Attribute = new String [] {"accountid", "customertypecode", "name", "accountnumber"};

    account oAccount = (account) service.Retrieve(EntityName.account.ToString(), new Guid(accountid),cols);


    string testacctnum = oAccount.accountnumber;



    and so on....



    Wrote this in notepad so there might be some mistyping but I hope you get the picture.


    Remember you can debug you plugin by attaching your code to the w3p.exe process and also copy the pdb file into the same folder as you dll.







    Wednesday, May 28, 2008 7:21 PM
  • Thanks Christian,


    We have tried it with both of the retrieve commands, the one I have (building the target and using service.execute) and the one you have (casting the service.Retrieve as an account). The problem wasn't stemming from that.


    We have never had success debuggin class libraries like this -- we would love to be able to do so. We usually just use the WriteToFile method to see what values our code is returning and where it breaks. Can you point us toward some documentation on how to use the debuggin method you mentioned above?




    Wednesday, May 28, 2008 10:26 PM
  • If your CRM and Visual Studio is running on the same server copy you dll and its pdb file to the crm assembly folder and register the plugin to a CRM event.


    C:\Program Files\Microsoft Dynamics CRM Server\Server\bin\assembly


    In Visual Studio make a break point in your code. Click Debug > Attach to Process in the menu and Attach your code to process w3wp.exe. Go to CRM and do the event that will make your plugin run. After a few seconds your visual studio will pop up and with the F10 key you can step forward in your code. (shift +F5 to stop debug).


    Remember you have to do and IIS reset to overwrite any running plugin that is registered with CRM.






    Thursday, May 29, 2008 10:25 AM
  • The issue ended up being that the value was null. Even though I expected it to be null, if I tried to access it outside of a try/catch it threw an error.


    Thanks for the help,



    Monday, June 2, 2008 5:24 PM