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
Girbson Bijou 8Girbson Bijou 8 

Test invocable Method

I want to test a class with an invocable method. Actually i get 51% Coverage and  at the line 73 in the Test class the error is:

"System.DmlException: Update failed. First exception on row 0 with id a03f0000006BbNwAAK; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Post Affiliate Inventory” process failed. Give your Salesforce admin these details. An Apex error occurred: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: : [RecordTypeId]
: []


I want to remind that this class is to copy all child records from an object to an other object when a criteria is meet in the parent record. 

Apex Class: 
public class PostToAffiliateInventory {
    
    @InvocableMethod(label ='post to affiliate inventory')
    
    public static void PosttoInv(list<Id> deliveryId){
        
        list <Container__c> con = new List <Container__c>();
        list <Articles_Containers__c> contenairItems = new list <Articles_Containers__c>();
        list <Item_Distributed__c> itemList = new  list<Item_Distributed__c>();
        list <Delivery__c> delList = [SELECT Id, Is_Posted_To_Affiliate_Inventory__c, Affiliate_Center__c,AffiliateCenterId__c, Automatic_Code__c  FROM Delivery__c WHERE Id in :deliveryId];
        
        Id containerRecordTypeCaracole = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('FFP_Caracole').getRecordTypeId();
        Id containerRecordTypePAPCRN = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('CRN_PAP').getRecordTypeId();
        Id containerRecordTypeCanteenPAP = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('Canteen_PAP').getRecordTypeId();
        id crt; 
        
        Id ArticleContainerRecordTypeCaracole = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('FFP_Caracole').getRecordTypeId();
        Id ArticleContainerPAPCRN = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('CRN_PAP').getRecordTypeId();
        Id ArticleContainerCanteenPAP = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('PAP_Canteen').getRecordTypeId();
        id ArticleContainerAdmin = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('Admin').getRecordTypeId();
        id acrt;
        
        Container__c c;
        
        for (Delivery__c d : delList){
            if (d.Is_Posted_To_Affiliate_Inventory__c){
                if(d.Affiliate_Center__c =='FFP Caracole'){
                    crt = containerRecordTypeCaracole;
                }else if(d.Affiliate_Center__c =='CRN PAP'){
                    crt = containerRecordTypePAPCRN;
                }else if(d.Affiliate_Center__c =='PAP Canteen'){
                    crt = containerRecordTypeCanteenPAP;
                }
                c = new  Container__c();
                c.Name = d.Automatic_Code__c;
                c.FFP_Centers__c = d.Affiliate_Center__c ;
                c.RecordTypeId = crt;
                c.Distribution_Center__c = d.AffiliateCenterId__c;
                c.Is_Owner_Shipper__c = 'No';
                c.Provenance__c = 'FFP PAP';
                c.Type__c = 'Food';
                c.Shipment_Status__c = 'A - AWAITING ARRIVAL';
                c.Is_Automatic_Creation__c = TRUE;
                
                insert c;
                
                itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                            Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                            Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                            FROM Item_Distributed__c  where Delivery__c  =: d.id ];             
                
                for (Item_Distributed__c OrderItems: itemList){
                    Articles_Containers__c ac = new Articles_Containers__c();
                    if(c.FFP_Centers__c == 'FFP Caracole'){
                        acrt = ArticleContainerRecordTypeCaracole;
                    }else if(c.FFP_Centers__c == 'CRN PAP'){
                        acrt = ArticleContainerPAPCRN;
                    }else if (c.FFP_Centers__c == 'PAP Canteen'){
                        acrt = ArticleContainerCanteenPAP;
                    }else acrt = ArticleContainerAdmin;
                    
                    ac.FFP_Centers__c = d.Affiliate_Center__c ;
                    ac.RecordTypeId = acrt; 
                    ac.Container__c =c.Id;
                    ac.Product__c = OrderItems.Product__r.Product__r.Id;
                    ac.Number__c= 0;
                    ac.Quantity__c = OrderItems.Quantity__c;
                    ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                    ac.UM__c = OrderItems.Product__r.UM__c;
                    ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                    ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                    ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                    ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                    ac.DeliveryItemsReferences__c = OrderItems.id;
                    
                    contenairItems.add(ac);
                }      
                Insert contenairItems; 

            }
            
        } 
    }
}

Test Class: 
@IsTest
public class PostToAffiliateInventoryTest {
    
     @isTest static  void PostToAffiliateInv (){
        Account afc = new Account(Name ='test Account parent', Representant__c ='hkjlhjd', 
                                 Distribution_Center__c='FFP Caracole', Class__c = 'Clinic', Type = 'CLINICS', 
                                 Departement__c = 'NORD', Address__c ='Test Address');
        
        Account pa = new Account(Name ='Central Office', Representant__c ='hkjlhjd', 
                                 Distribution_Center__c='Central Office', Class__c = 'Clinic', Type = 'CLINICS', 
                                 Departement__c = 'OUEST', Address__c ='Test Address');
        
        insert pa;
   
        Product2 p = new Product2(Name ='TestProduct'); 
        insert p;
        Container_Shipper__c cs =new Container_Shipper__c(Name = 'Test Shipper');
        insert cs;
        
        //Id containerRecordTypeCaracole =      Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('FFP_Caracole').getRecordTypeId();
        //Id containerRecordTypeCentralOffice = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('Central Office').getRecordTypeId();
        Container__c c = new Container__c( Name = 'CMLU', Provenance__c='FFP FLORIDA', Statut__c='Open', Distribution_Center__c= pa.id, FFP_Centers__c = 'Central Office',  RecordTypeId='0126A000000nMlj',  Shipper__c=cs.Id );
        insert c;
        
       // Id ArticleContainerRecordTypeCaracole = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('FFP_Caracole').getRecordTypeId();
       // Id ArticleContainerRecordTypeCentralOffice = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('HT Central Office').getRecordTypeId();
        Articles_Containers__c ac = new Articles_Containers__c();   
        
        ac.Product__c=p.ID;
        ac.Unit_Weight__c = 45; 
        ac.Unit_Cost__c = 87;
        ac.Container__c= c.ID;
        ac.Number__c = 55;
        ac.UM__c ='UNIT(S)';    
        ac.Local_ID__c = 7888;
        ac.Comments__c = 'UNIT(S)';
        ac.Purpose__c='Consignment';
        ac.Condition__c= 'New';
        ac.FFP_Centers__c = 'Central Office';
        ac.RecordTypeId = '0126A0000019jRK';
        insert ac;
        
        Account a = new Account();
        a.Name ='test Account'; 
        a.Representant__c ='Bijou';
        a.Distribution_Center__c='Central Office';
        a.Class__c = 'NGO'; 
        a.Type = 'CLINICS';
        a.Departement__c = 'NORD';
        a.Address__c ='Test Address';
        a.ParentId = pa.ID;
        a.Affiliate_Center__c = afc.Id;
        insert a ;
       
        
        Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = a.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
       
        del.Is_Posted_To_Affiliate_Inventory__c = false;
        
        insert del;
        
        Item_Distributed__c itemDis = new Item_Distributed__c();
        itemDis.Quantity__c = 10;
        itemDis.Product__c = ac.Id;
        itemDis.Delivery__c =del.Id;
     
        insert itemDis;
        
        del.Is_Posted_To_Affiliate_Inventory__c = true;
        update del;
        
        
        List<Delivery__c> delivery_Obj  =  [SELECT Beneficiaire__c from Delivery__c];
        System.assertEquals(1,delivery_Obj.size());
        List<Item_Distributed__c> item_distributed_Obj  =  [SELECT Product__c,Quantity__c,Delivery__c from Item_Distributed__c];
        System.assertEquals(1,item_distributed_Obj.size());

        PostToAffiliateInventory pav = new PostToAffiliateInventory();
      
    }
    
}


 
Best Answer chosen by Girbson Bijou 8
Karan KeharKaran Kehar
Can you check do you have the correct permission on the profile. This can also be due to incorrect recordtype id in the code. Can you confirm that as well?

All Answers

Karan KeharKaran Kehar
Hi Girbson,

You get this error because the profile of the user who is running the test class doesn't have access to the record type with id 0126A0000019jRK. You can verify the same by going to profile--> object settings -->Articles_Containers__c --> record types
Girbson Bijou 8Girbson Bijou 8
Hi KARANKEHAR,
Thank you for replying.

I have the System Administrator profil, I do not understand how i do not have access to this record type. is there a way to force the process run as the administrator profil?

Best, 

Girbson
Karan KeharKaran Kehar
Can you check do you have the correct permission on the profile. This can also be due to incorrect recordtype id in the code. Can you confirm that as well?
This was selected as the best answer
Girbson Bijou 8Girbson Bijou 8
Yes, I have full access in everything and i use to create record with all record Type. 
AnudeepAnudeep (Salesforce Developers) 
You are running into an issue that is similar to what is described here
Girbson Bijou 8Girbson Bijou 8
Thanks all for replying. My problem was in the line 5 in the test Class.  I instanciate the sObject Account but i did not insert insert it. 
Account afc