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
Raja JammulaRaja Jammula 

Error can anybody help: OP_WITH_INVALID_USER_TYPE_EXCEPTION

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

@isTest
    public static void testhandleCorporateSalesBeforeInsert() {
        
        Id rTDCXX = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('CDC').getRecordTypeId();
        Id rTPCTX = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('CSP').getRecordTypeId();
           Test.startTest();
        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');
        oppownerList.add(Products1);
        Custom_Setopp__c Products2 = new Custom_Setopp__c(Name = 'Paty', Record_Type__c = 'CSP', 
                                                                PTC__c = 'PAY');
        oppownerList.add(Products2);
        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);
        empList.add(Employee1);
        insert empList;
        system.debug('empList::'+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);
        prodList.add(product1);
        Prod__c product2 = new Prod__c(Name='PAY - Test Account',PC__c = 'PAY',Account__c = account.Id,
                                                         stat__c = 'Prospect', Owner__c = Employee1.Id);
        prodList.add(product2); 
        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);
        oppList.add(opp1);
        Opportunity opp2 = new Opportunity(AccountId = account.id, Name = 'Test DC', Type = 'Conversion',
                                           Consultant_Advisor__c = 'Yes', StageName = 'LGC - 0%', 
                                           CloseDate = System.today(), RecordTypeId = rTPCTX);
        oppList.add(opp2);
        insert oppList;
        system.debug('oppList[0].OwnerId ::'+oppList[0].OwnerId);
        //System.assertEquals(Userinfo.getUserId(), oppList[0].OwnerId);
        
        Test.stopTest();
    }
  }

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() ){
            prt.put(csRec.Record_Type__c,csRec.Product_Type__c);
            rtwp.put(csRec.Product_Type__c,csRec.Record_Type__c);
        }
        
        for (Opportunity opportunity : opportunities) {
            Schema.RecordTypeInfo rt = rtMap.get(opportunity.RecordTypeId);
            if (rt != null && prt.containsKey(rt.getName()) ){ 
                accountIds.add(opportunity.AccountId);
                csopp.add(opportunity);
            }
        }
        
        // If no CS opportunities found then discontinue processing
        if (csopp.isEmpty())
            return;
        
        Map<Id,Map<String,Id>> acpto = new Map<Id,Map<String,Id>>();
        system.debug('accountIds::'+accountIds+'\nprt.values()::'+prt.values());
        List<Prod__c> pl= [SELECT Id, PC__c,Owner__c, Account__c,Owner__r.User__c 
                                      FROM Prod__c ];
         system.debug('pl::'+pl);
        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>();
             
             if(acpto.containsKey(prod.Account__c)){
                Pom = acpto.get(prod.Account__c);
             }
             Pom.put(rtwp.get(prod.PC__c),prod.Owner__r.User__c);
             acpto.put(prod.Account__c,Pom);
             system.debug('owner::'+acpto.values()+'\nPom.values()::'+Pom.values());
           }
         
        // 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