Wednesday, July 14, 2010

Create a task through JavaScript after using Web Service in MS CRM 4.0

Some times we required to create a task for a user through JavaScript, you can do this by the following code

Here you can pass your Description ,Owner, Regarding,subject and created By parametters.

function CreateTask(MangerID,CreatedByID)
{
var CurrentQuoteID = crmForm.all.quotenumber.DataValue;
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>" +
" <Create xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <entity xsi:type=\"task\">" +
" <description>Your Message "+ CurrentQuoteID + "</description>" +
" <ownerid>" + MangerID + "</ownerid>" +
" <regardingobjectid type=\"quote\">" + crmForm.ObjectId + "</regardingobjectid>" +
" <subject>Your Subject "+ CurrentQuoteID + "</subject>" +
" <createdby>" + CreatedByID + "</createdby>" +
" </entity>" +
" </Create>" +
" </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/Create");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;
}

How can we use LookUpControl in my Custom page in CRM 4.0

I'm trying to replicate the functionality of this CRM 4.0 in my custom aspx page

<nobr>
<asp:TextBox ID="lookupresult" runat="server"></asp:TextBox>
<img src="/_imgs/btn_off_lookup.gif" onclick="ShowOwnerLookupDialog()" />

<script language="javascript">
function ShowOwnerLookupDialog()
{
var lookupItems = showModalDialog( '/_controls/lookup/lookupsingle.aspx?class=BasicOwner&objecttypes=8,2020&browse=0&ShowNewButton=1&ShowPropButton=1&DefaultType=0' , null, '' );
document.all.lookupresult.value = lookupItems.items[0].id;
}
</script>
</nobr>

Update Filtered lookup data in CRM 4.0

For this We have to pass fetch xml string using one of the parameters "search" recognised by the server otherwise exception is thrown (there is a way to disable parameters check through the registry setting
DisableParameterFilter, but it's easier without yet another undocumented setting).
Since we want to disable search functionality it makes sense to re-use search parameter.

Use the following code:

This line of code using for single entity
//The attribute where lookup is attached
var field = crmForm.all.pricelevelid;

// Ensure that search box is not visible in a lookup dialog
field.lookupbrowse = 1;
// Pass fetch xml through search value parameter
field.AddParam("search",
"<fetch mapping='logical'><entity name='pricelevel'>"
+ "<filter><condition attribute='name' operator='eq' value='pricelist name' /></filter></entity></fetch>");


This line of code using for multiple entity
//Lookup will show all the Child users of current loggedIn user

//Return all child users
var targetUserUnits = GetChildusers() //for eg: <value>"+Value1+"</value><value>"+Value2+"</value>
//The attribute where lookup is attached
var field = crmForm.all.new_userid;
//Ensure that search box is not visible in a lookup dialog
field.lookupbrowse = 1;

field.AddParam("search",
"<fetch mapping='logical'><entity name='systemuser'>"
+"<all-attributes/>"
+"<link-entity name='new_nesteduser' from='new_userid' to='systemuserid'>"
+"<filter type='and'><condition attribute='new_lft' operator='between'>"
+targetUserUnits
+"</condition></filter></link-entity></entity></fetch>");

Unsupported
You can manage it from lookup form means if this parameter contains any value then apply this filter otherwise use as origional.
to do this open the page "C:\Program Files\Microsoft Dynamics CRM\CRMWeb\_controls\lookup\lookupsingle.aspx " and change it accordingly as required.

For Eg.

void crmgrid_PreRender( object sender , EventArgs e )
{
// As we don't want to break any other lookups, ensure that we use workaround only if
// search parameter set to fetch xml.
<strong>if (crmGrid.Parameters["search"] != null && crmGrid.Parameters["search"].StartsWith("<fetch"))
{
crmGrid.Parameters.Add("fetchxml", crmGrid.Parameters["search"]);
// searchvalue needs to be removed as it's typically set to a wildcard '*'
crmGrid.Parameters.Remove("searchvalue");
// Icing on a cake - ensure that user cannot create new contact outside of the account
// and then select it.
this._showNewButton = false;
} </strong>
}

Expose the toggling function at the window level in CRM 4.0

After create a function in page load event of any entity in CRM and tried to call it from onSave or any attribute OnChange event
then you don't required to type the same function again and again just toggle this function at the bottom of pageLoad
event, to do this use the following code

//Expose the toggling function at the window level
this.functionName = functionName;

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...