• ctone
  • NEWBIE
  • 0 Points
  • Member since 2007

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies
I'm hoping that some one can spend a few minutes and verify the results that I am seeing here and hopefully, before I pull out what little hair I have left, tell me where I am going wrong.

I am attempting to swap a user's profile using Apex Code but I am receiving this error message:
Code:
System.DmlException: Update failed. First exception on row 0 with id 005500000011fHiAAI; first error: FIELD_INTEGRITY_EXCEPTION, 
This profile is used by a user who is a delegated admin and must have the view setup permission: Profile ID: [ProfileId]
 
I do not have any delegated admins defined AND the two profiles in question are unused (except by the user in this test case). 

Here is the Apex Class.  The test method runs w/o issue.
Code:
global class ProfileSwap {
    
    public static String origProfile = 'Sales-Test-A';
    public static String altProfile = 'Sales-Test-B';
    public static String targetUserEmail = 'someone@somewhere.com';
    
    public ProfileSwap(){}
    
    /* swaps current profile for a pre-determined alternate */
    public void swap(ID userID){
        
        //-- load current user profile
        User u = [select id, profileId from User where id =: userId];
        if( u == null ) throw new ValidationException('Unable to load user with id '+userId);
        System.debug('>>>>>> Loaded user '+u);
        
        Map<ID,ID> profileMap = getProfileMap();
        if( !profileMap.containsKey(u.profileId) ) throw new ValidationException('Unknow profile, cannot swap');
         
        //-- update w/ new profile
        u = new User(id=u.id, profileId = profileMap.get(u.profileId));
        System.debug('>>>>>> Updating user: '+u);
        update u;
    }
    
    /* returns mapping between current and target profiles */
    public static Map<ID, ID> getProfileMap(){
        //-- load target profiles
        ID origProfileId = [select Id from Profile where name =: origProfile].id;
        ID altProfileId = [select Id from Profile where name =: altProfile].id;
        
        Map<ID,ID> profileMap = new Map<ID,ID>();
        profileMap.put(origProfileId, altProfileId);
        profileMap.put(altProfileId, origProfileId);
        
        return profileMap;
    }

    public static testMethod void test(){
        
        User u = [select id, profileId, profile.name from User where email =: targetUserEmail];
        
        //-- mapping of profile to swap from -> to
        Map<ID,ID> profileMap = getProfileMap();
        
        //-- expected resulting id
        ID targetId = profileMap.get(u.profileId);
        String startProfile = u.profile.name;
        
        //-- swap profiles
        new ProfileSwap().swap(u.id);
        
        //-- verify
        u = [select id, profileId, profile.name from User where email =: targetUserEmail];
        System.debug('Orig Profile: '+startProfile+'; Swapped Profile: '+u.profile.name);
        System.assert(targetId == u.profileId, 'Expected target profile "'+targetId+'", got "'+u.profileId+'"');
    }
    
    webservice static void doIt(String userId){
        new ProfileSwap().swap(userId);
    }
}
 

The 'doIt' method is invoked by an OnClick javascript link on the user's home page.  Adding 'without sharing' to the ProfileSwap class does not affect anything.
Code:
{!requireScript("/soap/ajax/14.0/connection.js")}
{!requireScript("/soap/ajax/14.0/apex.js")}
  try{
        var userId = "{!$User.Id}";
 sforce.apex.execute("ProfileSwap", "doIt", {userId:userId});

  } catch(e){ alert(e); }

Thanks for your time.

--Chris
  • October 09, 2008
  • Like
  • 0
I am trying to switch a user's profile using Apex Code however I'm getting the following error message:

Code:
Error: This profile is used by a user who is a delegated admin and must have the view setup permission

I do not believe I have any delegated administrators defined (Admin Setup> Security Controls> Delegated Administration is empty).

Am I missing something?

Thanks
--Chris
  • October 04, 2008
  • Like
  • 0
Hello-

I'm hoping some one can help me because I can't see what I am doing wrong here - in fact, this test case was working a few months ago but as of today it raises an exception.

All I am doing is creating an Opp, adding a line item, and then trying to delete that Opp.  At this point, I do not have any triggers activated

Code:
Account a = new Account(name = 'Test Account A');
insert a;
  
Opportunity opp = new Opportunity(name='Test Opp A', stageName='New', accountId = a.id, closeDate = Date.today(), Pricebook2Id = pbId);
insert opp;
  
//-- works OK
delete opp;
  
opp = new Opportunity(name='Test Opp A', stageName='New', accountId = a.id, closeDate = Date.today(), Pricebook2Id = pbId);
insert opp;

OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.id, UnitPrice=500.00, Quantity=1, pricebookentryid = pbeNames.get('SomeProduct'));
insert oli;
  
//-- fails: UNKNOWN_EXCEPTION
delete opp;

 The debug log shows the following:

20081001213343.803:Class.Test_Triggers.testOppMod: line 212, column 9: Delete: SOBJECT:Opportunity
20081001213343.803:Class.Test_Triggers.testOppMod: line 212, column 9:     DML Operation executed in 20 ms
System.DmlException: Delete failed. First exception on row 0 with id 006T0000003xKmiIAE; first error: UNKNOWN_EXCEPTION, assertion failed

 
Any one have any ideas?

--Chris

  • October 01, 2008
  • Like
  • 0
I'm hoping that some one can spend a few minutes and verify the results that I am seeing here and hopefully, before I pull out what little hair I have left, tell me where I am going wrong.

I am attempting to swap a user's profile using Apex Code but I am receiving this error message:
Code:
System.DmlException: Update failed. First exception on row 0 with id 005500000011fHiAAI; first error: FIELD_INTEGRITY_EXCEPTION, 
This profile is used by a user who is a delegated admin and must have the view setup permission: Profile ID: [ProfileId]
 
I do not have any delegated admins defined AND the two profiles in question are unused (except by the user in this test case). 

Here is the Apex Class.  The test method runs w/o issue.
Code:
global class ProfileSwap {
    
    public static String origProfile = 'Sales-Test-A';
    public static String altProfile = 'Sales-Test-B';
    public static String targetUserEmail = 'someone@somewhere.com';
    
    public ProfileSwap(){}
    
    /* swaps current profile for a pre-determined alternate */
    public void swap(ID userID){
        
        //-- load current user profile
        User u = [select id, profileId from User where id =: userId];
        if( u == null ) throw new ValidationException('Unable to load user with id '+userId);
        System.debug('>>>>>> Loaded user '+u);
        
        Map<ID,ID> profileMap = getProfileMap();
        if( !profileMap.containsKey(u.profileId) ) throw new ValidationException('Unknow profile, cannot swap');
         
        //-- update w/ new profile
        u = new User(id=u.id, profileId = profileMap.get(u.profileId));
        System.debug('>>>>>> Updating user: '+u);
        update u;
    }
    
    /* returns mapping between current and target profiles */
    public static Map<ID, ID> getProfileMap(){
        //-- load target profiles
        ID origProfileId = [select Id from Profile where name =: origProfile].id;
        ID altProfileId = [select Id from Profile where name =: altProfile].id;
        
        Map<ID,ID> profileMap = new Map<ID,ID>();
        profileMap.put(origProfileId, altProfileId);
        profileMap.put(altProfileId, origProfileId);
        
        return profileMap;
    }

    public static testMethod void test(){
        
        User u = [select id, profileId, profile.name from User where email =: targetUserEmail];
        
        //-- mapping of profile to swap from -> to
        Map<ID,ID> profileMap = getProfileMap();
        
        //-- expected resulting id
        ID targetId = profileMap.get(u.profileId);
        String startProfile = u.profile.name;
        
        //-- swap profiles
        new ProfileSwap().swap(u.id);
        
        //-- verify
        u = [select id, profileId, profile.name from User where email =: targetUserEmail];
        System.debug('Orig Profile: '+startProfile+'; Swapped Profile: '+u.profile.name);
        System.assert(targetId == u.profileId, 'Expected target profile "'+targetId+'", got "'+u.profileId+'"');
    }
    
    webservice static void doIt(String userId){
        new ProfileSwap().swap(userId);
    }
}
 

The 'doIt' method is invoked by an OnClick javascript link on the user's home page.  Adding 'without sharing' to the ProfileSwap class does not affect anything.
Code:
{!requireScript("/soap/ajax/14.0/connection.js")}
{!requireScript("/soap/ajax/14.0/apex.js")}
  try{
        var userId = "{!$User.Id}";
 sforce.apex.execute("ProfileSwap", "doIt", {userId:userId});

  } catch(e){ alert(e); }

Thanks for your time.

--Chris
  • October 09, 2008
  • Like
  • 0
Hi All,

I am an newbie and would appreciate your help in the following:

Once a week I would like to take several records (those who answer a simple date criteria) and duplicate these.
When duplicating, I would like to edit a certain field (again, in an automatic way, one change for all, not per record).

I assume an Apex trigger is needed, but I need a little direction pointing. anyone?

Thanks
  • October 07, 2008
  • Like
  • 0
http://wiki.apexdevnet.com/index.php/LeadDupePreventerTests.apex
http://wiki.apexdevnet.com/index.php/LeadDupePreventerTests.apexhttp://wiki.apexdevnet.com/index.php/LeadDupePreventerTests.apex
Produces the following error when I try to implement:

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 47 column 45



Message Edited by JAW99 on 10-06-2008 12:09 PM
  • October 06, 2008
  • Like
  • 0
Hello.  Is there any way in Apex to drop pending workflow actions on a Lead so that it can be converted?  The workflow actions are time-based email reminders.

Thanks
David
My client has set up a custom object named Industry_Type__c that is a child object of the Account record.  The child relationship name is "IndustryTypes."  To make things confusing, this custom object has a field that is also named Industry_Type__c.

I'm writing some unit test code like this:
Code:
Id acctID = lcr.getAccountId();
Account acct = [select Id, Name, (select Industry_Type__c from IndustryTypes__r) 
from account where Id = :acctID];

 No errors.  The problem is, I can't figure out how to reference the Industry Type field value using dotted notation or any other way.

Here are the things I've tried:
String testval = acct.Industry_Type__c.Industry_Type__c;
String testval = acct.IndustryTypes.Industry_Type__c;
String testval = acct.IndustryTypes__r.Industry_Type__c;
String testval = acct.IndustryType__r.Industry_Type__c;
Industry_Type__c[] test = acct.IndustryTypes;

The first 4 attempts give me "Invalid foreign key relationship" errors, and the last one throws "Invalid Field IndustryTypes".

I've read page 28 and page 53 of the Apex Reference Manual and read the online SOQL doc but I still don't understand how I can do this.  Do I need to use queryResult()?

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content%2Fsforce_api_calls_soql.htm|SkinName=webhelp

Thanks
David