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
Peter BölkePeter Bölke 

Deployment problem with trigger/process

Hello,

i have some trouble deploying a trigger and an approval process. The Process exists in an older version in the production environment.
The changeset includes the Trigger, Testclass and new version of approval process.
When i run the test-class in production, i am getting this error:
 
System.DmlException: Process failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2: [] 
Stack Trace: Class.TrgKnowledgeSendNotificationTest.test01: line 80, column 1

It runs in our sandbox without any errors.

Do i need to deploy the new version of approval process seperatly in advace of deploying the trigger?

thanks
PEter
Vishal_GuptaVishal_Gupta
Hi Peter,

It look like that issue is in your trigger test class, if you can share your trigger and test class code then I can look into it and help you out. Thanks.
Peter BölkePeter Bölke
Hello Vishal,

thanks for your replayere is my testclass
 
@isTest(SeeAllData = True)
public class TrgKnowledgeSendNotificationTest {
    
    public testMethod static void testAirKnowledge() {
        
        Id prId = [SELECT Id FROM Profile WHERE Name = 'Company Administrator'].Id;
        Id urId = [SELECT Id From UserRole WHERE Name = 'ALL' LIMIT 1].Id;
        UserRole ur = new UserRole(Name = 'Finance Approver');
        insert ur;
        List<User> rec = TrgKnowledgeSendNotificationTest.createACUser();
        User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'Company Administrator'].Id,
            LastName = 'last',
            Email = 'test@test.de',
            Username = 'TEST1_me@tester.de' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            UserRoleId = [SELECT Id From UserRole WHERE Name = 'ALL' LIMIT 1].Id
            
        );
        insert u;
        User u2 = [SELECT id From User WHERE ProfileId = :prId AND isactive = true And UserRoleId =:urId LIMIT 1];
        
        Group g = [SELECT Id From Group WHERE Name = 'APartners' LIMIT 1];
        GroupMember gm = new GroupMember(UserOrGroupId = u.id, GroupId = g.id);
        insert gm;
        Group g1 = [SELECT Id From Group WHERE Name = 'Business' LIMIT 1];
        GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
        insert gm1;
        
        System.runAs(u2){
            System.debug(
                [Select RecordTypeId From Knowledge__kav WHERE RecordType.DeveloperName= 'Air' Limit 1].RecordTypeId);
            Knowledge__kav art = new Knowledge__kav();
            art.Title = 'Test';
            art.Summary = 'Test Summary';
            art.Article_Body__c = 'rhtgrthgtr';
            art.Publication_Year__c = '2018';
            art.Publication_Number__c = '6669';
            art.Publication_Type__c = 'Circular Letter';
            art.RecordTypeId =
                Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('Air').getRecordTypeId();
            art.IsVisibleInCsp = true;
            art.IsVisibleInPkb = true;
            art.IsVisibleInPrm = true;
            art.UrlName = 'ttttttttttttttt';
            art.Article_Body__c = 'wergwgwegq rfewrg aerg ear ge r';
            insert art;
            
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for Approval. Please Review');
            req.setNextApproverIds(new Id[] {u.id});
            req.setObjectId(art.Id);
            req.setSkipEntryCriteria(true);
            req.setProcessDefinitionNameOrId('Approval_of_Knowledge_Articles_new');
            
            //Knowledge__kav art4 = [Select Id, PublishStatus, KnowledgeArticleId, Language, ValidationStatus, RecordType.DeveloperName  FROM Knowledge__kav WHERE Id = :art3.Id];
            Approval.ProcessResult result = Approval.process(req);
            System.debug(result);
            
            System.assert(result.isSuccess());
            List<Id> newWorkItemIds = result.getNewWorkitemIds();
            
            // Instantiate the new ProcessWorkitemRequest object and populate it
            Approval.ProcessWorkitemRequest req2 =
                new Approval.ProcessWorkitemRequest();
            req2.setComments('Approving request.');
            req2.setAction('Approve');
            req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});
            
            // Use the ID from the newly created item to specify the item to be worked
            req2.setWorkitemId(newWorkItemIds.get(0));
            
            // Submit the request for approval
            Approval.ProcessResult result2 = Approval.process(req2);
            
            // Verify the results
            System.assert(result2.isSuccess(), 'Result Status:' + result2.isSuccess());
            
            System.assertEquals(
                'Approved', result2.getInstanceStatus(),
                'Instance Status' + result2.getInstanceStatus());
            
            Knowledge__kav art4 =
                [Select Id, PublishStatus, KnowledgeArticleId, Language, ValidationStatus, RecordType.DeveloperName,Approved__c FROM Knowledge__kav WHERE Id = :art.Id];
            System.debug('ART 4 :: ' + art4);
            //KbManagement.PublishingService.publishArticle(art4.KnowledgeArticleId, true);
        }
    }
    
    public testMethod static void testGroundKnowledge() {
        
        Id prId = [SELECT Id FROM Profile WHERE Name = 'Company Administrator'].Id;
        Id urId = [SELECT Id From UserRole WHERE Name = 'ALL' LIMIT 1].Id;
        UserRole ur = new UserRole(Name = 'Finance Approver');
        insert ur;
        List<User> rec = TrgKnowledgeSendNotificationTest.createGroundUser();
        User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'Company Administrator'].Id,
            LastName = 'last',
            Email = 'esrgsergse@test.de',
            Username = 'TEST2_peter.boelke@abilex.de' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            UserRoleId = [SELECT Id From UserRole WHERE Name = 'ALL' LIMIT 1].Id
            
        );
        insert u;
        
        
        User u2 = [SELECT id, isactive From User WHERE ProfileId = :prId AND isactive = true AND UserRoleId =:urId LIMIT 1];
        System.debug('USER 2 ' + u2.IsActive + ' ' + u2);
        Group g = [SELECT Id From Group WHERE Name = 'KPartners' LIMIT 1];
        //KA Publication_Partner
        //Group g = [SELECT Id From Group WHERE Name = 'KPub_Partner'LIMIT 1];
        GroupMember gm = new GroupMember(UserOrGroupId = u.id, GroupId = g.id);
        insert gm;
        Group g1 = new Group();
        try{
            g1 = [SELECT Id From Group WHERE Name = 'KBusiness' LIMIT 1];
            GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
            insert gm1;
        }
        catch(Exception ex){
            System.debug('GROUP "KBusiness" NOT FOUND');
                }
        
        try{
            g1 = [SELECT Id From Group WHERE Name = 'KPub_Internal' LIMIT 1];
            GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
            insert gm1;
        }
        catch(Exception ex){
            System.debug('GROUP "KPub_Internal" NOT FOUND');
                }
        
        
        System.runAs(u2){
            Knowledge__kav art = new Knowledge__kav();
            art.Title = 'Test';
            art.Summary = 'Test Summary';
            art.Article_Body__c = 'rhtgrthgtr';
            art.Publication_Year__c = '2018';
            art.Publication_Number__c = '5558';
            art.Publication_Type__c = 'Circular Letter';
            art.RecordTypeId =
                Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('Ground').getRecordTypeId();
            art.IsVisibleInCsp = true;
            art.IsVisibleInPkb = true;
            art.IsVisibleInPrm = true;
            art.UrlName = 'ttttttttttttttt';
            art.Article_Body__c = 'wergwgwegq rfewrg aerg ear ge r';
            insert art;
            
            
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for Approval. Please Review');
            req.setNextApproverIds(new Id[] {u.id});
            req.setObjectId(art.Id);
            req.setSkipEntryCriteria(true);
            req.setProcessDefinitionNameOrId('Approval_of_Knowledge_Articles_new');
            
            //Knowledge__kav art4 = [Select Id, PublishStatus, KnowledgeArticleId, Language, ValidationStatus, RecordType.DeveloperName  FROM Knowledge__kav WHERE Id = :art3.Id];
            Approval.ProcessResult result = Approval.process(req);
            System.debug(result);
            
            System.assert(result.isSuccess());
            List<Id> newWorkItemIds = result.getNewWorkitemIds();
            
            // Instantiate the new ProcessWorkitemRequest object and populate it
            Approval.ProcessWorkitemRequest req2 =
                new Approval.ProcessWorkitemRequest();
            req2.setComments('Approving request.');
            req2.setAction('Approve');
            req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});
            
            // Use the ID from the newly created item to specify the item to be worked
            req2.setWorkitemId(newWorkItemIds.get(0));
            
            // Submit the request for approval
            Approval.ProcessResult result2 = Approval.process(req2);
            
            // Verify the results
            System.assert(result2.isSuccess(), 'Result Status:' + result2.isSuccess());
            
            System.assertEquals(
                'Approved', result2.getInstanceStatus(),
                'Instance Status' + result2.getInstanceStatus());
            
            Knowledge__kav art4 =
                [Select Id, PublishStatus, KnowledgeArticleId, Language, ValidationStatus, RecordType.DeveloperName, Approved__c FROM Knowledge__kav WHERE Id = :art.Id];
            System.debug('ART 4 :: ' + art4);
            //KbManagement.PublishingService.publishArticle(art4.KnowledgeArticleId, true);
        }
    }
    
    private static List<User> createACUser(){
        List<User> rec = new List<User>();
        Id PrId = [SELECT Id FROM Profile WHERE Name = 'Company Administrator'].Id;
        Id roId =   [SELECT Id From UserRole WHERE Name = 'ALL' LIMIT 1].Id;
        for(Integer i = 0; i< 5; i++){
            User u = new User(
                ProfileId = PrId,
                LastName = 'last',
                Email = 'test@test.de',
                Username = String.valueOf(i)+'TEST1_ptest@atesbal.de' + System.currentTimeMillis(),
                CompanyName = 'TEST',
                Title = 'title',
                Alias = 'alias',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                UserRoleId = roid
                
            );
            
            rec.add(u);
        }
        insert rec;
        List<GroupMember> lgm1 = new List<GroupMember>();
        List<GroupMember> lgm2 = new List<GroupMember>();
        
        Group g = [SELECT Id From Group WHERE Name = 'APartners' LIMIT 1]; 
       
        
        Group g1 = [SELECT Id From Group WHERE Name = 'ABusiness' LIMIT 1];
        for(User u :rec){
            GroupMember gm = new GroupMember(UserOrGroupId = u.id, GroupId = g.id);
            GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
            lgm1.add(gm);
            lgm2.add(gm1);
        }
        insert lgm1;
        insert lgm2;
        
        //GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
        //insert gm1;
        return rec;
        
    }
    private static List<User> createGroundUser(){
        List<User> rec = new List<User>();
        Id PrId = [SELECT Id FROM Profile WHERE Name = 'Company Administrator'].Id;
        Id roId =   [SELECT Id From UserRole WHERE Name = 'ALL' LIMIT 1].Id;
        for(Integer i = 0; i< 5; i++){
            User u = new User(
                ProfileId = PrId,
                LastName = 'last',
                Email = 'test@test.de',
                Username = String.valueOf(i)+'TEST1_blab@de.de' + System.currentTimeMillis(),
                CompanyName = 'TEST',
                Title = 'title',
                Alias = 'alias',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                UserRoleId = roid
                
            );
            
            rec.add(u);
        }
        insert rec;
        List<GroupMember> lgm1 = new List<GroupMember>();
        List<GroupMember> lgm2 = new List<GroupMember>();
        Group g = [SELECT Id From Group WHERE Name = 'KPartners' LIMIT 1];
        Group g1 = [SELECT Id From Group WHERE Name = 'KBusiness' LIMIT 1];
        for(User u :rec){
            GroupMember gm = new GroupMember(UserOrGroupId = u.id, GroupId = g.id);
            GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
            lgm1.add(gm);
            lgm2.add(gm1);
        }
        insert lgm1;
        insert lgm2;
        
        //GroupMember gm1 = new GroupMember(UserOrGroupId = u.id, GroupId = g1.id);
        //insert gm1;
        return rec;
        
    }  
}

thanks for any advice :)
Peter