I have solved this error:
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.
Another user has already selected this username.
Please select another.: [Username]"
By creating this:
`String orgId = UserInfo.getOrganizationId();
String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
String uniqueName = orgId + dateString + randomInt;`
And then I use this combination to obtain a unique username.
`Profile p = [select id FROM Profile WHERE Name ='Administrator'];
User user1 = new User();
user1.Username = uniqueName + '@test' + orgId + '.org';
user1.Email = uniqueName + '@test' + orgId + '.org';
user1.LastName = 'LastName1';
user1.FirstName = 'FirstName1';
user1.Alias = uniqueName.substring(18, 23);
user1.CommunityNickname ='use1';
user1.ProfileId =p.id;
user1.EmailEncodingKey='UTF-8';
user1.LanguageLocaleKey='en_US';
user1.LocaleSidKey='en_US';
user1.TimeZoneSidKey='America/Los_Angeles';
user1.Country = 'US';
user1.Title = 'Territory Manager';
insert user1;`
If I try to transfer my change set now I have no problems with the username.
I repeat this for the second user but I created a second set of Integer radomInt and String uniqueName.
Integer randomInt2 = Integer.valueOf(math.rint(math.random()*2000000));
I notice that if I use the same randomInt for my second username the random composition of
String uniqueName = orgId + dateString + randomInt;
will be the same than the one for the first user and it's not unique.
But using String uniqueName2 = orgId + dateString + randomInt2;
for the second user will works, creating a new username that is unique.
This is a solution. Any comments on this?
Edit 11-21-14
So far there is not problems and the code has been active for a few months. We are going to modify all test classes and add a test factory class to avoid issues like this one.
I see that the code where you find actual accounts is not returned in wrapper format
public pagereference Search()
{
String finalSearchValue = '%' + fullname + '%';
Accounts = [Select Id, Name, Active__c FROM Account
where Name like :finalSearchValue];
wraplist = new list<wrapData>();
for(Account a: Accounts)
{
wrapList.add(new wrapData(a));
}
return null;
}
Same way apply for filter method as well.
Best Answer
Your code as written uses
UserWWID
in the redirect, which is set inGetUserId()
, but that method is never called. If it were called, you'd get an error on the last line ofGetUserId()
, since you are trying to reference the Name field, which wasn't included in your query. Here's a simplified version of your code which I think will work, but I'm making the assumption that the field you need in your constructed URL is actually User.WWID__c.A few notes:
ControllerName
in the sample code above.user where id = :currentUserId
is always safe in a controller, since there's always a user. Just be sure the user has FLS on the WWID__c field, or it will come back null. Since activeUser is a public property, you can stick{!activeUser.wwid__c}
in your VF page temporarily for debugging, to make sure your are getting awwid__c
value./
or a?param=
at the end of the URL; I'm guessing you need one or the other.setRedirect()
; per the docs it isn't needed when the URL refers to an external website.Update: I've updated the PageRedirect() method, so that if User.WWID__c is null, the method returns null. Since you are calling this as an
action
on your VF Page, if the method returns null, it doesn't redirect, and just outputs the VF Page. Along with this change, add something to your VF page to show what happened, since it will only be shown if the redirect fails. For example:Also added a debug statement to output the user data.