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
aks001aks001 

test class scenerio

hi there,

friends I have a vf page that generating pdf and that page has a controller that is getting data from a master object and also from it's multiple child records using fieldsets the both objects having it's own fieldset and now when I'm doing

ApexPages.StandardController controller;
        productionEditControl proController = new productionEditControl(controller);
       
        //inserting test data... contains one production ticket and production line item under it...
        production__c p = new production__c();
        p.name = 'new test prodution 0011112';
        insert p;
       
        List<ProductionLineItem__c> plis = new List<ProductionLineItem__c>();
        for(Integer i = 0; i<=3; i++){
            productionLineItem__c pli = new productionLineItem__c();
            pli.production__c = p.id;
            plis.add(pli);
        }
        insert plis;
       
        // now passing the id to related page to use that data inserted above...
        PageReference pageRef = Page.EditProductionTicket;
        pageRef.getParameters().put('id', p.id);
        Test.setCurrentPage(pageRef);
       
        // calling instance methods...
        proController.save();
        proController.cancel();


it's giving me an error saying .... List has no rows for assignment to SObject so please tell me how to solve it..

 

thanks in advance.....

Best Answer chosen by Admin (Salesforce Developers) 
JPClark3JPClark3

Move your Controller instantiation down below your PageReference creation. The test.SetCurrentPage needs to run before you controller is created.

 

 //inserting test data... contains one production ticket and production line item under it...
        production__c p = new production__c();
        p.name = 'new test prodution 0011112';
        insert p;
       
        List<ProductionLineItem__c> plis = new List<ProductionLineItem__c>();
        for(Integer i = 0; i<=3; i++){
            productionLineItem__c pli = new productionLineItem__c();
            pli.production__c = p.id;
            plis.add(pli);
        }
        insert plis;
       
        // now passing the id to related page to use that data inserted above...
        PageReference pageRef = Page.EditProductionTicket;
        pageRef.getParameters().put('id', p.id);
        Test.setCurrentPage(pageRef);

ApexPages.StandardController controller = new ApexPages.StandardController(p);
productionEditControl proController = new productionEditControl(controller);

       
        // calling instance methods...
        proController.save();
        proController.cancel();

 

All Answers

JPClark3JPClark3

Move your Controller instantiation down below your PageReference creation. The test.SetCurrentPage needs to run before you controller is created.

 

 //inserting test data... contains one production ticket and production line item under it...
        production__c p = new production__c();
        p.name = 'new test prodution 0011112';
        insert p;
       
        List<ProductionLineItem__c> plis = new List<ProductionLineItem__c>();
        for(Integer i = 0; i<=3; i++){
            productionLineItem__c pli = new productionLineItem__c();
            pli.production__c = p.id;
            plis.add(pli);
        }
        insert plis;
       
        // now passing the id to related page to use that data inserted above...
        PageReference pageRef = Page.EditProductionTicket;
        pageRef.getParameters().put('id', p.id);
        Test.setCurrentPage(pageRef);

ApexPages.StandardController controller = new ApexPages.StandardController(p);
productionEditControl proController = new productionEditControl(controller);

       
        // calling instance methods...
        proController.save();
        proController.cancel();

 

This was selected as the best answer
aks001aks001

thanks... man .. thank u very much... it's working good.