You need to sign in to do that
Don't have an account?
wt35
Error in test class: System.DmlException: DUPLICATE_USERNAME
Hi community,
I am getting this error while running a test class, I don't really understand why because I am not even trying to insert a user, only opportunities...
Error message:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>Another user has already selected this username.<br>Please select another.: [Username] Stack Trace Class.CloseDuplicateOpportunitiesTest.test1: line 17, column 1
Class:
global class CloseDuplicateOpportunities{ webService static String closeDuplicateOpportunities(Id contextOppId) { Opportunity contextOpp; List<Opportunity> relevantOpps; String finalMessage; Profile userProfile = [SELECT Name FROM Profile WHERE Id = :Userinfo.getProfileId()]; /*********************************************************/ /*****************AUTHORIZED USER ************************/ /*********************************************************/ try{ contextOpp = [SELECT Id,AccountId,Owner.Name FROM Opportunity WHERE Id=:contextOppId]; } catch(Exception e){ System.debug('The following exception has occurred while trying to get the context opp: ' + e.getMessage()); } if ( (userProfile.Name.equals('NA Telesales Rep') || userProfile.Name.equals('NA Telesales Manager') || userProfile.Name.equals('NA Territory Manager - Outbound')) && contextOpp.Owner.Name == 'NA BS User' ) { List<Opportunity> oppsToUpdate = new List<Opportunity>(); oppsToUpdate.add(contextOpp); try{ relevantOpps = [SELECT Id,StageName,Reason_For_Closed_Lost__c FROM Opportunity WHERE AccountId = :contextOpp.AccountId AND IsClosed = false AND Owner.Name like 'NA BS User' AND Id != :contextOpp.Id AND Product_Target__c INCLUDES ('Advanced','Express Checkout Shortcut (ECS)','Pro')]; oppsToUpdate.addAll(relevantOpps); } catch(Exception e){ finalMessage = 'No relevant opportunities have been closed because either none were found or an error has happened.'; } if(oppsToUpdate.size() > 0) { for (Opportunity o : oppsToUpdate) { o.StageName = 'Closed Lost'; o.Reason_For_Closed_Lost__c = 'Close - TS Duplicate'; } System.debug('###########oppsToUpdate '+ oppsToUpdate); try{ update oppsToUpdate; finalMessage = relevantOpps.size() + ' similar opportunities have been closed in addition to this one'; } catch(Exception e){ finalMessage = 'No relevant opportunities have been closed'; } } } /*********************************************************/ /********************NON-AUTHORIZED USER *****************/ /*********************************************************/ else{ finalMessage = 'No relevant opportunities have been closed because you do not have an authorized profile or the opportunity is not owned by NA BS User.'; } return finalMessage; } }
Just change ur username in test class it will solve the problem......
All Answers
can you post here your test class?
because this exception is occurs because of test class code.
Sorry I want to provide the test class in the first class...here it is:
thanks
Just change ur username in test class it will solve the problem......
Yes that's what I did in the end and it worked.
Thanks