Test Class For Appval Process Salesforce - salesforce

I create this controller to diplay on a visualforce page a custom approval related list of record with the signature (photo) of Actor for an object Called Car_Maintenance. It works perfectly on my sandbox but i have trouble to write the test Class to deploy it in the production.
The Controller and my test class are below. My headache start at the line 53 in the Test Class. the error message is some field are required in the approval record.
Controller
public MaintenanceRequestController() {
}
public Map<Id,User> userMap { get; set; }
public List<ProcessInstanceStep> processInstanceList { get; set; }
public MaintenanceRequestController(ApexPages.StandardController controller) {
userMap = new Map<Id, User>();
processInstanceList = new List<ProcessInstanceStep>();
Id recordId = ApexPages.CurrentPage().getparameters().get('id');
processInstanceList = [Select id,ActorId, Actor.Name,CreatedDate, Actor.Title, Actor.FirstName , ProcessInstanceId,
Actor.LastName, StepStatus, ProcessInstance.TargetObjectId
from ProcessInstanceStep
WHERE ProcessInstance.TargetObjectId = :recordId];
for (ProcessInstanceStep step : processInstanceList){
userMap.put(step.ActorId, new User(Signature__c=null));
}
userMap.putAll([SELECT Id,Name,Signature__c FROM User WHERE Id = :userMap.keySet()]);
}
}```
test Class
```#IsTest
public class CarMaitenanceControllerTest{
static testMethod void CarMaitenanceController(){
Car__c veh = new Car__c ( Category__c='Pickup', Color__c='Red', Condition__c='Excellent',
Description__c='Test Vehicle', Engine_Number__c='XXX',
Matriculation__c = 'IT-00011', Make__c='Toyota', Year__c ='2017', Model__c='Colorado');
insert veh;
Part_Accessories_Reception__c par = new Part_Accessories_Reception__c();
par.Date__c = System.today();
par.Provider__c = 'Other';
insert par;
Part_Accessories__c pa = new Part_Accessories__c(Name = 'filter');
insert pa;
Part_Accessories_Inventory__c pai = new Part_Accessories_Inventory__c();
pai.Quantity_Received__c =7771;
pai.Part_Accessories_Reception__c = par.ID;
pai.Part_Accessories__c = pa.Id;
insert pai;
Car_Maintenance__c carMain = new Car_Maintenance__c();
carMain.Car__c = veh.ID;
carMain.Cheque_Bank__c = '444-Cmmm';
carMain.Company__c = 'FFP';
carMain.Date__c = System.today();
insert carMain;
Maintenance_Details__c mdet = new Maintenance_Details__c();
mdet.Description__c = 'qiuyiuyi';
mdet.Quantity__c =555;
mdet.Maintenance__c = carMain.Id;
mdet.Description__c ='kjalhsdhfklasdf';
mdet.Cost__c = 999;
mdet.Part_Accessories_Inventory__c = pai.Id;
insert mdet;
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(carMain.Id);
Approval.ProcessResult result = Approval.process(app);
ProcessInstance pi = new ProcessInstance ();
pi.TargetObjectId = carMain.Id;
pi.Status ='Approved';
Insert pi;
ProcessInstanceStep pis = new ProcessInstanceStep();
pis.ProcessInstanceId = pi.Id;
Insert pis;
Test.setCurrentPageReference(new PageReference('CarMaintenanceView'));
System.currentPageReference().getParameters().put('id', veh.ID);
CarMaintenanceController CRC = new CarMaintenanceController();
MaintenanceRequestController mrc = new MaintenanceRequestController();
}
}```

Can you identify which is line 53 of the test class, the one that is giving you trouble? And the specific error?
It may be that the required fields in production are different than what's in the sandbox, and that's tripping you up when you insert your test data.

Related

FLS Create for Saleforce Object

Is there a way to apply FLS Create check - Schema.sObjectType.Account.fields.Name.isCreateable() to the following?
public static Account createAccount() {
return new Account(
Name = 'Test',
OwnerId = UserInfo.getUserId()
);
}
Wondering if there is a way to apply without re-writing to the following:
public static Account createAccount() {
Account a = new Account();
if (Schema.sObjectType.Account.fields.Name.isCreateable()) {
a.Name = 'Test';
}
if (Schema.sObjectType.Account.fields.OwnerId.isCreateable()) {
a.OwnerId = UserInfo.getUserId();
}
insert a;
}
You can create a generic method that can iterate on each field to check for the FLS.
if access available then retain
if access not available then remove the particular field from the object instance.
I Have created a Generic Method to truncate the non-writable fields as follow:
public static List<SObject> truncateNotWriteableFields(List<SObject> listSObject){
Set<String> readOnlyFields = new Set<String>();
List<SObject> listSObjectNew = new List<SObject>();
if(listSObject.size() < 1){
return listSObjectNew;
}
Schema.SObjectType sObjType = listSObject.getSObjectType();
for(SObjectField field : sObjType.getDescribe().fields.getMap().values()){
if(field.getDescribe().isAccessible() && !field.getDescribe().isUpdateable() && !field.getDescribe().isCreateable()){
readOnlyFields.add(String.valueOf(field));
}
}
readOnlyFields.remove('Id'); // avoid removal in update
for(SObject obj : listSObject){
Map<String, Object> objMap = (Map<String, Object>) JSON.deserializeUntyped( JSON.serialize( obj ) );
objMap.keySet().removeAll(readOnlyFields);
SObject objWithoutNotWritableFields = (SObject) JSON.deserialize( JSON.serialize( objMap ), SObject.class );
system.debug('objWithoutNotWritableFields=>'+objWithoutNotWritableFields);
listSObjectNew.add(objWithoutNotWritableFields);
}
return listSObjectNew;
}

How to improve the coverage of this test class in Apex?

I have created a test class with 51% code coverage till line no 34.
Further, I tried to satisfy if condition but I couldn't. Now I am not getting how to do with 100% code coverage.
Here is the Apex class:
public class AssignProjectController {
public String CaseIds;
public String status {get;set;}
public List<Project__c> activeProjects {get;set;}
public String keyWordSearched {get;set;}
public Id projectId {get;set;}
public AssignProjectController (){
CaseIds = ApexPages.currentPage().getParameters().get('id');
}
public void getProjects(){
status = '';
String searchQuery = 'FIND \'' + keyWordSearched + '*\' IN ALL FIELDS RETURNING Project__c (id,Name,Description__c where Status__c =\'Active\')';
try{
List<List<Project__c >> searchList = search.query(searchQuery);
activeProjects = searchList[0];
if(activeProjects.size() == 0) status = 'No search result found.';
}catch(Exception ex){
system.debug('ex..'+ex.getMessage());
}}}
public PageReference assignProjectToCases(){
List<Case__c> customSettingList = Case__c.getall().values();
List<String> settingRecordTypeList = new List<String>();
for(Case__c caseObj:customSettingList){
settingRecordTypeList.add(caseObj.Name);
}
List<RecordType> recordTypeListData = [SELECT Id FROM RecordType WHERE SObjectType = 'Case' and Name In : settingRecordTypeList];
if(CaseIds != null){
List<String> caseIDList = new List<String>();
caseIDList = CaseIds.split(',');
if([Select id from Case where Id In : caseIDList and RecordType.Id NOT In : recordTypeListData].size() > 0){
status = 'failed';
}else{
List<Case> cases = [Select id,Project__c,RecordType.Name from Case where Id In : caseIDList and RecordType.Id In : recordTypeListData];
if(cases.size() > 0){
for(case caseOb: cases){
caseOb.Project__c = projectId ;
}
try{
update cases ;
status = 'Changes are scheduled';
}catch(Exception ex){
system.debug('AssignProjectController :::'+ex.getMessage());
status = 'Something Went Wrong';
}}}}
return null;
}}
Here is the test class- which I tried to resolve
#isTest public class TestAssignProjectController {
public static Project__c insertProject(){
Project__c proObj = new Project__c();
proObj.Name = 'testProject';
proObj.Status__c = 'Active';
proObj.Description__c = 'for testing';
proObj.Internal_Email_Alias__c = 'a#test.com';
return proObj;
}
public static Account getAccount(){
Account accoObj = new Account();
accoObj.Name = 'testAcc';
accoObj.Location__c = 'testLocation';
accoObj.Type = 'CM';
accoObj.BillingCountry = 'United States';
return accoObj;
}
public static Contact insertContact(Account accObj){
Contact conObj = new Contact();
conObj.FirstName = 'test';
conObj.LastName = 'testLastname';
conObj.AccountId = accObj.Id;
conObj.Email = 'abc#gmail.com';
return conObj;
}
public static Id getTechTypeId(){
return Schema.SObjectType.Case.getRecordTypeInfosByName().get('Tech ').getRecordTypeId();
}
public static Case insertCase(String conId, String proId){
Case caseObj = new Case();
caseObj.Project__c = proId;
caseObj.ContactId = conId;
caseObj.Status = 'Open';
caseObj.Inquiry_Type__c = 'All';
caseObj.Subject = 'TestSubject';
caseObj.Description = 'TestDescription';
caseObj.Case_Is_Reopened__c = false;
caseObj.RecordTypeId = getTechTypeId();
return caseObj;
}
public static testmethod void testMethodExecution(){
AssignController asigncon = new AssignController ();
Project__c proObj = insertProject();
insert proObj;
System.assertEquals(proObj.Status__c,'Active');
Account accObj = getAccount();
insert accObj;
System.assertNotEquals(accObj.Id,null);
Contact conObj = insertContact(accObj);
insert conObj;
System.assertNotEquals(conObj.Id,null);
Case caseObj = insertCase(conObj.Id, proObj.Id);
insert caseObj;
system.debug(caseObj);
//Set baseURL & case ID
PageReference pageRef = Page.Assign;
pageRef.getParameters().put('id',caseObj.id+',');
AssignController asigncon1 = new AssignController ();
asigncon1.getProjects();
asigncon1.assignProjectToCases();
}}
If you are referring if(cases.size() > 0) this statement, then surely there is problem of inserting the case. Make sure that insert caseObj; is working and inserts data in Salesforce backend.
If there is no data in case object, the test method cannot cover the if statement.

How to write a test function for salesforce ConnectApi.FeedElement

this is my code
public class ChatterFunction{
public ChatterFunction(){
LIST<ISSUE__C> issID = [SELECT Id, Name FROM ISSUE__C];
for(ISSUE__C i : issID){
postfeed(i.Id);
}
}
public static void postfeed(String iss){
ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
mentionSegmentInput.id = '00528045147FTed';
messageBodyInput.messageSegments.add(mentionSegmentInput);
textSegmentInput.text = 'Something';
messageBodyInput.messageSegments.add(textSegmentInput);
feedItemInput.body = messageBodyInput;
feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
feedItemInput.subjectId = iss;
ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(null,feedItemInput );
}
}
and I tried to test it with code
#IsTest(SeeAllData=true) public static void testpostfeed() {
System.assertEquals(postfeed('a000l0013315hgr'),null);
}
but it shows the error message: Method does not exist or incorrect signature: postfeed(String)
I have no idea why it doesn't exist ....
Your method is static so you need to call it like this
System.assertEquals(ChatterFunction.postfeed('...'), null);

System.TypeException: Invalid integer salesforce test class error in controller

i have a visual force page which is used as a view to send a custom sms to the leads generated in salesforce.The year field is a number field on salesforce. Posting the controller and test class error. Also mentioning the error line.
Controller :-
//Class to send Message to Lead or Account
public class nmSendSMS
{
//Name of Lead or Account
public string strName{get;set;}
public Lead objLead {get;set;}
public String defaultNumbersToBeAdded;
public String leadYear{get;set;}
//Mobile number of lead of account
public string strMobile{get;set;}
public String messageToBeSent{get;set;}
public List<SelectOption> getYear{get;set;}
public string strSMSBody{get;set;}
//To disable fields if data is not available
String session,statusOfLead,stringOfMobileNumbers;
public nmSendSMS()
{
objLead = new Lead();
leadYear ='';
defaultNumbersToBeAdded = '9820834921,9920726538';
messageToBeSent = '';
stringOfMobileNumbers= '';
}
//Method to send SMS
public PageReference SendSMS()
{
if(leadYear!='' || messageToBeSent!='')
{
session = objLead.nm_Session__c;
integer lengthOfCommaSeperatedNumbers;
String finalString ='';
statusOfLead = objLead.Status;
list<lead> leadNumbersList = [select MobilePhone from Lead where Status=:statusOfLead and nm_Session__c=:session and nm_Year__c=:integer.valueOf(leadYear)];
for(Lead obj :leadNumbersList)
{
stringOfMobileNumbers = obj.MobilePhone+','+stringOfMobileNumbers;
}
System.debug('stringOfMobileNumbers -->'+stringOfMobileNumbers);
lengthOfCommaSeperatedNumbers = stringOfMobileNumbers.length();
finalString = stringOfMobileNumbers.substring(0,lengthOfCommaSeperatedNumbers-1);
finalString = finalString + defaultNumbersToBeAdded;
System.debug('Final String--->'+finalString+'Message To Be Sent-->'+messageToBeSent);
String response = SMSSenderWebService.sendSMSForNotContactedLead(finalString,messageToBeSent);
System.debug('Response-->'+response);
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please Mention all the fields For Lead Search'));
return null;
}
return null;
}
public List<SelectOption> getYear()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('2016','2016'));
options.add(new SelectOption('2017','2017'));
options.add(new SelectOption('2018','2018'));
options.add(new SelectOption('2019','2019'));
return options;
}
}
Test Class:-
#isTest
public class nmSendSMSTracker
{
public static Lead obj;
public static nm_Centers__c objLearningCenter;
public static nm_Program__c program;
public static nm_EligiblityCriteria__c eligibility ;
static testMethod void tesMethod()
{
LoadData();
PageReference pg = new PageReference('/apex/nmSendSMS');
Test.setCurrentPage(pg);
Test.StartTest();
nmSendSMS smsSend = new nmSendSMS();
smsSend.getYear();
smsSend.objLead = obj;
smsSend.messageToBeSent ='Hello Vikas';
smsSend.SendSMS();
Test.StopTest();
}
static void LoadData()
{
nm_EscalationMatrix__c objCustomSeetings3 = new nm_EscalationMatrix__c();
objCustomSeetings3.name='0-1 Week';
objCustomSeetings3.nm_LCEscalationTime__c='22:45';
objCustomSeetings3.nm_RemidertoIC__c='22:45';
objCustomSeetings3.nm_HOEscalationTime__c='20:56';
objCustomSeetings3.nm_RemidertoHO__c='22:45';
insert objCustomSeetings3;
nm_EscalationMatrix__c objCustomSeetings = new nm_EscalationMatrix__c();
objCustomSeetings.name='2-4 Months';
objCustomSeetings.nm_LCEscalationTime__c='20:45';
objCustomSeetings.nm_RemidertoIC__c='21:45';
objCustomSeetings.nm_HOEscalationTime__c='20:56';
objCustomSeetings.nm_RemidertoHO__c='21:45';
insert objCustomSeetings;
nm_EscalationMatrix__c objCustomSeetings2 = new nm_EscalationMatrix__c();
objCustomSeetings2.name='3-6 Week';
objCustomSeetings2.nm_LCEscalationTime__c='20:34';
objCustomSeetings2.nm_RemidertoIC__c='21:45';
objCustomSeetings2.nm_HOEscalationTime__c='20:56';
objCustomSeetings2.nm_RemidertoHO__c='21:45';
insert objCustomSeetings2;
nm_Holidays__c objHoliday = new nm_Holidays__c();
objHoliday.Name='Holi';
objHoliday.nm_Date__c=system.today();
insert objHoliday;
// profile objprofile =[SELECT Id FROM Profile WHERE Name='System Administrator'];
user usr = [Select id from user limit 1];
SystemConfiguration__c objSystemConfiguration=new SystemConfiguration__c();
objSystemConfiguration.name='test';
objSystemConfiguration.nm_BusinessHoursStartTime__c='012213';
objSystemConfiguration.nm_BusinessHoursEndTime__c='0234533';
insert objSystemConfiguration;
Recordtype rt=[select id from Recordtype where sobjectType='nm_Centers__c' AND name ='Learning Center'];
objLearningCenter = new nm_Centers__c();
objLearningCenter.RecordTypeID =rt.id;
objLearningCenter.nm_CenterCode__c ='002';
objLearningCenter.nm_CenterCity__c='Delhi';
objLearningCenter.nm_City__c='Delhi';
objLearningCenter.nm_StateProvince__c='Delhi';
objLearningCenter.nm_Street__c='Laxmi Ngar';
objLearningCenter.nm_PostalCode__c='110091';
insert objLearningCenter;
program = new nm_Program__c();
program.nmIsActive__c = true;
program.nm_ProgramCode__c = 'test';
program.nm_ProgramDuration__c= 2.0;
program.nm_ProgramName__c = 'Post grad diploma finance';
program.nm_ProgramValidity__c = 4;
program.nm_TotalSemesters__c = 4;
program.nm_Type__c = 'Post Graduate Diploma Program';
insert program;
eligibility = new nm_EligiblityCriteria__c();
eligibility.Name = 'Bachelors degree';
eligibility.nm_EligiblityCriteria__c = 'bjhwbghbjgw';
eligibility.Experience_Required_In_Year__c= 2;
eligibility.Graduation_Percentage__c = 6;
eligibility.Graduation_Required__c = true;
insert eligibility;
obj = new Lead();
obj.Email='amit.kumar#saasfocus.com';
obj.MobilePhone='8377985721';
obj.FirstName='sandy';
obj.LastName='babar';
obj.nm_BloodGroup__c='B+';
obj.nm_Gender__c='male';
obj.nm_FathersName__c='subhash';
obj.nm_MothersName__c='kalpana';
obj.nm_StateProvince_P__c='maharashtra';
obj.nm_Nationality__c='Indian';
obj.nm_Street_P__c='xyz';
obj.nm_LocalityName__c='mohitep';
obj.nm_SelfLearningMaterial__c='Send to my shipping address';
obj.Status='Cold';
obj.nm_Session__c = 'January';
obj.nm_NameofBoard__c='CBSE';
obj.nm_EligiblityCriteria__c = eligibility.id;
obj.nm_Program__c = program.id;
obj.nm_InformationCenter__c =objLearningCenter.id;
obj.nm_10thPercentage__c=77.00;
obj.nm_NameofBoard__c='ICSC';
obj.nm_YearofCompletion__c='2000';
obj.nm_NameofSchool__c='nutan';
obj.nm_Class12OrDiploma__c='HSC';
obj.nm_NameofBoard12__c='LCSC';
obj.nm_YearofCompletion12__c='2002';
obj.nm_NameofSchool12__c='dfg';
obj.nm_Stream__c='adw';
obj.nm_BachelorsDegreeName__c='gfc';
obj.nm_Specialization__c='gf';
obj.nm_NameofUniversity__c='G K university';
obj.nm_BachelorsDegreePercentage__c=55.00;
obj.nm_GraduationDegreeMode__c='fgc';
obj.nm_YearofCollegeCompletion__c='2006';
obj.LeadSource='Web';
obj.OwnerId=usr.id;
insert obj;
}
}
Error Message:
System.TypeException: Invalid integer:
Class.nmSendSMS.SendSMS: line 37, column 1
Class.nmSendSMSTracker.tesMethod: line 19, column 1
List<Lead> leadNumbersList = [select MobilePhone from Lead where nm_Year__c=:leadYear];
Works for me and I get the correct Lead(s) in the list

Apex Test case for Email Class

I have following class to send email
global class SendConfirmation {
public SendConfirmation(ApexPages.StandardController controller)
{
}
Webservice static void SendEmail(string contactId,string oppId)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(contactId);
mail.setWhatId(oppId);
mail.setTemplateId('00Xd0000000PFaY');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
ablove class is fine
but not able to get following test case to complete code coverage in eclipse
#isTest
private class SendConfirmationTestCase {
private static testMethod void myUnitTest() {
Contact con = new Contact();
con.FirstName = 'Anil';
con.LastName = 'Dutt';
con.Email = 'anil#swiftsetup.com';
insert con;
Opportunity oppNew = new Opportunity();
oppNew.Name = 'Test Opp';
oppNew.StageName = 'Ticketing';
oppNew.CloseDate = System.now().date();
insert oppNew;
//ApexPages.StandardController sc = new ApexPages.StandardController(con);
//SendConfirmation sc1=new SendConfirmation (sc);
//sc1.SendEmail();
}
}
If i comment out last 3 lines from test case
Following error is coming
SendConfirmationTestCase: Invalid type: SendConfirmation
Thanks in advance for your help..
Try this, it's testing at 100% for me.
global class SendConfirmation
{
public SendConfirmation(ApexPages.StandardController controller)
{
}
Webservice static void SendEmail(string contactId,string oppId)
{
Messaging.SingleEmailMessage mail
= new Messaging.SingleEmailMessage();
mail.setTargetObjectId(contactId);
mail.setWhatId(oppId);
// assuming this Template ID exists in your org
mail.setTemplateId('00Xd0000000PFaY');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
private static testMethod void myUnitTest()
{
Contact con = new Contact();
con.FirstName = 'Anil';
con.LastName = 'Dutt';
con.Email = 'anil#swiftsetup.com';
insert con;
Opportunity oppNew = new Opportunity();
oppNew.Name = 'Test Opp';
oppNew.StageName = 'Ticketing';
oppNew.CloseDate = System.now().date();
insert oppNew;
ApexPages.StandardController sc
= new ApexPages.StandardController(con);
SendConfirmation sc1=new SendConfirmation (sc); // test constructor
// Not: sc1.SendEmail();
// Because method is a webservice in a global class
SendConfirmation.SendEmail(con.Id,oppNew.Id);
}
}

Resources