function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ppatppat 

Test Case? using UserInfo.getProfileId() in trigger

I have a trigger which validates based on the user profile Id. Trigger works fine in sandbox. How do I write test case to evaluate different profiles as test class always finds my profile id while executing the trigger

if (UserInfo.getProfileId()  != 'Salesforce Administrator') // used Id to compare
{
// Code here
}

any pointers would be great.

ppatppat
No one ?
learnSFlearnSF

Did you find out answer?

I am having same problem to write test case to cover userInfo.getUserId().

learnSFlearnSF

I find out same way.

Profile pro=new Profile(Name=' Sales User1');

MyControllerExtension ext1 = new MyControllerExtension(new Account(RecordTypeId = '012300000000AGr'),pro);

You pass profile object in your constructor.
ppatppat
I have not found the answer yet  :smileysad:  Let me know if your approach works
learnSFlearnSF

I tried with controller and it covered till 80%.

So you need to create profile object.set Id to this object in your test method and pass this object with constructor. this way you will get userinfo Id in class.

ppatppat
Learnsf ,

Thanks for the reponse.I am assuing you are using class with visual pages I am using userinfo method's in trigger and not exactly sure on how to use controllers for getting coverage for trigger .. :smileysad:

Can you point me to documentation where I can find some details on what you are trying ?

Thanks in advance.

-Pramod
learnSFlearnSF

True,

I am using controller with visual pages.

Here is my controller with test method.

Code:
public class MyControllerExtension {
 private  Account acct;
 private  RecordType recordType;
 private Profile profile;

    public MyControllerExtension(ApexPages.StandardController stdController) {
     
      this.acct  = [select id, name, site,RecordTypeId from Account where id =
                       :System.currentPageReference().getParameters().get('id')];
      this.profile=[Select p.Name From Profile p where p.Id=:UserInfo.getProfileId()];
      init();
  
    }
      
    public MyControllerExtension(Account acct,Profile profile) {
     
      this.acct = acct;
      this.profile=profile;
      init();
      
    }
    
    private void init() {
     
      this.recordType=[Select r.Name, r.Id From RecordType r where r.Id=:acct.RecordTypeId];
      
    }
    
  public PageReference doRedirect() {
      if (profile.Name =='Sales User'&& recordType.Name=='Dealer - Customer') {
        PageReference p = Page.TabbedAccountTest;
        p.setRedirect(true);
        p.getParameters().put('id', acct.id);
        return p;
      } else {
        return null;
      }
    }
       public static testMethod void testController() {
        Profile pro=new Profile(Name='Cars.com Sales User1');
     MyControllerExtension ext1 = new MyControllerExtension(new Account(RecordTypeId = '012300000000AGr'),pro);
   Test.setCurrentPageReference(new PageReference('Page.TabbedAccountTest')); 
      PageReference p1 =ext1.doRedirect();
      System.assert(p1 == null);
        Profile pro1=new Profile(Name='Cars.com Sales User');
     MyControllerExtension ext = new MyControllerExtension(new Account(RecordTypeId = '012300000000AGr'),pro1);
   Test.setCurrentPageReference(new PageReference('Page.TabbedAccountTest')); 
      PageReference p2 =ext.doRedirect();
     //   System.assert(p1 == Page.TabbedAccountTest );
     //   System.assert(ext.getName()=='Dealer - Customer');
 // System.assert(ext.getId()!='012300000000AGr');
    }
}
 


 hope this helps.

And here is link where I got idea.

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=692


 

ppatppat
Thanks for the quick response. Let me see if I can get some pointers from the references you provided.