Saturday 14 May 2016

Create A public View Using Java Script In CRM 2015/2016

Today I am back with the new requirement where my client has asked me to create a public view on the button click using java script .

To work around this requirement need to follow some step :-

1. Get the fetchXml generated using advance find or any other code source.
2. Create a layout for view according to your requirement.
3. create a record in "SavedQuery" Entity which is the CRM default Entity to save all the view.

Step 1.
Generating of fetchXml can be done through Advance Find if it has a static fetch request.
For dynamic you have to create your own method to generate a fetchXml.

Sample of FetchXml will be like :-
      var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                        "<entity name='account'> " +
                            "<attribute name='new_lastname' />" +
                            "<attribute name='new_firstname' />" +                                                   
                            "<filter type='and'>" +
"<condition attribute='new_lastname' operator='like' value='%abc%' />" +
                            "</filter>" +                         
                         "</entity>" +
                      "</fetch>";
Step 2.
During creating Layout one thing need to keep in mind is the object value in Layout it should be the objectcode value of that entity on which the view is created.

To get the objectCode of Entity dynamically follow my this blog link :-
http://crmpankaj.blogspot.in/2015/10/create-custom-dialog-lookup-values-for.html
  var layoutXml = "<grid name='resultset' object='1' jump='name' select='1' preview='1' icon='1'>" +
             "<row name='result' id="accountid'>" +
                   "<cell name='sy_lastname' width='150' /> " +
                   "<cell name='sy_firstname' width='150' /> " +                  
           "  </row>" +

          "</grid>";
Step 3.
Now create a record in SavedQuery Entity in CRM.
Here we have used SDK.REST Library to create a record.

Sample code to create record .                 var savedQueryObject = new Object();
                savedQueryObject.Name = "A Custom Public View ;
                savedQueryObject.Description = "A Saved Query created";
                savedQueryObject.ReturnedTypeCode = "account";   //schema name of the Entity in which view need to be created
                savedQueryObject.FetchXml = fetchXml;
                savedQueryObject.LayoutXml = layoutXml;
               
              //  savedQueryObject.IsDefault = true;
                savedQueryObject.QueryType = 0;  //public view =0;

                SDK.REST.createRecord(savedQueryObject, "SavedQuery", function (result) {
                     alert("view created");
                        },
                        function (error) { alert(error.message); });

            }
Hope this would be helpful in your custom development !!!!!
If you have any Query !!!  Please leave your comment !!!!!!!!!!

2 comments:

  1. In this post you show each steps to create a public view on the button click using Java Script in CRM. Its very helpful in our custom development.
    June 2019 Calendar Printable

    ReplyDelete