Wednesday, July 25, 2012

How to create a hyperlink in CRM 2011 using Javascript?


Introduction:

             Today I am going to Explain one important topic in MS CRM.Some days back I have to  create one hyperlink in account Entity and the hyperlink text will be the name of account + parent account name and after click on that link the Google search page will open with the search text as hyperlink text(fullname).

So,Today I am going to Explain how to create the Hyperlink with Google search Functionality.

First you have to create a one field with name Google Search and the type of the field will be Text and using java script I am converting this into hyperlink.

Lets discuss Step by Step

1.Create a Field name Google Search (Settings->Customization->Customize System ->Components->Entity->Account->Field->New->Save and Close)


2.Put this Field in Form of Account.(Open Form and just Drag and Drop the Field Google Search)


3.Now Our Objective is Put the Text of accountname in Google Search Field and make it hyperlink using Javascript.
4.So for that We need to create the Javascript File.
5.Open Notepad and put the text below.

function ConvertToLink(recordtype)
{

             var ctrl = Xrm.Page.ui.controls.get("new_googlesearch")._control;  
if(Xrm.Page.ui.getFormType() == 2)
{

                               var strDisplayText;
                               var strSearchString;
         switch(recordtype)
         {
                                          case 'Account':
                     strDisplayText = Xrm.Page.data.entity.attributes.get("name").getValue();
                                 strSearchString = strDisplayText;
                                                                break;
                                          case 'Contact':
                   if(Xrm.Page.data.entity.attributes.get("parentcustomerid").getValue() != null)
{
           strDisplayText = Xrm.Page.data.entity.attributes.get("fullname").getValue() + "(" + Xrm.Page.data.entity.attributes.get("parentcustomerid").getValue()[0].name +")";
          strSearchString = Xrm.Page.data.entity.attributes.get("fullname").getValue() + " " + Xrm.Page.data.entity.attributes.get("parentcustomerid").getValue()[0].name;
                    }
else
{
strDisplayText = Xrm.Page.data.entity.attributes.get("fullname").getValue();
strSearchString = Xrm.Page.data.entity.attributes.get("fullname").getValue();
}
break;
                                }
                    if(trim(strSearchString).length >=0)
{
strSearchString=strSearchString.replace(/&/g, "" ).replace(/>/g, "").replace(/</g, "").replace(/'/g, "");
var btn = "<a href='javascript: void(0);' onclick=\"window.open(\'http://www.google.com/search?q=" + strSearchString + "\', \'windowname1\', \'resizable=1, scrollbars=1\');  return false;\" style='color:blue;text-decoration:underline !important'>" + strDisplayText + "</a>";

ctrl.get_element().innerHTML += btn;  
}
}
                     ctrl.get_element().firstChild.style.display = 'none';

}
function errorDisplay(XmlHttpRequest, textStatus, errorThrown)
{
     alert(errorThrown);
}

function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

new_googlesearch is the Id of the Google Search Field.

6.Now Save the File With Name GoogleSearchFunction.js.(dont forget to include .js Extension).
7.Now we need to add this Javascript to Web Resources.
8.Now Go to Settings->Customization->Customize System ->Web Resources->New and Add Following detail.


9.Now Save it and Publish it.
10.Now Go to Settings->Customization->Customize System ->Components->Entity->Account->Open Form->Form Properties(its is in header)
11.In Events Tab Add Form Google Search Function(new_GoogleSearchFunction) Library.
12.Then Add Form Libraries
   Control:Form
   Event:OnLoad
13.Click on Add Event Handler.
     Library:new_GoogleSearchFunction
     Function Name:ConvertToLink
     Parameter:'Account' or (you can pass also 'Contact' if you want to do same functionality in Contact Form.because above javascript will be useful for Both Entity Account and Contact.I have used Switch statement in Javascript.)
and click on OK



14.Now Save it and Publish it.
15 Now open any Existing Account and check.


->Now If you look at the Final Output you will see only hyperlink is there Now if you want same hyperlink inside the Text-Box then you just add the Following Lines in GoogleSearchFunction.js File.
(Settings->Customization->Customize System ->Components->Entity->Account->Open Form->Form Properties(its is in header)->Edit Form Libraries)

function ConvertToURL()
{
var urlField =Xrm.Page.data.entity.attributes.get("new_google");

if(urlField.getValue()==null)
urlField.setValue(Xrm.Page.data.entity.attributes.get("name").getValue())

var urlField1=document.getElementById("new_google")
urlField1.readOnly = true
urlField1.style.color = "blue";
urlField1.style.textDecoration="underline";
urlField1.style.cursor='hand';
urlField1.onclick = OpenGoogleUrl;
}

function OpenGoogleUrl(evt)
{
var str=Xrm.Page.data.entity.attributes.get("name").getValue()
window.open("http://www.google.co.in/search?q=" +str );
}

->Now create one More field name Google(The Hyperlink will be shown inside the textbox)

     Library:new_GoogleSearchFunction
     Function Name:ConvertToURL

->Click OK->Save->Publish and check



No comments:

Post a Comment