Salesforce 代码覆盖率很低 (53%),我不知道为什么
Saleseforce code covrage is low (53%) and i dont know why
我已经在沙盒中为 salesforce 创建了一个 class,它运行良好,但是当我试图将它部署到生产环境时,我得到了一个低代码共价。
我尝试了我所知道的一切,但猫找到了解决方案,
这是代码,有帮助吗?
public Class CandidateFileUploads_Ver1{
public String parentId ;
public String idxVal {get;set;}
public Map<String, Attachment> attachments {get;set;}
public Map<String, Attachment> attachmentsDup {get;set;}
public Boolean validateUser {get;set;}
public String multipulDocs {get;set;}
public String userMessage {get;set;}
private static final Map<String, String> descriptionNameMap = new Map<String, String>{
'Upload a scan of the Passport'=>'Passport',
'Upload a Resume (CV)'=>'Resume (CV)'};
private static final Map<String, String> descriptionCheckFieldMap = new Map<String, String>{
'Upload a scan of the Passport'=>'Passport_Attached__c',
'Upload a Resume (CV)'=>'CV_Attached__c'};
public CandidateFileUploads_Ver1(ApexPages.StandardController controller){
idxVal = '';
validateUser = true;
multipulDocs = System.Label.multiple_docs;
userMessage = '';
checkUserValid();
attachments = new Map<String, Attachment>();
attachmentsDup = new Map<String, Attachment>();
parentId = System.currentPageReference().getParameters().get('Id');
//parentId = '0034E00000FVJzA';
List<Attachment> existingAttachments = fetchAllAttachments(parentId);//get all attachements for thew user
for(String key :descriptionNameMap.keySet()){
String kokp = descriptionCheckFieldMap.get(key);
attachments.put(key.toLowerCase(), new Attachment(parentId=parentId, Description=key, body=null,Name = kokp )) ;
}
for(Attachment attach :existingAttachments){
attach.body = null;
attachments.put(attach.Description.toLowerCase(), attach);
}
}
public void checkUserValid(){
String hours = System.Label.hours;
String whitelist = System.Label.whitelist;
ID pId = System.currentPageReference().getParameters().get('Id');
Boolean validateUser = true;
String userMessage;
Contact parenter = new Contact(Id=pId);
// Validate Proccess status: get proccess string status
String getStatus = parenter.Process_Status__c;
String[] whitelistArr = whitelist.split(',');// split proccess string
Boolean checkWhiteList = false;
for(String val : whitelistArr){
if(val == getStatus) {
checkWhiteList = true;
}
}
if(validateUser != checkWhiteList){
validateUser = false;
userMessage = System.Label.error_not_premitted;
return;
}
//Validate Dates
//Datetime getLinktimestamp = (Datetime) contactSObject.get('linktimestamp__c');
Datetime getLinktimestamp = parenter.linktimestamp__c;
Datetime getValidTime = getLinktimestamp.addHours(Integer.valueof(hours.trim())); //add 48 hours
Datetime timeNow = System.now();
if(getValidTime < timeNow){//if if more then 48 hours
validateUser = false;
userMessage = System.Label.error_expired;
return;
}
}
public Datetime getLinktimestamp(){
ID pId = System.currentPageReference().getParameters().get('Id');
Datetime accts = [SELECT linktimestamp__c FROM Contact WHERE Id=:pId].linktimestamp__c;
return accts;
}
public List<Contact> contactObj(){
ID pId = System.currentPageReference().getParameters().get('Id');
sObject mySObject =[SELECT linktimestamp__c,Process_Status__c FROM Contact WHERE Id=:pId];
//Datetime llli = s.linktimestamp__c;
String strObjectName = String.valueOf( mySObject.get('linktimestamp__c') );
List<Contact> obj = [SELECT linktimestamp__c,Process_Status__c FROM Contact WHERE Id=:pId];
return obj;
}
public void uploadAttachment(){
List<Attachment> uploads = new List<Attachment>() ;
List<Attachment> deletes = new List<Attachment>() ;
try{
for(integer i = 0 ; i < attachments.values().size() ; i++){
Attachment attach = attachments.values()[i] ;
if(attach.parentId == NULL){
attach.parentId = System.currentPageReference().getParameters().get('Id');
}
//this is a file that was uploaded now
if(attach.body != NULL){
//remove current attachement before uploading a new one
if(attach.Id != NULL){
deletes.add(attachments.values().remove(i));
}
attach.Name = renameFile(attach.Name, attach.Description);
uploads.add(attach) ;
}
}
if(!deletes.isEmpty()){
delete deletes ;
}
if(!uploads.isEmpty()){
for(Attachment atchmnt :uploads){
atchmnt.Id = null;
}
insert uploads;
Contact parent = new Contact(Id=parentId);
for(Attachment attach :getUpdatedAttachments(uploads).values()){
attach.body = null;
attachments.put(attach.Description.toLowerCase(), attach);
parent.put(descriptionCheckFieldMap.get(attach.Description), true);
}
update parent;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info,'Attachment upload Successfully');
ApexPages.addMessage(myMsg);
}
}
catch(exception e){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'An Error occured when uploading attachment');
ApexPages.addMessage(myMsg);
}
}
public void removeRow(){
Attachment a = attachments.get(idxVal.toLowerCase());
Contact parent = new Contact(Id=parentId);
parent.put(descriptionCheckFieldMap.get(a.Description), false);
delete a;
update parent;
system.debug('attachments before: ' + attachments);
attachments.put(a.Description.toLowerCase(), new Attachment(parentId = parentId, description = a.description, body=null));
system.debug('attachments after: ' + attachments);
}
private static String renameFile(String fileName, String description){
String newName = descriptionNameMap.get(description);
if(String.isNotBlank(fileName) && fileName.contains('.')){
newName += fileName.substring(fileName.lastIndexOf('.'), fileName.length());
}
return newName;
}
private static Map<String,Attachment> getUpdatedAttachments(List<Attachment> attachList){
Map<String,Attachment> attMap = new Map<String,Attachment>();
for(Attachment att : [SELECT Id,Name,Description
FROM Attachment
WHERE Id = :attachList]){
attMap.put(att.Description.toLowerCase(), att) ;
}
return attMap;
}
public List<Attachment> fetchAllAttachments(String parentId){
return [SELECT Id,Name,Description,parentId
FROM Attachment
WHERE ParentId =: parentId
AND Description IN :descriptionNameMap.keySet()];
}
}
你是什么意思"I tried everything i know but cat find a solution"?这是 Salesforce 的要求——Apex 代码应该至少被 75% 的测试覆盖。如果平均覆盖率未达到 75% 的阈值,则部署失败。
此处唯一的解决方案是为您的代码编写测试。
您可以使用 Developer Console 查看您的代码的哪些部分被覆盖,哪些没有覆盖,然后调整您的测试。
在开发者控制台编辑代码时,页面左上角有一个'Code coverage'选项
我已经在沙盒中为 salesforce 创建了一个 class,它运行良好,但是当我试图将它部署到生产环境时,我得到了一个低代码共价。 我尝试了我所知道的一切,但猫找到了解决方案, 这是代码,有帮助吗?
public Class CandidateFileUploads_Ver1{
public String parentId ;
public String idxVal {get;set;}
public Map<String, Attachment> attachments {get;set;}
public Map<String, Attachment> attachmentsDup {get;set;}
public Boolean validateUser {get;set;}
public String multipulDocs {get;set;}
public String userMessage {get;set;}
private static final Map<String, String> descriptionNameMap = new Map<String, String>{
'Upload a scan of the Passport'=>'Passport',
'Upload a Resume (CV)'=>'Resume (CV)'};
private static final Map<String, String> descriptionCheckFieldMap = new Map<String, String>{
'Upload a scan of the Passport'=>'Passport_Attached__c',
'Upload a Resume (CV)'=>'CV_Attached__c'};
public CandidateFileUploads_Ver1(ApexPages.StandardController controller){
idxVal = '';
validateUser = true;
multipulDocs = System.Label.multiple_docs;
userMessage = '';
checkUserValid();
attachments = new Map<String, Attachment>();
attachmentsDup = new Map<String, Attachment>();
parentId = System.currentPageReference().getParameters().get('Id');
//parentId = '0034E00000FVJzA';
List<Attachment> existingAttachments = fetchAllAttachments(parentId);//get all attachements for thew user
for(String key :descriptionNameMap.keySet()){
String kokp = descriptionCheckFieldMap.get(key);
attachments.put(key.toLowerCase(), new Attachment(parentId=parentId, Description=key, body=null,Name = kokp )) ;
}
for(Attachment attach :existingAttachments){
attach.body = null;
attachments.put(attach.Description.toLowerCase(), attach);
}
}
public void checkUserValid(){
String hours = System.Label.hours;
String whitelist = System.Label.whitelist;
ID pId = System.currentPageReference().getParameters().get('Id');
Boolean validateUser = true;
String userMessage;
Contact parenter = new Contact(Id=pId);
// Validate Proccess status: get proccess string status
String getStatus = parenter.Process_Status__c;
String[] whitelistArr = whitelist.split(',');// split proccess string
Boolean checkWhiteList = false;
for(String val : whitelistArr){
if(val == getStatus) {
checkWhiteList = true;
}
}
if(validateUser != checkWhiteList){
validateUser = false;
userMessage = System.Label.error_not_premitted;
return;
}
//Validate Dates
//Datetime getLinktimestamp = (Datetime) contactSObject.get('linktimestamp__c');
Datetime getLinktimestamp = parenter.linktimestamp__c;
Datetime getValidTime = getLinktimestamp.addHours(Integer.valueof(hours.trim())); //add 48 hours
Datetime timeNow = System.now();
if(getValidTime < timeNow){//if if more then 48 hours
validateUser = false;
userMessage = System.Label.error_expired;
return;
}
}
public Datetime getLinktimestamp(){
ID pId = System.currentPageReference().getParameters().get('Id');
Datetime accts = [SELECT linktimestamp__c FROM Contact WHERE Id=:pId].linktimestamp__c;
return accts;
}
public List<Contact> contactObj(){
ID pId = System.currentPageReference().getParameters().get('Id');
sObject mySObject =[SELECT linktimestamp__c,Process_Status__c FROM Contact WHERE Id=:pId];
//Datetime llli = s.linktimestamp__c;
String strObjectName = String.valueOf( mySObject.get('linktimestamp__c') );
List<Contact> obj = [SELECT linktimestamp__c,Process_Status__c FROM Contact WHERE Id=:pId];
return obj;
}
public void uploadAttachment(){
List<Attachment> uploads = new List<Attachment>() ;
List<Attachment> deletes = new List<Attachment>() ;
try{
for(integer i = 0 ; i < attachments.values().size() ; i++){
Attachment attach = attachments.values()[i] ;
if(attach.parentId == NULL){
attach.parentId = System.currentPageReference().getParameters().get('Id');
}
//this is a file that was uploaded now
if(attach.body != NULL){
//remove current attachement before uploading a new one
if(attach.Id != NULL){
deletes.add(attachments.values().remove(i));
}
attach.Name = renameFile(attach.Name, attach.Description);
uploads.add(attach) ;
}
}
if(!deletes.isEmpty()){
delete deletes ;
}
if(!uploads.isEmpty()){
for(Attachment atchmnt :uploads){
atchmnt.Id = null;
}
insert uploads;
Contact parent = new Contact(Id=parentId);
for(Attachment attach :getUpdatedAttachments(uploads).values()){
attach.body = null;
attachments.put(attach.Description.toLowerCase(), attach);
parent.put(descriptionCheckFieldMap.get(attach.Description), true);
}
update parent;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info,'Attachment upload Successfully');
ApexPages.addMessage(myMsg);
}
}
catch(exception e){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'An Error occured when uploading attachment');
ApexPages.addMessage(myMsg);
}
}
public void removeRow(){
Attachment a = attachments.get(idxVal.toLowerCase());
Contact parent = new Contact(Id=parentId);
parent.put(descriptionCheckFieldMap.get(a.Description), false);
delete a;
update parent;
system.debug('attachments before: ' + attachments);
attachments.put(a.Description.toLowerCase(), new Attachment(parentId = parentId, description = a.description, body=null));
system.debug('attachments after: ' + attachments);
}
private static String renameFile(String fileName, String description){
String newName = descriptionNameMap.get(description);
if(String.isNotBlank(fileName) && fileName.contains('.')){
newName += fileName.substring(fileName.lastIndexOf('.'), fileName.length());
}
return newName;
}
private static Map<String,Attachment> getUpdatedAttachments(List<Attachment> attachList){
Map<String,Attachment> attMap = new Map<String,Attachment>();
for(Attachment att : [SELECT Id,Name,Description
FROM Attachment
WHERE Id = :attachList]){
attMap.put(att.Description.toLowerCase(), att) ;
}
return attMap;
}
public List<Attachment> fetchAllAttachments(String parentId){
return [SELECT Id,Name,Description,parentId
FROM Attachment
WHERE ParentId =: parentId
AND Description IN :descriptionNameMap.keySet()];
}
}
你是什么意思"I tried everything i know but cat find a solution"?这是 Salesforce 的要求——Apex 代码应该至少被 75% 的测试覆盖。如果平均覆盖率未达到 75% 的阈值,则部署失败。
此处唯一的解决方案是为您的代码编写测试。
您可以使用 Developer Console 查看您的代码的哪些部分被覆盖,哪些没有覆盖,然后调整您的测试。 在开发者控制台编辑代码时,页面左上角有一个'Code coverage'选项