You need to sign in to do that
Don't have an account?
kiran25
How to complete Test class for this scenario
Hi I am New Bie to salesforce development moved from Admin to Development few weeks ago .
I am trying to write a test class for the below code.
I am trying to write a test class for the below code.
Public class TriggerHandler_Controller { public void afterInsert(List<RPR__c> objList){ List<Quote> quoteList = new List<Quote>(); List<QuoteLineItem> qliList = new List<QuoteLineItem>(); Pricebookentry pbe = [select Id,pricebook2Id,product2Id from PricebookEntry where product2id = '01t28000000cRQsAAM']; Id oppId = [select id from opportunity where id =: '0060k00000CuKIw' LIMIT 1].Id; Set<Id> accId = new Set<Id>(); List<RPR__c> rprList = new List<RPR__c>(); for(RPR__c rpr : objList) { rprList.add(rpr); accId.add(rpr.Account__c); } List<Opportunity> oppList = [SELECT Id,Name FROM Opportunity WHERE AccountId IN : accId]; System.debug('Account Set'+accId + ' Opportunity List '+ oppList); for(RPR__c rprNew : rprList){ Quote q = new Quote(); q.Name = 'Teston New Quote'+rprNew.Name; q.OpportunityId = oppList[0].Id; q.Ship_To_City__c = rprNew.Shipment_From__c; q.Pricebook2Id = pbe.pricebook2id; quoteList.add(q); } if(!quoteList.isEmpty()){ insert quoteList; } for(Quote q : quoteList){ QuoteLineItem qli = new QuoteLineItem(); qli.QuoteId = q.Id; qli.quantity = 2; qli.pricebookentryId = pbe.id; qli.unitprice = 10; qli.product2Id = pbe.product2Id; qli.Packing_Style__c = q.X1_Packing_Style__c; qliList.add(qli); } if(!qliList.isEmpty()){ insert qliList; } } public void beforeUpdate(List<RPR__c> objList){ Set<Id> RPR_Ids = new Set<Id>(); for(RPR__c obj : objList){ RPR_Ids.add(obj.Id); } Map<Id, RPR__c> oppMap = new Map<Id, RPR__c>([ Select (Select IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId,Actor.Name, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From RPR__c WHERE Id IN : RPR_Ids]); for(RPR__c opp: objList) { RPR__c opp1 = oppMap.get(opp.Id); opp.Approver_Comments__c = ''; for (ProcessInstanceHistory processStep : opp1.ProcessSteps) { opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only. opp.Approver_Comments__c += 'Comments: ' + processStep.comments + ' . Status: ' + processStep.StepStatus + ' . Date: ' + processStep.CreatedDate +' . Commentor Name: ' + processStep.Actor.Name + '\\'; //opp.Approved_Date__c = DateTime.parse(system.now().format()); opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only, DO NOT DELETE, MAY EFFECT REPORT TYPE } opp.Approver_Comments3__c = 'aa'; } } }
The test class with i wrote is showing zero code coverage , please help me out what went wrong and what should be added to get the code coverage for this.
@isTest (SeeAllData=false) public class TriggerHandlerTest { @isTest public static void test() { RPR__c RPR = new RPR__c(); //RPR.id = 'a0A0k00000Pk6GgEAJ'; RPR.Requested_Date__c = Date.newInstance(2010, 07, 15); RPR.Approval_Status2__c = 'Draft'; RPR.Account__c = '0012800000mRNOaAAO'; RPR.Approver_Comments3__c = 'ok'; insert RPR; User submitter = [SELECT Id FROM User WHERE IsActive = true LIMIT 1]; //System.runAs(submitter) { Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest(); r.setObjectId(RPR.Id); r.setNextApproverIds(new Id[] {UserInfo.getUserId()}); //r.setNextApproverIds(null); //r.setNextApproverIds('80010000000EAMZ'); Approval.process(r); //} Test.startTest(); update RPR; Test.stopTest(); }
Try Below Test Class Please Mark It As Best Answer If It Helps
Thank You!
I think Apex Class was posted in instead of test class . can you resend the comment with Apex test class i already have trigger working with fine
here . I need Apex test class for the code coverage aspect .
Try below Test Class Please Mark It As Best Answer If It Helps
Thank You!
This code is still showing 0 % code coverage , can you help to suggest me why its showing zero .
If You have Trigger Then make Changes As Shown In Below Code
Not sure why it still showing 8 % CharuDutt
This Happening Because In Apex Class on Line Number 8 You Are using Produsct2Id = '01t28000000cRQsAAM' WhereAs The Product2Id We Are Inserting From Test Class is Different And Also On Line Number 9 Query Opportunity Where Id = '0060k00000CuKIw' WhereAs The Opportunty We Are Inserting From Test Class is Different Remove These lines
Hi CharuDutt,
For Quoteline item creation its mandatory to have Pricebook thats the reason i added a default product so that i can create quoteline item upon creation.
If i create a RPR custom object Record , this functionality should create a quote and quoteline object record.
The code coverage goes up to 66 % if i removed but i am afraid the code will not work like before to have a quote and quoteline item record .
Need your guidence on this please
yes please , How can we connect i wasnt able send you a direct message , Any meeting will help a lot to join and show you the functionaity
You Can Email Me At [ acssaleforce@gmail.com ]
CharuDutt,
I am using a shared account named Bhavana ,Kiran here Emailed you from my email account , Need your urgent help on this concern please
I have emailed you
was awaiting for your response back to the email sent . please let me know when can i expect response to it