• Thomas Caillet
  • NEWBIE
  • 55 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 48
    Replies
Hi all,

This is my scenario : 
I use all standard objects to do my opportunity (opportunity, opportunity line items, quotes, products ...)
When I create an opportunity, I select my product by click on "Add product" button. When I choose my pricebook I click on "next" and select my products" and after "Select". I arrive in the "Add products" page to enter my sales price.
Here is my problem : I just want to know if it's possible to create a visualforce page or another things to replace this page and display some fields in term of the product type ?

Really thanks in advance
Cheers
Thomas
Hello everybody,

I try in test class to add an opportunity line item WITH a product who have a product code.
But when I add : system.debug (OLI.PricebookEntry.Product2.ProductCode), I have a null value. Could you tell me why ?

Here is my test class : 
@isTest(SeeAllData=true)
private class TestCreateBillingCaseonCase {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity2() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='001w000001eDff7',
                                       Starting_date__c=date.Today(),
                                       CurrencyIsoCode='EUR',
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='003w000001sI2o5',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;
        
        Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
        insert pb22;
        
        Product2 pro = new Product2(Name='Google', isActive=true, ProductCode = '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2');
        insert pro;

        Product2 pro2 = new Product2(Name='training', isActive=true, Billing_Type__c='Once');
        insert pro2;
        
        Product2 pro3 = new Product2(Name='test', isActive=true, Billing_Type__c='Monthly');
        insert pro3;
        
        PricebookEntry pbe =new PricebookEntry(unitprice=0.01,Product2Id=pro.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe;

        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        PricebookEntry pbe3 =new PricebookEntry(unitprice=0.01,Product2Id=pro3.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe3;
        
        Existing_feature__c exf = new Existing_feature__c (Name='TEST', FeatureId__c='123456', Product__c= pro2.Id);
        insert exf;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id,ServiceDate=System.TODAY());     
        insert OLI;
        
        OpportunityLineItem OLI2 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe.Id,ServiceDate=System.TODAY());     
        insert OLI2;
        
        OpportunityLineItem OLI3 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe3.Id,ServiceDate=System.TODAY());     
        insert OLI3;

        Case ca = new Case(Subject='New hotel : Availpro Registration',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 

        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
Thanks
Thomas
Hello,

I have a probleme with a test class in sandbox.
Salesforce says that my field "Number_Of_Revenue_Installments__c" (line 153 and 193) is NULL but I tried to see this field with System.Debug() and it's not NULL.

Here is my trigger
trigger CreateBillingCaseAfterRegistration on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
                                List<Quote> Quo=new List<Quote>();
                                List<Case> CaseOpp =new List<Case>();
                                
                                case c1=new case(
                                        Subject='New hotel : Kick off',
                                        OwnerId='00Gw0000002lubt', 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Subject__c = 'New Hotel',
                                        Type = 'Action Request', 
                                        Training_type__c = 'Kick-Off Call',
                                        Kick_off_date__c=trigger.new[0].Kick_off_date__c, 
                                        Origin ='Automatic case',                                     
                                        AccountId = trigger.new[0].AccountId,
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                     
                              insert CaseOpp;
                              
                               case c2=new case(
                                        Subject='New hotel : Add / Modify Channel : Start Date Invoicing',
                                        OwnerId='00G20000001HboH',
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_first_invoicing_date__c = trigger.new[0].Opportunity_Start_Date__c.addDays(61),
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        insert c2;
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND (PricebookEntry.Product2.ProductCode =: '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2' OR PricebookEntry.Product2.ProductCode =: '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2')])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c3=new case(
                                        Subject='Deposit for'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Deposit Google Ads',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Description = 'Please see this doc to view the process : https://backoffice.availpro.com/search/Help/Index/HLP-258637'+'\n'+'Fee schedule :'+'\n'+'a. – of 25 rooms = 250 '+ trigger.new[0].CurrencyIsoCode +'\n'+'b. 26 to 50 rooms = 400 '+ trigger.new[0].CurrencyIsoCode +'\n'+'c. > of 50 rooms = 600 '+ trigger.new[0].CurrencyIsoCode,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;    
                                        
                                      }  
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND PricebookEntry.Product2.Billing_Type__c <> 'Monthly'])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = OLI.TotalPrice,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3; 
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Date__c = datetime.now(),
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                     
                                     }
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND PricebookEntry.Product2.Billing_Type__c =: 'Monthly' AND (PricebookEntry.Product2.ProductCode != '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2' AND PricebookEntry.Product2.ProductCode != '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2')])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c4=new case(
                                        Subject=ProductName + ' must be activated on '+ OLI.ServiceDate.format(),
                                        Description='This product has been subscribed by the customer and must be activated on the date mentioned in subject. Please update date if necessary',
                                        OwnerId = '00Gw0000002m46x', 
                                        AccountId = trigger.new[0].AccountId,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Subject__c = 'Other',
                                        Type = 'Action Request',
                                        Origin ='Internal action',
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c4;
                                }     

                                insert Feat;

                }
                
if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel FastBooking : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Case> CaseOpp =new List<Case>();
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        if(OLI.Billing_Type__c=='Monthly' && OLI.PricebookEntry.Product2.ProductCode != 'FAST_PRODUCT'){
                                        case c1=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = ((OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c)*0.6,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                        }
                                        
                                        if(OLI.Billing_Type__c!='Monthly' && OLI.PricebookEntry.Product2.ProductCode != 'FAST_PRODUCT'){
                                        case c2=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)*0.6,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c2);
                                        }
                                        
                                        if(OLI.Billing_Type__c=='Monthly' && OLI.PricebookEntry.Product2.ProductCode == 'FAST_PRODUCT'){
                                        case c3=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = ((OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c)*0.4,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c3);
                                        }
                                        
                                        if(OLI.Billing_Type__c!='Monthly' && OLI.PricebookEntry.Product2.ProductCode == 'FAST_PRODUCT'){
                                        case c2=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)*0.4,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c2);
                                        }
                            }
                            
                            insert CaseOpp;

                }
}
And my test class 
@isTest(SeeAllData=true)
private class TestCreateBillingCaseonCase3 {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity3() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0019E00000TDyso',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='0039E00000Mtqhs',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;
        
        Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
        insert pb22;

        Product2 pro = new Product2(Name='training', isActive=true, Billing_Type__c='Once',CurrencyIsoCode='EUR');
        insert pro;
        
        Product2 pro2 = new Product2(Name='training', ProductCode='FAST_PRODUCT', isActive=true, Billing_Type__c='Once',CurrencyIsoCode='EUR');
        insert pro2;
        
        Product2 pro3 = new Product2(Name='training', isActive=true, Billing_Type__c='Monthly',CurrencyIsoCode='EUR');
        insert pro3;
        
        Product2 pro4 = new Product2(Name='training', ProductCode='FAST_PRODUCT', isActive=true, Billing_Type__c='Monthly',CurrencyIsoCode='EUR');
        insert pro4;

        PricebookEntry pbe =new PricebookEntry(unitprice=0.01,Product2Id=pro.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe;
        
        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        PricebookEntry pbe3 =new PricebookEntry(unitprice=0.01,Product2Id=pro3.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe3;
        
        PricebookEntry pbe4 =new PricebookEntry(unitprice=0.01,Product2Id=pro4.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe4;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI;
        
        OpportunityLineItem OLI2 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI2;
        
        OpportunityLineItem OLI3 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe3.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI3;
        
        OpportunityLineItem OLI4 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe4.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI4;

        Case ca = new Case(Subject='New hotel FastBooking : Availpro Registration',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
Thanks for your help
Thomas
Hi everybody,

I have to create a test code trigger to coverage my case trigger. But When I test it, I have this error : caused by: System.NullPointerException: Argument cannot be null.
In fact I have to fill the Number Of Revenue Installments and I don't know to do that when I create the product in my test.

Here is my test trigger : 
@isTest
private class TestCreateBillingCaseonCase2 {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0012500000dVUEw',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='0032500000YR74S',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;
        
        Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
        insert pb22;

        Product2 pro2 = new Product2(Name='training', isActive=true, Billing_Type__c='Monthly');
        insert pro2;

        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id);     
        insert OLI;

        Case ca = new Case(Subject='New hotel : Kick off',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
And here the trigger line where is my error :
Amount__c = (OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c,
Thanks very much
Thomas
 
Hello,

I tried to code my test trigger apex class but I have an error with the code coverage.
Here is my trigger :
 
trigger CreateBillingCaseAfterRegistration on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
                                List<Quote> Quo=new List<Quote>();
                                List<Case> CaseOpp =new List<Case>();
                                
                                case c1=new case(
                                        Subject='New hotel : Kick off',
                                        OwnerId='00Gw0000002lubt', 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Subject__c = 'New Hotel',
                                        Type = 'Action Request', 
                                        Training_type__c = 'Kick-Off Call',
                                        Kick_off_date__c=trigger.new[0].Kick_off_date__c, 
                                        Origin ='Automatic case',                                     
                                        AccountId = trigger.new[0].AccountId,
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                     
                              insert CaseOpp;
                              
                               case c2=new case(
                                        Subject='New hotel : Add / Modify Channel : Start Date Invoicing',
                                        OwnerId='00G20000001HboH',
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_first_invoicing_date__c = trigger.new[0].Opportunity_Start_Date__c.addDays(61),
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        insert c2;
                                
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND ((PricebookEntry.Product2.Name LIKE '%Set Up Fee%') OR (PricebookEntry.Product2.Name LIKE '%training%'))])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        if(OLI.Billing_Type__c=='Monthly'){
                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;
                                        }
                                        
                                        else{
                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = OLI.TotalPrice,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;
                                        }
                                
                                        
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Date__c = datetime.now(),
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                     
                                     }

                                insert Feat;

                }
}
And here my test class:
 
@isTest
private class TestCreateBillingCaseonCase {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity2() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0012500000e1oU4',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;
        
        OpportunityLineItem OLI = new OpportunityLineItem (UnitPrice=300,opportunityId = opp.Id,Quantity=1,PricebookEntryId='01uw000000rrJ5F');     
        insert OLI;          
        
        Quote QUOT = new Quote (Name='TEST',BillingPostalCode = '75009' ,BillingCountry = 'France' ,ContactId='0032500000ZL882',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;

        Case ca = new Case(Subject='New hotel : Availpro Registration',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
My test code is okay up to the for loop but I can't enter in for loop.
For information, My pricebookentry contain "Set up fee" in the product name.

Thanks for your help
Thomas
Hello everybody,

I'd like to know if it's possible to create a button on cases that open new window which list all child accounts related to case's account ?
And after to select any accounts, I need to update a field in these accounts.

Thanks for your answers
Thomas
Hello,

I'm trying to create a trigger or a process builder to update an account status field when an opportunity populated on lookup field in account changed.
In fact I have a lookup field to opportunity on my account name's "Main_opportunity__c" and I want to update the account status when this "Main opportunity" is closed won.

Thanks to your answer
Thomas
Hello community,

I try to code an apex trigger on sandbox based on lead conversion.

I explain to you : I have a lookup field to opportunities on my account page an I would like to put the opportunity converted on this field.
I try to make a process builder (doesn’t work) and a trigger when a lead is converted based on an other post :

trigger trigMapFields on Lead (before update) {
for(Lead lead:System.Trigger.new)
{
if (lead.IsConverted)
{
system.debug('');
Opportunity opp = [SELECT Id FROM Opportunity WHERE Opportunity.Id=:lead.ConvertedOpportunityId];
Account acc = [SELECT Id,Main_Account_Contact__c FROM Account WHERE Account.Id=:lead.ConvertedAccountId];
acc.Main_Opportunity__c = opp.id;
update acc;
system.debug('' + acc.Main_Account_Contact__c );
}
}}

But I think that lead.IsConverted doesn’t work.
I activated Use Apex Lead Convert which upon the conversion of leads, enforces required field settings, field validation rules, workflow actions in Lead settings.
Could you help me please. 

Thanks
Thomas
Hello,

I tried to create a case when another is closed. On case, I have an Account lookup field and I want to put the account owner on the new case.
Here is my trigger :

trigger CreatePresentationCallCase on Case (before update) {

if((trigger.new[0].Status=='closed') && (trigger.old[0].Status<>'closed') && (trigger.new[0].Opportunity_Case__c != null) && ((trigger.new[0].Subject=='New hotel : Training') || (trigger.new[0].Subject=='New hotel : Training')))
                {

                case c1=new case(
                Subject='New hotel : Presentation Call To Be Done',
                OwnerId = trigger.new[0].Account.OwnerId,
                Alfred_Scope__c = 'Other',
                Subject__c = 'New Hotel',
                Type = 'Action Request',
                Origin ='Automatic case',  
                Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                AccountId = trigger.new[0].AccountId,
                Description = 'Kick off made on ' + trigger.new[0].Kick_off_date__c + ' by ' + trigger.new[0].Kick_off_person__c + '\n' + 'Training made on ' + trigger.new[0].Training_Date__c + ' by ' + trigger.new[0].Training_Person__c
                );

                insert c1;
                
                }
                
}

I can save it but when I try to close my case I've got this error : 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CreatePresentationCallCase caused an unexpected exception, contact your administrator: CreatePresentationCallCase: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: Trigger.CreatePresentationCallCase: line 18, column 1

Could you help me please ?
Thanks
Hello,

I tried to code a test trigger to put my triiger on production. I would like to know what is parenthesis on code coverage (1/23) and how to have 75% of code coverage
Here is my trigger:

trigger CreateBillingCase on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status=='closed') && (trigger.new[0].Subject=='New hotel : Kick off') && (trigger.new[0]. Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
        
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, UnitPrice, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        case c1=new case(
                                        Subject='Incoiving :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Amount__c = OLI.UnitPrice);
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                        insert c1;
                                     }
                                     
                                insert Feat;

                }
}

and my test :

@isTest
private class TestCreateBillingCaseonCase {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
        Test.startTest();        
        Case ca = new Case(Subject='New hotel : Kick off',Status='In progress');
        insert ca;
                
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Closed Won',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=ca.AccountId);
        insert opp;

        // Perform test

        ca.Status='Closed';
        ca.Opportunity_Case__c=opp.Id;
        update ca;
        
         List<Features__c> Feat =new List<Features__c>();
         List<Existing_feature__c> Existing =new List<Existing_feature__c>();
        
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, UnitPrice, ServiceDate from OpportunityLineItem where opportunityId =: ca.Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        case c1=new case(
                                        Subject='Incoiving :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = ca.AccountId, 
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Amount__c = OLI.UnitPrice);
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Account__c = ca.AccountId);
                                        
                                        Feat.add(f1); 
                                     }

                                     }
        Test.stopTest();
    }
    
}

Thanks to you 
Thomas
Hi everybody,

I'm trying to make a trigger that create one case for each opportunity product when opportunity is won.
I know how to create case but I don't know how to browse all opportunity products.
I'm new on apex so I need your help please.

Thanks to you 
Hello,

I need your help about something strange on my sandbox:
I want to write a trigger that create one case for each opportunity line items when my opportunity is "Closed Won".
But I'm a junior developper so I started to write just the case creation when opportunity is "Closed Won". But I've got this error: 
Error: Compile Error: Variable does not exist: StageName ...
I think the trigger don't recognize all fields.
Please tell me what I'm doing wrong. Thanks
Here is my "simple" code :

trigger createNewCase on Opportunity (before insert, before update) {  
  
List<Case> listcase = new List<Case>();    
for(Opportunity o: Trigger.new)     
    {        
     
    if ( o.StageName == 'Closed Won' )                     
        {                    
        listCase.add(                
        new Case(                                          
        Priority = 'Medium',                
        Status = 'New'));        
        }    
    }    
        if(listcase.size()>0)     
            {        
            insert listcase;    
            }
}
Hi all,

This is my scenario : 
I use all standard objects to do my opportunity (opportunity, opportunity line items, quotes, products ...)
When I create an opportunity, I select my product by click on "Add product" button. When I choose my pricebook I click on "next" and select my products" and after "Select". I arrive in the "Add products" page to enter my sales price.
Here is my problem : I just want to know if it's possible to create a visualforce page or another things to replace this page and display some fields in term of the product type ?

Really thanks in advance
Cheers
Thomas
Hello,

I have a probleme with a test class in sandbox.
Salesforce says that my field "Number_Of_Revenue_Installments__c" (line 153 and 193) is NULL but I tried to see this field with System.Debug() and it's not NULL.

Here is my trigger
trigger CreateBillingCaseAfterRegistration on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
                                List<Quote> Quo=new List<Quote>();
                                List<Case> CaseOpp =new List<Case>();
                                
                                case c1=new case(
                                        Subject='New hotel : Kick off',
                                        OwnerId='00Gw0000002lubt', 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Subject__c = 'New Hotel',
                                        Type = 'Action Request', 
                                        Training_type__c = 'Kick-Off Call',
                                        Kick_off_date__c=trigger.new[0].Kick_off_date__c, 
                                        Origin ='Automatic case',                                     
                                        AccountId = trigger.new[0].AccountId,
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                     
                              insert CaseOpp;
                              
                               case c2=new case(
                                        Subject='New hotel : Add / Modify Channel : Start Date Invoicing',
                                        OwnerId='00G20000001HboH',
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_first_invoicing_date__c = trigger.new[0].Opportunity_Start_Date__c.addDays(61),
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        insert c2;
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND (PricebookEntry.Product2.ProductCode =: '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2' OR PricebookEntry.Product2.ProductCode =: '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2')])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c3=new case(
                                        Subject='Deposit for'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Deposit Google Ads',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Description = 'Please see this doc to view the process : https://backoffice.availpro.com/search/Help/Index/HLP-258637'+'\n'+'Fee schedule :'+'\n'+'a. – of 25 rooms = 250 '+ trigger.new[0].CurrencyIsoCode +'\n'+'b. 26 to 50 rooms = 400 '+ trigger.new[0].CurrencyIsoCode +'\n'+'c. > of 50 rooms = 600 '+ trigger.new[0].CurrencyIsoCode,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;    
                                        
                                      }  
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND PricebookEntry.Product2.Billing_Type__c <> 'Monthly'])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = OLI.TotalPrice,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3; 
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Date__c = datetime.now(),
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                     
                                     }
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND PricebookEntry.Product2.Billing_Type__c =: 'Monthly' AND (PricebookEntry.Product2.ProductCode != '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2' AND PricebookEntry.Product2.ProductCode != '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2')])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c4=new case(
                                        Subject=ProductName + ' must be activated on '+ OLI.ServiceDate.format(),
                                        Description='This product has been subscribed by the customer and must be activated on the date mentioned in subject. Please update date if necessary',
                                        OwnerId = '00Gw0000002m46x', 
                                        AccountId = trigger.new[0].AccountId,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Subject__c = 'Other',
                                        Type = 'Action Request',
                                        Origin ='Internal action',
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c4;
                                }     

                                insert Feat;

                }
                
if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel FastBooking : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Case> CaseOpp =new List<Case>();
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        if(OLI.Billing_Type__c=='Monthly' && OLI.PricebookEntry.Product2.ProductCode != 'FAST_PRODUCT'){
                                        case c1=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = ((OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c)*0.6,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                        }
                                        
                                        if(OLI.Billing_Type__c!='Monthly' && OLI.PricebookEntry.Product2.ProductCode != 'FAST_PRODUCT'){
                                        case c2=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)*0.6,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c2);
                                        }
                                        
                                        if(OLI.Billing_Type__c=='Monthly' && OLI.PricebookEntry.Product2.ProductCode == 'FAST_PRODUCT'){
                                        case c3=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = ((OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c)*0.4,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c3);
                                        }
                                        
                                        if(OLI.Billing_Type__c!='Monthly' && OLI.PricebookEntry.Product2.ProductCode == 'FAST_PRODUCT'){
                                        case c2=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)*0.4,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c2);
                                        }
                            }
                            
                            insert CaseOpp;

                }
}
And my test class 
@isTest(SeeAllData=true)
private class TestCreateBillingCaseonCase3 {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity3() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0019E00000TDyso',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='0039E00000Mtqhs',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;
        
        Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
        insert pb22;

        Product2 pro = new Product2(Name='training', isActive=true, Billing_Type__c='Once',CurrencyIsoCode='EUR');
        insert pro;
        
        Product2 pro2 = new Product2(Name='training', ProductCode='FAST_PRODUCT', isActive=true, Billing_Type__c='Once',CurrencyIsoCode='EUR');
        insert pro2;
        
        Product2 pro3 = new Product2(Name='training', isActive=true, Billing_Type__c='Monthly',CurrencyIsoCode='EUR');
        insert pro3;
        
        Product2 pro4 = new Product2(Name='training', ProductCode='FAST_PRODUCT', isActive=true, Billing_Type__c='Monthly',CurrencyIsoCode='EUR');
        insert pro4;

        PricebookEntry pbe =new PricebookEntry(unitprice=0.01,Product2Id=pro.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe;
        
        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        PricebookEntry pbe3 =new PricebookEntry(unitprice=0.01,Product2Id=pro3.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe3;
        
        PricebookEntry pbe4 =new PricebookEntry(unitprice=0.01,Product2Id=pro4.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe4;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI;
        
        OpportunityLineItem OLI2 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI2;
        
        OpportunityLineItem OLI3 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe3.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI3;
        
        OpportunityLineItem OLI4 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe4.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI4;

        Case ca = new Case(Subject='New hotel FastBooking : Availpro Registration',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
Thanks for your help
Thomas
Hi everybody,

I have to create a test code trigger to coverage my case trigger. But When I test it, I have this error : caused by: System.NullPointerException: Argument cannot be null.
In fact I have to fill the Number Of Revenue Installments and I don't know to do that when I create the product in my test.

Here is my test trigger : 
@isTest
private class TestCreateBillingCaseonCase2 {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0012500000dVUEw',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='0032500000YR74S',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;
        
        Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
        insert pb22;

        Product2 pro2 = new Product2(Name='training', isActive=true, Billing_Type__c='Monthly');
        insert pro2;

        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id);     
        insert OLI;

        Case ca = new Case(Subject='New hotel : Kick off',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
And here the trigger line where is my error :
Amount__c = (OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c,
Thanks very much
Thomas
 
Hello,

I tried to code my test trigger apex class but I have an error with the code coverage.
Here is my trigger :
 
trigger CreateBillingCaseAfterRegistration on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
                                List<Quote> Quo=new List<Quote>();
                                List<Case> CaseOpp =new List<Case>();
                                
                                case c1=new case(
                                        Subject='New hotel : Kick off',
                                        OwnerId='00Gw0000002lubt', 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Subject__c = 'New Hotel',
                                        Type = 'Action Request', 
                                        Training_type__c = 'Kick-Off Call',
                                        Kick_off_date__c=trigger.new[0].Kick_off_date__c, 
                                        Origin ='Automatic case',                                     
                                        AccountId = trigger.new[0].AccountId,
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                     
                              insert CaseOpp;
                              
                               case c2=new case(
                                        Subject='New hotel : Add / Modify Channel : Start Date Invoicing',
                                        OwnerId='00G20000001HboH',
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_first_invoicing_date__c = trigger.new[0].Opportunity_Start_Date__c.addDays(61),
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        insert c2;
                                
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND ((PricebookEntry.Product2.Name LIKE '%Set Up Fee%') OR (PricebookEntry.Product2.Name LIKE '%training%'))])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        if(OLI.Billing_Type__c=='Monthly'){
                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;
                                        }
                                        
                                        else{
                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = OLI.TotalPrice,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;
                                        }
                                
                                        
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Date__c = datetime.now(),
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                     
                                     }

                                insert Feat;

                }
}
And here my test class:
 
@isTest
private class TestCreateBillingCaseonCase {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity2() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0012500000e1oU4',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;
        
        OpportunityLineItem OLI = new OpportunityLineItem (UnitPrice=300,opportunityId = opp.Id,Quantity=1,PricebookEntryId='01uw000000rrJ5F');     
        insert OLI;          
        
        Quote QUOT = new Quote (Name='TEST',BillingPostalCode = '75009' ,BillingCountry = 'France' ,ContactId='0032500000ZL882',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;

        Case ca = new Case(Subject='New hotel : Availpro Registration',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
My test code is okay up to the for loop but I can't enter in for loop.
For information, My pricebookentry contain "Set up fee" in the product name.

Thanks for your help
Thomas
Hello everybody,

I'd like to know if it's possible to create a button on cases that open new window which list all child accounts related to case's account ?
And after to select any accounts, I need to update a field in these accounts.

Thanks for your answers
Thomas