Archive

Posts Tagged ‘JavaScript’

Mini Preview Window for Contact Lookup Field

March 9th, 2010

Preview Field

Looks cool doesn’t it. well here goes nothing..

to achieve the above you will need the following (click read more link..)


// the following code is for the hover over lookup window

function OnCrmPageLoad()
{
/*
Reference the to Lookup, Provide the lookup (control) id.
*/
var PrvToLui = new LookupPreview("new_primarycontactid");
/*
Add Account Preview Information ,
returns an account entity wrapper object
*/
var accountEntity = PrvToLui.AddEntity("account");

/*
Add Account Preview Attributes,
Provide lable and schema name for each attribute
*/
accountEntity.AddAttribute("Business Phone","address1_line1");
accountEntity.AddAttribute("Mobile Phone","address1_city");
accountEntity.AddAttribute("Fax","address1_stateorprovince");
accountEntity.AddAttribute("E-mail","address1_postalcode");
/*
Add Related Entity (Primary Contact) Information
AddLinked requires the entity name and
lookup field schema name on the account.
*/
var contactEntity = accountEntity.AddLinked("contact","primarycontactid");
contactEntity.AddAttribute("Primary Contact","fullname");
contactEntity.AddAttribute("Contact Phone","telephone1");

/* ------------------------------------------------------------------ */

//Contact Entity Preview
var contactEntity = PrvToLui.AddEntity("contact");
contactEntity.AddAttribute("Business Phone","telephone1");
contactEntity.AddAttribute("Mobile Phone","mobilephone");
contactEntity.AddAttribute("Fax","fax");
contactEntity.AddAttribute("E-mail","emailaddress1");

var accountEntity = contactEntity.AddLinked("account","parentcustomerid");
accountEntity.AddAttribute("Parent Company", "name");
accountEntity.AddAttribute("Parent Street","address1_line1");
accountEntity.AddAttribute("Parent Suburb","address1_line2");
accountEntity.AddAttribute("Parent State","address1_stateorprovince");
accountEntity.AddAttribute("Parent Postcode","address1_postalcode");
accountEntity.AddAttribute("Parent Website","websiteurl");
/* ------------------------------------------------------------------ */

}
Read more...

MSCRM-Admin CRM 4.0, JavaScript , , , , ,

CRM4: Hiding tabs and sections

June 17th, 2009

Hello all,

I am currently working on a small CRM project and one of the requirements was to hide a section and a tab onLoad.

// Hiding the Contract section

crmForm.all.contractid_c.parentElement.parentElement.style.display=’none’;

// Hide the KB article tab (2nd Tab)

crmForm.all.tab1Tab.style.display=’none’;

I’m sure you can use this in other ways (i.e:. onChange, onClick etc..).

Happy Coding!

MSCRM-Admin CRM 4.0, Customizations, JavaScript , , , , ,

Call/Request a workflow in CRM 4.0 using Javascript

May 25th, 2009
  1. Add a button on Form’s Toolbar/Grid’s Menu Bar for the entity in ISV config file (export ISV config)
  2. Call launchOnDemandWorkflowForm (for button on Form) or launchOnDemandWorkflow (for button on grid) functions and pass the parameters sGrid, iObjType and workflowId.
  3. Save the config file and import it to CRM.

An example of this could be:

<Entity name="account">
<ToolBar ValidForCreate="0" ValidForUpdate="1">
<Button Icon="/_imgs/mybutton.jpg" JavaScript="launchOnDemandWorkflowForm('', '10029','{00000000-0000-0000-0000-000000-00000}');" PassParams="1" WinParams="" WinMode="0">
<Titles>
<Title LCID="1033" Text="Send Email" />
</Titles>
<ToolTips>
<ToolTip LCID="1033" Text="Send Welcome Email" />
</ToolTips>
</Button>
</Entity>

Or you can alternatively request it from the web service:

/* the function */
ExecuteWorkflow = function(entityId, workflowId)
{
var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"ExecuteWorkflowRequest\">" +
" <EntityId>" + entityId + "</EntityId>" +
" <WorkflowId>" + workflowId + "</WorkflowId>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return(resultXml.xml);
}

// call the workflow
var theWorkflowId = "3FD2DD58-4708-43D7-A21B-F0F90A0AA9F2"; //change to your workflow Id
ExecuteWorkflow(crmForm.ObjectId, theWorkflowId);

MSCRM-Admin CRM 4.0, Customizations, JavaScript , , , , ,

Show no. of open or closed activities (history) on the CRM form.

May 18th, 2009

Hello all,

Have you ever wanted to see the total no. of outstanding/completed activities on a Case, Account, Opportunity, Contact etc..?  

Well now you can, simply add the following JavaScript code to the onLoad event of the entity, it will query the webservice and it will give you the exact count of open/closed activities.

Update: 9th March 2010 - I have updated the code below and tested.

// this code is to get the number of activities and historical activities.
var buXml = GetRegardingActivity();
if(buXml != null)
{
    var buNodes = buXml.selectNodes("//BusinessEntity/q1:statecode"); // CRM 4.0
    var iActivity = 0;
    var iHistory = 0;

    if(buNodes != null )
    {
        /*get values*/
        for( i = 0; i < buNodes.length; i++)
        {
            switch(buNodes[i].text)
            {
                case "Open" : iActivity++; break;
                case "Scheduled" : iActivity++; break;
                case "Completed" : iHistory++; break;
                case "Canceled" : iHistory++; break;
            }
        }

        if(document.getElementById('navActivities') != null)
        {
            document.getElementById('navActivities').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navActivities').getElementsByTagName('NOBR')[0].innerText + " (" + iActivity + ")";
        }

        if(document.getElementById('navActivityHistory') != null)
        {
            document.getElementById('navActivityHistory').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navActivityHistory').getElementsByTagName('NOBR')[0].innerText + " (" + iHistory + ")";
        }
    }
}

function GetRegardingActivity()
{
    var xml = "" +
    "<?xml version="1.0" encoding="utf-8"?>" +
    "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">" +
    GenerateAuthenticationHeader()  +
    " <soap:Body>" +
    "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
    " xsi:type='q1:QueryExpression'>"+
    " <q1:EntityName>activitypointer</q1:EntityName>" +
    " <q1:ColumnSet xsi:type="q1:ColumnSet">" +
    " <q1:Attributes>" +
    " <q1:Attribute>statecode</q1:Attribute>" +
    " </q1:Attributes>" +
    " </q1:ColumnSet>" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:Criteria>" +
    " <q1:FilterOperator>And</q1:FilterOperator>" +
    " <q1:Conditions>" +
    " <q1:Condition>" +
    " <q1:AttributeName>regardingobjectid</q1:AttributeName>" +
    " <q1:Operator>Equal</q1:Operator>" +
    " <q1:Values>" +
    " <q1:Value xsi:type="xsd:string">" + crmForm.ObjectId + "</q1:Value>" +
    " </q1:Values>" +
    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:Criteria>" +
    " </query>" +
    "</RetrieveMultiple>"+
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;
    return resultXml;
}

Any comments or suggestions are welcome!

MSCRM-Admin CRM 4.0, Customizations, JavaScript , , , , ,

Add custom toolbar buttons to the Case Entity form.

May 17th, 2009

Hi All,

Have you ever wanted to add some toolbar buttons to the CASE/CONTACT/ACCOUNT form?

Well now you can, you can use the ISV.config provided by CRM.

  1. Export the ISV Config Customizations from CRM
  2. Edit the XML file
  3. Find the <entity name=”incident”> node and add the following - I couldn’t paste the XML because of wordpress limitations.

  4. Note: the following are custom images: taskicon.gif, emailicon.gif and phonecallicon.gif.

  5.  Save the XML and import into CRM and publish the customizations.

Code explantion:

JavaScript="locAddActTo(4210)" PassParams="0" WinParams="" WinMode="0" Client="Web, Outlook" AvailableOffline="true"

Here we are using the CRM javascript function locAddActTo() and using the entity identifier (this being a phone call) which is located in the SDK and we are telling CRM to show this button is Outlook and the web version of CRM

JavaScript="resolve();" PassParams="0" WinParams="" WinMode="0" Client="Web, Outlook" AvailableOffline="true"

We are using the resolve() function on the case entity form which will resolve the case like you would when going to the actions menu > resolve case

MSCRM-Admin CRM 4.0, Customizations, JavaScript , , , ,

Hiding Sidebar Navigation (Related Entities)

September 18th, 2008

Hi Guys,

Recently for a client, we had to create some many-to-1 relationship from Contact to Accounts & Opportunities.  We then had the problem of the related entity navigation on the side showing up.  They didn’t want to see all these related navigations. I added some onLoad code on the contact form to hide the Sidebar navigation.

Before:

The key is to get the relationship Elementid from the source code of the page (Press Ctrl + N to open new window, then click on the ‘view’ menu and click ’source’). Then find the Name of the Navigation and find the element id:

onLoad code:
document.getElementById("nav_new_contact_account_dm1").style.display = "none";
document.getElementById("nav_new_contact_account_dm2").style.display = "none";
document.getElementById("nav_new_contact_account_dm3").style.display = "none";
document.getElementById("nav_new_contact_account_dm4").style.display = "none";
document.getElementById("nav_new_contact_opportunity_dm1").style.display = "none";
document.getElementById("nav_new_contact_opportunity_dm2").style.display = "none";
document.getElementById("nav_new_contact_opportunity_dm3").style.display = "none";
document.getElementById("nav_new_contact_opportunity_dm4").style.display = "none";

After:

I hope this helps you all and gives you some idea of how powerful JavaScript & CRM are.

-MSCRM Person

MSCRM-Admin CRM 4.0, Customizations, JavaScript , , , , , ,

lookup field - onchange event

August 12th, 2008

Hi Guys,

I have noticed that sometimes onchange code for a lookup needs some time (1sec) than to run its code.

Heres an example of a 1 second wait (wait until the lookup is populated with the lookup id, name etc):

window.setTimeout(getDetails, 1000);

function getDetails()
{
if(crmForm.all.ownerid.DataValue[0].id != null)
{
// do something
}
}

MSCRM-Admin Customizations, JavaScript , , , , , , ,