You need to sign in to do that
Don't have an account?
Timmy Ahluwalia
test class on apex which is fetching data only
Hi,
How to write a test class for the apex which is fetching data and displaying.
Thanks
How to write a test class for the apex which is fetching data and displaying.
Thanks
All Answers
you can do it two ways ..
1 . Set SeeAll Data to true which is not recommended ..
2 .insert the data into the test class and query
Thanks
public class CampaignAdvisorTeamController {
@AuraEnabled
public static List<Advisor_Team__c> showme(Id campaignId){
List<campaignmember> campaignMemList = [SELECT Id, Name, Status, ContactId, LeadOrContactId, CampaignId
FROM Campaignmember WHERE CampaignId =:campaignId AND Status = 'Attended'];
Set<Id> MemberSet = new Set<Id>();
for(campaignmember cm: campaignMemList){
MemberSet.add(cm.LeadOrContactId);
}
return [select team__c, team__r.Name, advisor__c, advisor__r.Name,Team_Advisor_Email__c,Team_Parent_Entity__c,
Team__r.Account.ParentId, Team__r.Account.Parent.Name, Advisor__r.Firm_Name__c,advisor__r.account.Name,
advisor__r.Account.Parent.Name,Team__r.Account.Name,
Advisor_Parent_Entity__c,advisor__r.Parent_Entity__c from advisor_team__c
WHERE advisor__c IN: MemberSet AND team__c NOT IN: MemberSet];
}
After displaying i am adding the team member which are not in the campaign member list
the code is below
how do i write this Test class.
public static String apexCampaignMember(list<SObject> rows, Id campaignId ){
List<CampaignMember> campaignMemList = [SELECT Id, Name, Status, ContactId, LeadOrContactId, CampaignId
FROM CampaignMember WHERE CampaignId =:campaignId];
List<CampaignMember> MembersToAdd = new List<CampaignMember>();
Set<Id> MemberSet = new Set<Id>();
for(campaignmember cm: campaignMemList){
MemberSet.add(cm.LeadOrContactId);
}
for(SObject s: rows){
Advisor_Team__c team = (Advisor_Team__c)s;
if(!MemberSet.contains(team.Team__c)){
CampaignMember cm = new CampaignMember();
cm.CampaignId=campaignId;
cm.ContactId=team.Team__c;
cm.Status='Team';
MemberSet.add(team.Team__c);
MembersToAdd.add(cm);
}
}
if(!MembersToAdd.isEmpty()){
Boolean TeamRequired = true;
Integer SortOrder = 0;
for(CampaignMemberStatus stat : [SELECT Id,Label,SortOrder FROM CampaignMemberStatus WHERE CampaignId =: CampaignId ORDER BY SortOrder ASC]){
if(stat.Label == 'Team'){
TeamRequired=false;
SortOrder=SortOrder;
}
}
if(TeamRequired){
SortOrder++;
CampaignMemberStatus NewStat = new CampaignMemberStatus();
NewStat.Label='Team';
NewStat.SortOrder=SortOrder;
NewStat.CampaignId=CampaignId;
insert NewStat;
}
insert MembersToAdd;
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeBooleanField('success', true);
gen.writeStringField('Message', 'Records Added');
gen.writeEndObject();
return gen.getAsString();
} else {
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeBooleanField('success', true);
gen.writeStringField('Message', 'No records to add');
gen.writeEndObject();
return gen.getAsString();
}
}
}
I have used this liink https://rajvakati.com/2018/10/17/lightning-component-clone-with-related-records/#respond for cloning custom objects with related list. Can you please help me with the test class.? it's really urgent.
Thank you!