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
priya bhawna shettypriya bhawna shetty 

apex class related to user and profile condition

hi guys 
hope all are doing well.pls help me out with this small issue.
I created a apex class where i fetched query like user u1=standard platform user profile and user u2=system admin profile,so my condition is ,i want to gave user u1 to run Annual revenue field and user u2 to run phone field from account.

heres my code:
public class runasdemo2{

user u1=[select user.profile.name, user.Username from user where profile.name='Standard Platform User' and user.username='aasalesforce.consultant11@gmail.com'];
user u2=[select user.profile.name, user.Username from user where profile.name='System Administrator' and user.username='salesforce.learner999@gmail.com'];

{
  if(u1.contains(profile.name='Standard Platform User'))
  {
   account a1=new account();
   a1.name='testaccount';
   a1.annualrevenue=12345;
   insert a1;
  }else
  {
   account b1=new account();
   b1.name='salesforce';
   b1.phone=1234124;
   insert b1;
   }
   
}
}

pls help me out.
tnks 
justin_sfdcjustin_sfdc
Hi Priya,

is this for your test class.
If so then just create two users and two accounts. That should work.

Thanks!
Himanshoo SethHimanshoo Seth
Hi PRiya, 

Can you please elaboreate more. What do you mean by 'user u1 to run Annual revenue field and user u2 to run phone field from account.' Also what is the purpose you are trying to achieve.

Regards
Himanshoo  
Sravan R PinnintiSravan R Pinninti
Hi Priya,

I guess you need to use userinfo class userinfo.getuserid() to get the profile of the current running user who is invoking the apex class. Then write your conditions in sucha way that if the current users profile is 'System Administrator' populatephone field and if the profile is 'Standard Platform user' populate the Revenue field.

Thank You.

Regards,
Sravan.
priya bhawna shettypriya bhawna shetty
helo guys
iam not worried about test classes or about getting profile information from user.
iam getting an error in if condition i used.i want to overcome that error.

heres my code:
public class runasdemo2{

user u1=[select user.profile.name, user.Username from user where profile.name='Standard Platform User' and user.username='aasalesforce.consultant11@gmail.com'];
user u2=[select user.profile.name, user.Username from user where profile.name='System Administrator' and user.username='salesforce.learner999@gmail.com'];

{
  if(u1.contains(profile.name='Standard Platform User'))
  {
   account a1=new account();
   a1.name='testaccount';
   a1.annualrevenue=12345;
   insert a1;
  }else
  {
   account b1=new account();
   b1.name='salesforce';
   b1.phone=1234124;
   insert b1;
   }
  
}
}

error:Error: Compile Error: Expression cannot be assigned at line -1 column -1
i dont know what this mean.
pls help me out.
justin_sfdcjustin_sfdc
Hi Priya,

This is how you should begin your test class;

@isTest
public class TestUtil {

   public static void createTestAccounts() {
      // Create some test accounts
   }

   public static void createTestContacts() {
      // Create some test contacts
   }

}

You could create all your accounts and users info into one method and apply the logic in the same like below:
@isTest
public class runClassDemo2{

   public static void testMethod() {
     user u1=[select user.profile.name, user.Username from user where profile.name='Standard Platform User' and user.username='aasalesforce.consultant11@gmail.com'];
user u2=[select user.profile.name, user.Username from user where profile.name='System Administrator' and user.username='salesforce.learner999@gmail.com'];

{
  if(u1.contains(profile.name='Standard Platform User'))
  {
   account a1=new account();
   a1.name='testaccount';
   a1.annualrevenue=12345;
   insert a1;
  }else
  {
   account b1=new account();
   b1.name='salesforce';
   b1.phone=1234124;
   insert b1;
   }
 
}
   }

}