Can any one help me out how to call 'n' no of USER profiles in test class.Any help very much appreciated.

Trigger :

trigger oli_multiple_products_before_insert on OpportunityLineItem (before insert) {

 Id userProfileId = userinfo.getProfileId();
  String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;

  if( userProfileName != 'System Administrator' &&
      userProfileName !='Custom Marketing Users 10K 25K '&&
      userProfileName !='Customer Service User'&&
      userProfileName !='Fulfillment User'
     ) {

    for (OpportunityLineItem oli : Trigger.new) {
        if (Trigger.isInsert) {

            Integer line_Count = [SELECT COUNT()
                                    FROM OpportunityLineItem o
                                    WHERE o.OpportunityId = :oli.OpportunityId
                                    AND o.PriceBookEntryId = :oli.PriceBookEntryId  ];

            if (line_Count > 0) {
                oli.addError('A Product can not be added more than once to the Opportunity.');

Test class

    private class Oli_multiple_Products_TestClass{
    static testmethod void ValidateOlimultipleproducts(){
    Date closeDt = Date.Today();

//Find user with Profile = Sales and Service
        Profile SalesNService = [Select id from Profile where Name = 'Sales and Service' limit 1];
        User u = new User(
            Alias = 'standt', 
            Email='[email protected]',
            ProfileId = SalesNService.Id,
            UserName='[email protected]'

    Account acc = new Account (Name ='TestClassAccountOpportunitySegment', Region__c = 'AM', Persona__C = 'Artisan');
    //System.debug('AccountOpportunitySegment before insert of new Account: ' + acc.segment__C);
    insert acc;

    Opportunity opp = new opportunity (Name ='TestclassAccountOpportunitySegment', AccountId= acc.Id, StageName = 'Prospecting', 
                                       CloseDate = closeDt, ForecastCategoryName = 'Pipeline');
    insert opp;                                 

    OpportunityLineItem ooli = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFqAAO');
    insert ooli;

    OpportunityLineItem ooli1 = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFzAAO');
    insert ooli1;

  • So bulk testing?
    – EricSSH
    Commented Apr 21, 2015 at 18:35
  • @ EricSSH:In the trigger i do have users other than those users it should throw an error.so how do i use the users and the add error message in test class.Any help very much appreciated.
    – nikkey
    Commented Apr 21, 2015 at 18:45
  • If you wanted to test Error messages you need to use System.Asset and I believe it's getMessage(), you should be to find some documentation online
    – EricSSH
    Commented Apr 22, 2015 at 15:56

2 Answers 2


So if I understand you correctly, you just want to select 'n' profiles?

List<Profile> profs = [select Id, Name from profile LIMIT n]

Or if you wanted to exclude the ones from your trigger it would look more like:

Set<String> bad = new Set<String>(){'System Administrator, Fulfillment User'};
//Didn't test that syntax, but you get the idea
List<Profile> profs = [select Id, Name from profile where name not in :bad LIMIT n]

Your question could use some cleanup, but it seems you may be looking for the System.runAs method [link]. Create or query for a User with the profile that you desire, then pass the User record into runAs.

    // do stuff

You must log in to answer this question.

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