locked
Run a FetchXML query during pre-operation in a plug-in. Code inside. RRS feed

  • Question

  • My case is: I want to execute a FetchXML query via a plugin on the pre-operation stage. I need to check if the existing record has certain fields filled. At the moment; I have this:

    EntityCollection result = service.RetrieveMultiple(new FetchExpression(FetchXML));
                Guid entityGUID = (Guid)entity.Id;
    
                foreach (var c in result.Entities)
                {
                    if ((Guid)c.Attributes["accountid"] == entityGUID)
                    {
                        executeCondition = true;
                    }
                }
                return executeCondition;

    Which as you can see, taked in a FetchXML string, iterates through the collection, checks if there is a match with the current entity, and then if so, sets the executeCondtion to true.

    The problem with this method is that during the Pre-Operation stage; the current record doesn't have a guid - it's not in the DB.

    The other problem is that it is inefficient; I have over 10000 records, and so far it is just looping through all of them.

    Does anyone know how I could achieve this?

    Wednesday, March 9, 2016 12:05 PM

All replies

  • You can get the values in the current record through the Target InputParameter. If this is a new record, then it doesn't matter if it has no Guid, as there can't be any existing records that link to it.

    To make the code more efficient, you should be able to use conditions on the FetchXml to restrict the number of records returned


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, March 9, 2016 2:54 PM
    Moderator
  • BTW, it's looping through all of them since you are not exiting that "for" loop once executeCondition has been set. Yet it won't give you more then 5000 records because of fetch limitations..

    For a new record, you can just check if it's a "create" message.. Here is an example:

    https://crmbusiness.wordpress.com/2011/04/20/crm-2011-how-to-check-the-context-message-in-a-plugin/

    If it's "create", you don't even need to fetch - as David mentioned, there will be no links anyways.

    But, with that said, you may still need to adjust your fetch by adding a condition there for the accountid (and you would update that condition in your plugin code dynamically).. again, as David mentioned.

     

    Alex Shlega, TreecatSoftware.Com


    Wednesday, March 9, 2016 11:00 PM