Tuesday, June 7, 2011

Executing FetchXML in CRM 2011

Hi friends, in crm 2011 service don't have fetch method to execute fetch XML query. we can execute fetch xml query using RetrieveMultiple method of service. Here i'm going to retrieve system user's team details using fetchxml.

string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                                    <entity name='team'>
                                    <attribute name='name' />
                                    <attribute name='businessunitid' />
                                    <attribute name='teamid' />
                                    <order attribute='name' descending='false' />
                                    <link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>
                                        <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='user'>
                                        <filter type='and'>
                                            <condition attribute='systemuserid' operator='eq' value='{0}' />
                                        </filter>
                                        </link-entity>
                                    </link-entity>
                                    </entity>
                                </fetch>";

                 //  pass user guid as parameter
                string formatXml = string.Format(fetchXml, userid.ToString());

                // Executing fetchxml using  RetrieveMultiple method
                EntityCollection entities = service.RetrieveMultiple(new FetchExpression(formatXml));

                foreach (Entity e in entities.Entities)
                {
                   Guid Id = new Guid(e.Attributes["teamid"].ToString());
                   string TeamName = e.Attributes["name"].ToString();
                }

Force submit in CRM 2011

Hi, I got a requirement to update CRM Field data on a Ribbon Button click. I will be calling the following java script method from ribbon button. Here we will set value to the CRM Field and then set force submit to the field. After that we will call the save method, it will save the crm Form Data.

function SaveData() {

    Xrm.Page.getAttribute("new_field").setValue("100");

     // setting Force Submit to the field, This must be set to save the data
    Xrm.Page.getAttribute("new_field").setSubmitMode("always");

    // Call Form save method
    crmForm.Save();
    

Happy coding.. :)