• Adam Drissel
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Alright...so the basics are this: I have scoured the blogs, stackexchange posts, and any other resource I could find looking for a simple way to access use the REST API to access object metadata.  I just cannot find anything.  Heck, I can't even get an access token to return.

Can anyone provide me with a minimal example of some code I can execute anonymously to receive an object's metadata?  This is the code I currently have, but if it is more complex than necessary please snip whatever is unnecessary out of your response(s).
 
String clientId = <my_client_id>;
String clientSecret = <my_client_secret>';
String username = <my_login>;
String password = <my_password>;
String prodUri = 'https://login.salesforce.com';
String testUri = 'https://test.salesforce.com';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(testUri+'/services/oauth2/token/');
req.setBody('grant_type=password' +
            '&client_id=' + clientId +
            '&client_secret=' + clientSecret +
            '&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
            '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
Http binding = new Http();
HttpResponse res = binding.send(req);

system.debug('--------------------> '+res);
system.debug('--------------------> '+req);

The responses I am getting for any modifications I have attempted thus far have varied between 401 (unauthorized), 404 (not found), and 405 (method not available).  Please help.

Thanks!
Here is my code:

Main Class
public class GetOpptyOwnerMgrEmailHandler{
    
    public List<Id> opptyIds {get;set;}
    public List<Opportunity> allOpptys {get;set;}

    public void getMgrEmail(List<Opportunity> opptys){
        
        /*
         * Set variables
         */
        
        getEligibleOpptys(opptys);
        getAllOpptys(opptyIds);
        updateAllOpptys(allOpptys);
    }
    
    private void getEligibleOpptys(List<Opportunity> opptys){
        
        opptyIds = new List<Id>();
        
        for(Opportunity oppty : opptys){
            
            if(oppty.Managers_Email_Address__c == null){
                opptyIds.add(oppty.Id);
            }
        }
    }
    
    private void getAllOpptys(List<Id> opptyIds){
        
        allOpptys = [SELECT Id,Owner.Manager.Email,Owner.Manager.Id FROM Opportunity WHERE Id IN :opptyIds];
    }
    
    private void updateAllOpptys(List<Opportunity> allOpptys){
        
        for(Opportunity oppty : allOpptys){
            
            oppty.Managers_Email_Address__c = oppty.Owner.Manager.Email;
            update oppty;

            system.debug('------------------> here is oppty.Managers_Email_Address__c value: '+oppty.Managers_Email_Address__c);
        }
    }
}

Test Class
@isTest
public class GetOpptyOwnerMgrEmailHandlerTest {
        
    /*
    * Method to test "GetOpptyOwnerMgrEmail" trigger
    */

    @isTest
    public static void testGetOpptyOwnerMgrEmail(){
        
        /*
         * Create two test Users, one manager, one standard
         */
        
        User mgr = DataFactory.createUserMgr();
        insert mgr;
        User u = DataFactory.createUser(); 
        u.ManagerId = mgr.Id;
        insert u;
        
        System.assertEquals(u.ManagerId,mgr.Id,'The managerId didn\'t set properly');
        
        /*
         * Create test Opportunity
         */
        
        Opportunity opp = DataFactory.createOpp();
        opp.Type = 'New Account';
        opp.StageName = 'Prospecting';
        opp.OwnerId = u.Id;
        opp.Competitors__c = 'SaaS Corporate';
        opp.Current_Vendor__c = 'SaaS Corporate';
        opp.Close_Reason__c = 'Features';
        
        System.RunAs(u){
            
            insert opp;
        
            system.debug('------------------> here is the opp value: '+opp);
            system.debug('------------------> here is opp.Managers_Email_Address__c value: '+opp.Managers_Email_Address__c);

            System.assertEquals(opp.Managers_Email_Address__c,mgr.Email,'The email addresses aren\'t the same');
        }
    }
}

DataFactory Class
public class DataFactory {

    /*
     * Method to create test Standard User
     */
    
    public static User createUser(){
        
        Id proId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;
        UserRole ur = createUR();
        insert ur;
        User u = new User(
            alias = 'u1', 
            email = 'testUser@company.com',
            emailencodingkey = 'UTF-8',
            firstname = 'Test',
            lastname='User',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = proId,
            isActive = true,
            username = 'testUser@company.com',
            userRoleId = ur.id
        );
        return u;
    }

    /*
     * Method to create test User Manager
     */
    
    public static User createUserMgr(){
        
        Id proId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;
        UserRole ur = createUR();
        insert ur;
        User u = new User(
            alias = 'uMgr', 
            email = 'testUserMgr@company.com',
            emailencodingkey = 'UTF-8',
            firstname = 'Test',
            lastname='UserMgr',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = proId,
            isActive = true,
            username = 'testUserMgr@company.com',
            userRoleId = ur.Id
        );
        return u;
    }

    /*
     * Method to create UserRole
     */
    
    public static UserRole createUR(){
    
        // Method creates a UserRole that will cause the GEO to evaluate to "NA"
        
        UserRole ur = new UserRole();
        ur.Name = 'NA TestRole';
        ur.OpportunityAccessForAccountOwner = 'None';        
        return ur;
    }

    /*
     * Method to create test Opportunity
     */
    
    public static Opportunity createOpp(){
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.CurrencyIsoCode = 'USD';
        opp.CloseDate = date.parse('1/1/2199');
        opp.Managers_Email_Address__c = null;
        return opp;
    }
}

My System.assertEquals is evaluating to false.  When I debug the value at the very end of the main class it is set properly.  But, it isn't populating to the opp variable in the test class.  What am I missing here?


 
Hello all,

When loading only select Opportunities I see the following error message at the bottom:

User-added image
The reason I am posting this here is because there are some SOQL queries running in the background that may or may not be affecting it.  Mainly I am wanting to know if anyone has seen this issue in the past (on a per-record basis) and what was done to resolve it.  Or, if you have any troubleshooting ideas that would be amazing as well.

Thanks!!
 
Alright...so the basics are this: I have scoured the blogs, stackexchange posts, and any other resource I could find looking for a simple way to access use the REST API to access object metadata.  I just cannot find anything.  Heck, I can't even get an access token to return.

Can anyone provide me with a minimal example of some code I can execute anonymously to receive an object's metadata?  This is the code I currently have, but if it is more complex than necessary please snip whatever is unnecessary out of your response(s).
 
String clientId = <my_client_id>;
String clientSecret = <my_client_secret>';
String username = <my_login>;
String password = <my_password>;
String prodUri = 'https://login.salesforce.com';
String testUri = 'https://test.salesforce.com';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(testUri+'/services/oauth2/token/');
req.setBody('grant_type=password' +
            '&client_id=' + clientId +
            '&client_secret=' + clientSecret +
            '&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
            '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
Http binding = new Http();
HttpResponse res = binding.send(req);

system.debug('--------------------> '+res);
system.debug('--------------------> '+req);

The responses I am getting for any modifications I have attempted thus far have varied between 401 (unauthorized), 404 (not found), and 405 (method not available).  Please help.

Thanks!
Here is my code:

Main Class
public class GetOpptyOwnerMgrEmailHandler{
    
    public List<Id> opptyIds {get;set;}
    public List<Opportunity> allOpptys {get;set;}

    public void getMgrEmail(List<Opportunity> opptys){
        
        /*
         * Set variables
         */
        
        getEligibleOpptys(opptys);
        getAllOpptys(opptyIds);
        updateAllOpptys(allOpptys);
    }
    
    private void getEligibleOpptys(List<Opportunity> opptys){
        
        opptyIds = new List<Id>();
        
        for(Opportunity oppty : opptys){
            
            if(oppty.Managers_Email_Address__c == null){
                opptyIds.add(oppty.Id);
            }
        }
    }
    
    private void getAllOpptys(List<Id> opptyIds){
        
        allOpptys = [SELECT Id,Owner.Manager.Email,Owner.Manager.Id FROM Opportunity WHERE Id IN :opptyIds];
    }
    
    private void updateAllOpptys(List<Opportunity> allOpptys){
        
        for(Opportunity oppty : allOpptys){
            
            oppty.Managers_Email_Address__c = oppty.Owner.Manager.Email;
            update oppty;

            system.debug('------------------> here is oppty.Managers_Email_Address__c value: '+oppty.Managers_Email_Address__c);
        }
    }
}

Test Class
@isTest
public class GetOpptyOwnerMgrEmailHandlerTest {
        
    /*
    * Method to test "GetOpptyOwnerMgrEmail" trigger
    */

    @isTest
    public static void testGetOpptyOwnerMgrEmail(){
        
        /*
         * Create two test Users, one manager, one standard
         */
        
        User mgr = DataFactory.createUserMgr();
        insert mgr;
        User u = DataFactory.createUser(); 
        u.ManagerId = mgr.Id;
        insert u;
        
        System.assertEquals(u.ManagerId,mgr.Id,'The managerId didn\'t set properly');
        
        /*
         * Create test Opportunity
         */
        
        Opportunity opp = DataFactory.createOpp();
        opp.Type = 'New Account';
        opp.StageName = 'Prospecting';
        opp.OwnerId = u.Id;
        opp.Competitors__c = 'SaaS Corporate';
        opp.Current_Vendor__c = 'SaaS Corporate';
        opp.Close_Reason__c = 'Features';
        
        System.RunAs(u){
            
            insert opp;
        
            system.debug('------------------> here is the opp value: '+opp);
            system.debug('------------------> here is opp.Managers_Email_Address__c value: '+opp.Managers_Email_Address__c);

            System.assertEquals(opp.Managers_Email_Address__c,mgr.Email,'The email addresses aren\'t the same');
        }
    }
}

DataFactory Class
public class DataFactory {

    /*
     * Method to create test Standard User
     */
    
    public static User createUser(){
        
        Id proId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;
        UserRole ur = createUR();
        insert ur;
        User u = new User(
            alias = 'u1', 
            email = 'testUser@company.com',
            emailencodingkey = 'UTF-8',
            firstname = 'Test',
            lastname='User',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = proId,
            isActive = true,
            username = 'testUser@company.com',
            userRoleId = ur.id
        );
        return u;
    }

    /*
     * Method to create test User Manager
     */
    
    public static User createUserMgr(){
        
        Id proId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;
        UserRole ur = createUR();
        insert ur;
        User u = new User(
            alias = 'uMgr', 
            email = 'testUserMgr@company.com',
            emailencodingkey = 'UTF-8',
            firstname = 'Test',
            lastname='UserMgr',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = proId,
            isActive = true,
            username = 'testUserMgr@company.com',
            userRoleId = ur.Id
        );
        return u;
    }

    /*
     * Method to create UserRole
     */
    
    public static UserRole createUR(){
    
        // Method creates a UserRole that will cause the GEO to evaluate to "NA"
        
        UserRole ur = new UserRole();
        ur.Name = 'NA TestRole';
        ur.OpportunityAccessForAccountOwner = 'None';        
        return ur;
    }

    /*
     * Method to create test Opportunity
     */
    
    public static Opportunity createOpp(){
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.CurrencyIsoCode = 'USD';
        opp.CloseDate = date.parse('1/1/2199');
        opp.Managers_Email_Address__c = null;
        return opp;
    }
}

My System.assertEquals is evaluating to false.  When I debug the value at the very end of the main class it is set properly.  But, it isn't populating to the opp variable in the test class.  What am I missing here?