0

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);
    }
    }

1 Answer 1

1

Something like this might work for you. The Alias will be the first 8 characters or less from the name. The Nickname will be the first 40 characters or less before the "@" on the user's email.

Example: Name: Arnold Shwartzeneger
Email: [email protected]
Alias: Shwartze
Nickname: testing

Good luck!

    {!requireScript("/soap/ajax/22.0/connection.js")} 

sforce.connection.query("Select Id FROM User WHERE FirstName LIKE '{!Case.First_Name__c}%' AND LastName = '{!Case.Last_Name__c}'",
{ 
    onSuccess : function(queryResult) 
    { 
        var searchRecords = queryResult.getArray('records');
        if(searchRecords.length > 0)
        {
            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 
        {
            var alias = '{!Case.Last_Name__c}';
            if (alias.length > 8) {
                alias = alias.substring(0, 8);
            }

            var nickName = "{!IF(CONTAINS(Case.Email__c,'@'),LEFT(Case.Email__c, FIND('@',Case.Email__c)),Case.Email__c)}";
            if (nickName.length > 40) {
                nickName = nickName.substring(0, 40);
            }

            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";
        }
    },
    onFailure : function(error)
    {
        alert("Error: "+error);
    }
});
5
  • It seems like I am really close now, thank you! I had to add a parenthesis at the end of the CONTAINS statement you created(var nickName = "{!IF(CONTAINS(Case.Email__c,'@'),LEFT(Case.Email__c, FIND('@',Case.Email__c),Case.Email__c))}";), and now I'm getting an error about how many parameters that should be in the LEFT() function. With the code you graciously gave me it says this: "Error: Incorrect number of parameters for function 'LEFT()'. Expected 2, received 3" Mar 17, 2015 at 18:47
  • I missed a parenthesis, but in another spot. I just edited the post. Could you try again?
    – slashApex
    Mar 17, 2015 at 19:03
  • Thanks Luis. I updated the code now with no errors, but it doesn't run when I click the button.. :( Mar 17, 2015 at 19:35
  • Try this new version. I changed it to use a regular SOQL query and I reorganized a little, but other than that it is pretty much the same. I believe the problem was with the FIND statement before.
    – slashApex
    Mar 17, 2015 at 23:26
  • Ah, I'm so close with this. Everything is mapping perfectly if there is not already a user in the DB but if there is a User in the DB and I want to take the user to the User's page that's already in the DB, I get the error 'Cannot read property 'Id' of undefined' Mar 23, 2015 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .