+ Start a Discussion
Raja JammulaRaja Jammula 

Please 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);
            
        FESCoProductTriggerHandler.setFirstRun();    
        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;
        List<Prod__c> pl= [SELECT Id, PC__c,Owner__c, Account__c,Owner__r.User__c 
                                      FROM Prod__c WHERE PC__c = 'DC' AND Account__c = :account.Id];
         system.debug('pl2::'+pl);
        //System.assertEquals(Userinfo.getUserId(), oppList[0].OwnerId);
        
        Test.stopTest();
    }
  }

Actual Class:

private 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()) ;
                }
            }
        } 
    }
v varaprasadv varaprasad
Hi Raja,


Please remove below code and check once :
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;

Otherwise, use system admin profile instead of the standard user.

Please add debug logs in test class and class check once on which line error message is showing.

More Info : 
https://salesforce.stackexchange.com/questions/24390/mysterious-op-with-invalid-user-type-exception-operation-not-valid-for-this-use

 Thanks
Varaprasad
Raja JammulaRaja Jammula
Hi @v varaprasad 

it's throwing error on : insert oppList;
Raja JammulaRaja Jammula
i tried with system admin it's throwing me an error
Raja JammulaRaja Jammula
the opp owner is setting to null
v varaprasadv varaprasad
Hi ,

private void HSCBI(Opportunity[] opportunities) change here access modifie private to public and check once.
Raja JammulaRaja Jammula
Hi,

Tried with Public but still getting the error
Raja JammulaRaja Jammula
Hi any Ideas?
Raja JammulaRaja Jammula
USER_DEBUG oppList[0].OwnerId ::null