I am creating a JavaScript button on the 'Case' object and if the user selects the button it will create a New User. I'm able to map most of the fields from the Case to the User page just fine but for the 'Alias' and 'Community Nickname', since they are created once a user INPUTS their first and last name and email on the User page. It's not wanting to create the Alias or Nickname since they are mapped over from the Case object. I found some APEX code to possibly create the Alias and Nickname but I'm curious as to if this will work since it's APEX and not JavaScript. I have no experience in this and greatly appreciate any help. Here is my current JavaScript Code:
{!requireScript("/soap/ajax/22.0/connection.js")}
sforce.connection.search("FIND {{!Case.First_Name__c}* {!Case.Last_Name__c}} IN NAME FIELDS",
{
onSuccess : function(queryResult) {
var searchRecords = queryResult.getArray('searchRecords');
for (var i = 0; i < searchRecords.length; i++) {
var record = searchRecords[i].record;
if (record.type == 'User') {
if (confirm("A user with a similar name to {!Case.First_Name__c} {!Case.Last_Name__c} " +
"already exists.\n\nClick OK to browse to this record or Cancel to create a new record."))
window.location = record.Id;
else
throw "Create User";
}
}
} ,
onFailure : function(error) {
window.location = "/005/e?" +
"name_firstName={!JSENCODE( Case.First_Name__c )}" +
"&name_lastName={!JSENCODE( Case.Last_Name__c )}" +
"&Email={!JSENCODE( Case.Email__c )}" +
"&Username={!JSENCODE( Case.Email__c )}" +
"&Title={!JSENCODE( Case.Title__c )}" +
"&CompanyName={! Case.Company__c }" +
"&Division={!Case.Division_Region__c}" +
"&Phone={!Case.Phone__c}" +
"&CommunityNickName = nickName" +
"&Alias = alias" +
"&TimeZoneSidKey={!Case.Time_Zone__c}" +
"&LanguageLocaleKey={! Case.Language_Preference__c}" +
"&UserPermissions_0={! Case.Marketing_User__c }" +
"&UserPermissions_1={! Case.Offline_User__c }" +
"&MobilePhone={!JSENCODE( Case.Mobile_Phone__c )}" +
"&EmployeeNumber={!JSENCODE( Case.Employee_Number__c )}" +
"&Addressstreet={!JSENCODE( Case.Street_Address__c )}" +
"&Addresscity={!JSENCODE( Case.City__c )}" +
"&Addressstate={!JSENCODE( Case.State_Province__c )}" +
"&Addresszip={!JSENCODE( Case.Postal_Code__c )}" +
"&Addresscountry={! Case.Country__c }" +
"&00N20000000hUKg={! Case.Country__c }" +
"&00N200000020UJ5={! Case.Division__c }" +
"&defaultcurrencyisocode={! Case.Currency__c }" +
"&info_email={! Case.Salesforce_newsletter__c }" +
"&Profile=00eD0000001V2Vp" +
"&user_license_id=1002000000008Xw";
}
});
Here is the APEX code I found that I believe I need to add to the JavaScript code above to create the Alias and Nickname fields:
List<Case> Cases = [
Select Id
From Case
];
List<User> newUsers = new List<User>();
for (Contact contact : portalContacts) {
String alias = contact.LastName;
if (alias.length() > 8) {
alias = alias.substring(0, 8);
}
String nickName = contact.Email;
if (nickName.length() > 40) {
nickName = nickName.substring(0, 40);
}
}