+ Start a Discussion
Raja JammulaRaja Jammula 


Hi all, i am getting this error in my Test class:

System.DmlException: Insert failed. First exception on row 0; first error: OP_WITH_INVALID_USER_TYPE_EXCEPTION, Operation not valid for this user type: []

Don't know how to solve this? Any help would be rally helpful

Scenario: updating the opp owner based on the product owner from account

    public static void testhandleCorporateSalesBeforeInsert() {
        Id rTDCXX = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('CDC').getRecordTypeId();
        Id rTPCTX = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('CSP').getRecordTypeId();
        List<Custom_Setopp__c> oppownerList = new List<Custom_Setopp__c>();
        Custom_Setopp__c Products1 = new Custom_Setopp__c(Name = 'DC', Record_Type__c = 'CDC', 
                                                                PTC__c = 'DC');
        Custom_Setopp__c Products2 = new Custom_Setopp__c(Name = 'Paty', Record_Type__c = 'CSP', 
                                                                PTC__c = 'PAY');
        insert oppownerList;
        Profile p = [select id from profile where name='Standard User'];
        RecordType rt = [select id from recordtype where sobjecttype = 'Opportunity' and name = 'CDC'];
        User u = new User(alias = 'standt', email='testuser@test.com',
                          emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                          localesidkey='en_US', profileid = p.Id, employeenumber = 'A000000',
                          timezonesidkey='America/Los_Angeles', username='testuser737712@test.com', IsActive = true);
        insert u;          
        System.runAs (u) {
            UserRole r = new UserRole(Name='ACCROL');      
            insert r;
        Account account = new Account(Name='Test Account',Type='PS',CSG_Account_stat__c='PROSPECT',OwnerId=u.Id,BillingStreet='123 Main',BillingCity='Co',BillingState = 'CA',BillingPostalCode='67890',Phone='123-456-7890');
        insert account;
        system.debug('in test account id ::'+account.Id);
        List<Employee__c> empList = new List<Employee__c>();
        Employee__c Employee1 = new Employee__c(Name='Testing1', Employee_Number__c='A000000', Email__c='testuser@test.com', User__c = u.Id);
        insert empList;
        List<Prod__c> prodList = new List<Prod__c>();
        Prod__c product1 = new Prod__c(Name='DC - Test Account',PC__c = 'DC',Account__c = account.Id, 
                                                         stat__c = 'Prospect', Owner__c = Employee1.Id, AE__c = Employee1.Id);
        Prod__c product2 = new Prod__c(Name='PAY - Test Account',PC__c = 'PAY',Account__c = account.Id,
                                                         stat__c = 'Prospect', Owner__c = Employee1.Id);
        insert prodList;
        system.debug('in test prodList id ::'+prodList);
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opp1 = new Opportunity(AccountId = account.id, Name = 'Test DC', Type = 'Conversion', 
                                          Consultant_Advisor__c = 'Yes', StageName = 'LGC - 0%',
                                           CloseDate = System.today(), RecordTypeId = rTDCX);
        Opportunity opp2 = new Opportunity(AccountId = account.id, Name = 'Test DC', Type = 'Conversion',
                                           Consultant_Advisor__c = 'Yes', StageName = 'LGC - 0%', 
                                           CloseDate = System.today(), RecordTypeId = rTPCTX);
        insert oppList;
        system.debug('oppList[0].OwnerId ::'+oppList[0].OwnerId);
        //System.assertEquals(Userinfo.getUserId(), oppList[0].OwnerId);

Actual Class:

public void HSCBI(Opportunity[] opportunities) {
        // Find all CDC opportunities
        Set<Id> accountIds = new Set<Id>();
        List<Opportunity> csopp = new List<Opportunity>();
        Map<String,String> prt = new Map<String,String>();
        Map<String,String> rtwp = new Map<String,String>();
        for(Custom_Setopp__c csRec : Custom_Setopp__c.getall().values() ){
        for (Opportunity opportunity : opportunities) {
            Schema.RecordTypeInfo rt = rtMap.get(opportunity.RecordTypeId);
            if (rt != null && prt.containsKey(rt.getName()) ){ 
        // If no CS opportunities found then discontinue processing
        if (csopp.isEmpty())
        Map<Id,Map<String,Id>> acpto = new Map<Id,Map<String,Id>>();
        List<Prod__c> pl= [SELECT Id, PC__c,Owner__c, Account__c,Owner__r.User__c 
                                      FROM Prod__c ];
        for (Prod__c prod : [SELECT Id, PC__c,Owner__c, Account__c,Owner__r.User__c 
                                      FROM Prod__c 
                                      WHERE Account__c in : accountIds AND PC__c in :prt.values() AND Owner__r.User__c != null]){
             Map<String,Id> Pom = new Map<String,Id>();
                Pom = acpto.get(prod.Account__c);
        // For new CS* opportunities, auto transfer owner to product owner 
        for (Opportunity opportunity : csopp) { 
            if (opportunity.StageName != null) {
                Schema.RecordTypeInfo rt = rtMap.get(opportunity.RecordTypeId);
                if(acpto.containsKey(opportunity.AccountId) && 
                    acpto.get(opportunity.AccountId).containsKey(rt.getName()) ){
                    opportunity.OwnerId = acpto.get(opportunity.AccountId).get(rt.getName()) ;
Timothy Gentet O'BrienTimothy Gentet O'Brien
A standard user would not have permissions to create a Role, it looks as if you are trying to do that:
UserRole r = new UserRole(Name='ACCROL');      
            insert r;
Raja JammulaRaja Jammula
I tried with System administrator but i get this error when i am trying to insert oppList, the debug logs shows that ownerid for the opp is setting to null. can you help me what is happening here