Skip to main content
meghna m ha preguntado en #Apex
Can any one help me out. How to call 'n' no of USER profiles  and add error method in test class.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 classAny 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.');

}

}

}

}

}

TestClass :

@isTest

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='standarduser@testorg.com',

EmailEncodingKey='UTF-8',

LastName='Testing',

LanguageLocaleKey='en_US',

LocaleSidKey='en_US',

ProfileId = SalesNService.Id,

TimeZoneSidKey='America/Los_Angeles',

UserName='standarduser@testorg.com'

);

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;

}

}

 
8 respuestas
  1. 22 abr 2015, 10:43
    Hi Nikkey,

    I have removed the redundant lines. Check now, and place an appropriate profile name in the query.

    @isTest

    private class Oli_multiple_Products_TestClass{

    static testmethod void ValidateOlimultipleproducts(){

    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];

    User u = new User(Alias = 'standt', Email='salesnsvc123@abcorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='salesnsvc3214@torg.com');

    System.runAs(u) {

    Date closeDt = Date.Today();

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

    try{

    insert ooli1;

    }

    catch(Exception e) {

    Boolean exceptionThrown = e.getMessage().contains('A Product can not be added more than once to the Opportunity.')? true : false;

    System.assertEquals(exceptionThrown, true);

    }

    }

    }

    }

    Thanks

    AR

    If you find the reply useful that solves your problem then please mark it as best answer.

     
0/9000