
Hi I'm trying to capitalize 1st and last name letters for the existing contacts using c# and for the new contacts javascript.
I need a help in fixing them please.
Thanks
c#:
private void button1_Click(object sender, EventArgs e)
{
_service = GetService();
var Criteria = new FilterExpression();
_log.Info("Capitalize function has started");
QueryExpression query = new QueryExpression("contact");
query.EntityName = "contact";
query.ColumnSet = new ColumnSet();
query.ColumnSet.AddColumns("firstname", "lastname", "contactid");
FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.Or);
childFilter.AddCondition("firstname", ConditionOperator.NotNull);
childFilter.AddCondition("lastname", ConditionOperator.NotNull);
EntityCollection results = _service.RetrieveMultiple(query);
foreach (var item in results.Entities)
{
var contactid = item.GetAttributeValue<Guid>("contactid");
ColumnSet cols = new ColumnSet(new[] {"firstname", "lastname"});
Entity contact = _service.Retrieve("contact", contactid, cols);
if (item.GetAttributeValue<string>("firstname") != null)
{
{
item["firstname"]= ToTitleCase(item.GetAttributeValue<string>("firstname"));
}
if (item.GetAttributeValue<string>("lastname") != null)
{
// item["lastname"] = item.GetAttributeValue<string>("lastname").ToUpper();
item["lastname"] = ToTitleCase(item.GetAttributeValue<string>("lastname"));
}
_service.Update(contact);
}
}
}
public string ToTitleCase(string str)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}
JavaScript:
//================Start: CapitalLetter=================================================
function CapitalizeFirstLetters(FieldName)
{
var firstnamelowercase = Xrm.Page.getAttribute("firstname").getValue();
if(firstnamelowercase!= null)
{ var firstName = firstToUpperCase(firstnamelowercase);
Xrm.Page.getAttribute("firstname").setValue(firstName); }
var lastnamelowercase = Xrm.Page.getAttribute("lastname").getValue();
if(lastnamelowercase!= null)
{ var firstName = firstToUpperCase(lastnamelowercase);
Xrm.Page.getAttribute("lastname").setValue(lastname); }
}
//====================End:CapitalLetter=================================================
*This post is locked for comments
I have the same question (0)You can use the following function to capitalize the first letter of a string:
function firstToUpperCase( str ) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
}
You can then call it:
var firstnamelowercase = Xrm.Page.getAttribute("firstname").getValue();
var firstName = firstToUpperCase(firstnamelowercase);
Xrm.Page.getAttribute("firstname").setValue(firstName);
In your code you have some mismatch in the attribute Names, so try and follow logic above.