• Mike Reynolds 6
  • NEWBIE
  • 45 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 13
    Replies
Hey, 

I have a basic trigger, with a facade class handling my business logic. In the facade, I have a void method that is calling another helper. My code is all good, and it does what I want, but I'm having an issue with my test class not covering the helper that is called from the facade. I'm thinking it may be due to the call being made from a void method. Here's the relevant code:

Trigger
trigger caseTriggerAll on Case (Before Insert, Before Update) {
        if (Trigger.isBefore) {
            CaseTriggerFacade.isBefore(Trigger.new);
        } 
}
Facade
public class CaseTriggerFacade {
    
    public static void isBefore(List<Case> casesToUpdate) {
        List<Case> caseList = new List<Case>();
		for (Case c :casesToUpdate) {
			if (c.description != null && c.description.trim() != '') {
				caseList.add(c);
			}
		}
		HelperCaseDescUpdate.descUpdate(caseList);
	}
}
Helper:
public class caseDescUpdate {
    public static void descUpdate(case[] cases) {
        for (case cse :cases){
            cse.description = cse.description.replace ('"Text to find a remove.', ''); 
        }
    }
}

Test Class:
@isTest
private class caseDescUpdateTestClass{
    static testMethod void validateCaseDescUpdate() {
       case cse = new case(
           description='Previous text. \n Text to find and remove.'
       );
       System.debug('Description before update: ' + cse.description);
       // Insert case
       insert cse;
       // Retrieve the new case
       cse = [SELECT description FROM case WHERE Id =:cse.Id];
       System.debug('Description after trigger fired: ' + cse.description);
       // Test that the trigger correctly updated the description
       System.assertEquals('Previous text.', cse.description);
    }
}

The trigger and facade are covered 100%, but I can't get coverage for the helper. Any ideas on how to get coverage? Thanks. 
-Mike






 
Hey, 

I've got a custom object called Deposit__c that is related to Case and Account. Each Deposit must have a Case, and an Account. Due to rules in place outside of salesforce, I know this will always be the case. I'm having issues getting things ironed out so I'm hoping someone can see where I've gone wrong...
 
trigger DepositTrigger on Deposit__c (before insert) {
	Set<String> AcctNums = new Set<String>();
    for(Deposit__c d : Trigger.New){
        AcctNums.add(d.SBR_ID__c);
    }
    List<Case> caseList = [select id from Case where RecordTypeId = '0121a0000001nWh' and LastModifiedDate < LAST_N_DAYS:30 and case.Account.Name in :AcctNums];
    Map<String,Id> accountCaseIdMap = new Map<String,Id>();
    for(Case c : caseList){
        accountCaseIdMap.put(c.Account.Name,c.Id);
    }
    for(Deposit__c d2: Trigger.new){
        if(case.Id != accountCaseIdMap.get(d2.Id)){
            Case newCase = new Case(Account = d2.Account.Name, RecordTypeId = '0121a0000001nWh');
            caseList.add(newCase);
        }
    }
    upsert caseList;
    for(Case c : caseList){
        accountCaseIdMap.put(c.SBR_ID__c,c.Id);
    }
    for(Deposit__c d3 : Trigger.New){
        Case depCase = accountCaseIdMap.get(d3.SBR_ID__c);
        d3.CaseId = depCase;
    }
    update accountCaseIdMap.values;
}
It is possible that within the same file, multiple deposit__c records will be for the same account. It is also possible that some deposit__c records will not have cases open, while others will. If there is an open case(or a recently closed one) i want to relate the new deposit__c record to the existing case and not create a new one. 

My current issue is that on line 13 I have an Invalid foreign key relationship: Deposit__c.Account. I have no idea why this is happening. Any thoughts?

Thanks in advance. 



 
Hey - I'm new (as everyone on here says) and i'm being thrown by an error message. Here is my test code:
@isTest
public class emailTriggerTestClass {
    static testMethod void isIncomingEmailTest() {
        case newCase = new Case();
        insert newCase;
        emailMessage newEmail = new emailMessage();
            newEmail.FromAddress = 'Email@No.com';
            newEmail.ToAddress = 'Feedback@no.com';
            newEmail.CcAddress = 'ccAddress@verify.com';
            newEmail.FromName = 'Someones Name';
            newEmail.Incoming = True;
            newEmail.ParentId=newCase.id;
        test.startTest();
            insert newEmail;
        test.stopTest();
           newCase=[select Id, Email_To_Address__c,From_Address__c,Email_CC_Address__c,Email_From_Name__c from case where id =:newEmail.ParentId];
    System.assertEquals ('email@no.com', newCase.From_Address__c);
    System.assertEquals ('Feedback@no.com', newCase.Email_To_Address__c);
    System.assertEquals ('ccAddress@verify.com', newCase.Email_CC_Address__c);
    System.assertEquals ('Someones Name', newCase.Email_From_Name__c);
    }
}
I'm getting the following error when I try to run my test:
"No tests to run. Add test methods to your test class."

This makes no sense to me. Any ideas?

Thanks - Mike



 
Hey, 

I have a simple trigger that puts basic emailMessage data on the related parent case after the parent case is inserted. See the relevant trigger snippet below:
if (Trigger.isAfter)
	{
    List<case> cases=new List<Case>();
	List<EmailMessage> email=[select ToAddress, FromAddress, CcAddress, FromName from EmailMessage where ParentId IN : trigger.newMap.keyset() and Incoming = true and Parent.Number_of_Messages_Received__c = 0];
	map<Id,EmailMessage> mapCaseId_Email = new map<Id,EmailMessage>();
	for(EmailMessage objEmail : email){
		mapCaseId_Email.put(objEmail.ParentId, objEmail); 
		}	
	if(!mapCaseId_Email.isEmpty()){
		for(Case c:Trigger.new){
			if(mapCaseId_Email.containsKey(c.Id)){}
				c.Email_To_Address__c=email[0].ToAddress;
				c.From_Address__c=email[0].FromAddress;
				c.Email_CC_Address__c=email[0].CcAddress;
				c.Email_From_Name__c=email[0].FromName;
				cases.add(c);
			}
		if(cases.size()>0){
			update cases;
			}
		}
	}

The trigger is great, but I can't get good code coverage because I need to insert the case with a related emailMessage record and I have no idea how to make that happen. Here's what I have now:
@isTest
private class caseTriggerAllTestIsAfter {
    static testMethod void isAfterCaseTest(){
        case newCase = new case ();
        	newCase.Email_CC_Address__c = '';
        	newCase.Email_From_Name__c = '';
        	newCase.Email_To_Address__c = '';
        	newCase.From_Address__c = '';
        emailMessage newEmail = new emailMessage();
           	newEmail.FromAddress = 'Email@No.com';
    		newEmail.ToAddress = 'Feedback@no.com';
    		newEmail.CcAddress = 'ccAddress@verify.com';
    		newEmail.FromName = 'Someones Name';
    		newEmail.Incoming = True;
    		newEmail.ParentId = newCase.id;
	insert newCase;
    insert newEmail;
	System.assertEquals ('Email@No.com', newCase.From_Address__c);
   	System.assertEquals ('Feedback@no.com', newCase.Email_To_Address__c);
   	System.assertEquals ('ccAddress@verify.com', newCase.Email_CC_Address__c);
   	System.assertEquals ('Someones Name', newCase.Email_From_Name__c);
    }
}

I know that if I create the case first, i can populate the emailMessage.ParentId as the newCase.id, but at that point, the trigger will have already fired because the case has already been inserted. Does anyone know how to create the child emailMessage first, and then associate it to the case at insert? Any help will be greatly appreciated! 

Thanks, Mike.


 
Hey, 

I'm trying to update a case with some details of the related email message. I need the case to be updated before Assignment rules are run. I would also like to limit the trigger to only fire when this is the first email associated to the case. I've been able to get it this far, but it's not working and I have no idea where I've gone wrong. Any help is greatly appreciated. 

Once this is working I'll move much of the work into a helper class and refactor the trigger into the existing case trigger. 
 
trigger EmailToCaseTrigger on Case(after insert) {
    system.debug('Here in Trigger');
    List<case> cases=new List<Case>();
    for(Case c:Trigger.new){
        List<EmailMessage> em=[select ToAddress,FromAddress,CcAddress,FromName from EmailMessage 
                               where ParentId=:c.Id and Incoming=true and Parent.Number_of_Messages_Received__c=0];
        	if(em!=null && em.size()>0){
            	c.Email_To_Address__c=em[0].ToAddress;
            	c.From_Address__c=em[0].FromAddress;
            	c.Email_CC_Address__c=em[0].CcAddress;
            	c.Email_From_Name__c=em[0].FromName;
            	cases.add(c);
        	}
    	}
    if(cases.size()>0)
    update cases;         
}

 
Hello, 

I need to make my trigger fire only when the case description is not null. is this the right way to approach this? I've really new to APEX and it's really throwing me for a loop. 

trigger descUpdate on Case (before update) {    
    case[] cases = Trigger.new;
    if(case.description !=null)
    caseDescUpdate.descUpdate(cases);
}

Thanks!

-Mike
Hey, 

I'm trying to update a checkbox with a button. The script doesn't have any errors, but i'm getting a popup that says "Unexpected token ILLEGAL" 

Any ideas? Here's the script:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
var newRecords = []; 
var opp = new sforce.SObject(“Opportunity”); 
opp.id =”{!Opportunity.Id}”; 
opp.Call_Attempt_No_Answer__c=True; 
newRecords.push(opp); 
result = sforce.connection.update(newRecords); 
window.location.reload();


Thanks!
-Mike
Hey, 

I have a basic trigger, with a facade class handling my business logic. In the facade, I have a void method that is calling another helper. My code is all good, and it does what I want, but I'm having an issue with my test class not covering the helper that is called from the facade. I'm thinking it may be due to the call being made from a void method. Here's the relevant code:

Trigger
trigger caseTriggerAll on Case (Before Insert, Before Update) {
        if (Trigger.isBefore) {
            CaseTriggerFacade.isBefore(Trigger.new);
        } 
}
Facade
public class CaseTriggerFacade {
    
    public static void isBefore(List<Case> casesToUpdate) {
        List<Case> caseList = new List<Case>();
		for (Case c :casesToUpdate) {
			if (c.description != null && c.description.trim() != '') {
				caseList.add(c);
			}
		}
		HelperCaseDescUpdate.descUpdate(caseList);
	}
}
Helper:
public class caseDescUpdate {
    public static void descUpdate(case[] cases) {
        for (case cse :cases){
            cse.description = cse.description.replace ('"Text to find a remove.', ''); 
        }
    }
}

Test Class:
@isTest
private class caseDescUpdateTestClass{
    static testMethod void validateCaseDescUpdate() {
       case cse = new case(
           description='Previous text. \n Text to find and remove.'
       );
       System.debug('Description before update: ' + cse.description);
       // Insert case
       insert cse;
       // Retrieve the new case
       cse = [SELECT description FROM case WHERE Id =:cse.Id];
       System.debug('Description after trigger fired: ' + cse.description);
       // Test that the trigger correctly updated the description
       System.assertEquals('Previous text.', cse.description);
    }
}

The trigger and facade are covered 100%, but I can't get coverage for the helper. Any ideas on how to get coverage? Thanks. 
-Mike






 
Hey, 

I've got a custom object called Deposit__c that is related to Case and Account. Each Deposit must have a Case, and an Account. Due to rules in place outside of salesforce, I know this will always be the case. I'm having issues getting things ironed out so I'm hoping someone can see where I've gone wrong...
 
trigger DepositTrigger on Deposit__c (before insert) {
	Set<String> AcctNums = new Set<String>();
    for(Deposit__c d : Trigger.New){
        AcctNums.add(d.SBR_ID__c);
    }
    List<Case> caseList = [select id from Case where RecordTypeId = '0121a0000001nWh' and LastModifiedDate < LAST_N_DAYS:30 and case.Account.Name in :AcctNums];
    Map<String,Id> accountCaseIdMap = new Map<String,Id>();
    for(Case c : caseList){
        accountCaseIdMap.put(c.Account.Name,c.Id);
    }
    for(Deposit__c d2: Trigger.new){
        if(case.Id != accountCaseIdMap.get(d2.Id)){
            Case newCase = new Case(Account = d2.Account.Name, RecordTypeId = '0121a0000001nWh');
            caseList.add(newCase);
        }
    }
    upsert caseList;
    for(Case c : caseList){
        accountCaseIdMap.put(c.SBR_ID__c,c.Id);
    }
    for(Deposit__c d3 : Trigger.New){
        Case depCase = accountCaseIdMap.get(d3.SBR_ID__c);
        d3.CaseId = depCase;
    }
    update accountCaseIdMap.values;
}
It is possible that within the same file, multiple deposit__c records will be for the same account. It is also possible that some deposit__c records will not have cases open, while others will. If there is an open case(or a recently closed one) i want to relate the new deposit__c record to the existing case and not create a new one. 

My current issue is that on line 13 I have an Invalid foreign key relationship: Deposit__c.Account. I have no idea why this is happening. Any thoughts?

Thanks in advance. 



 
Hey - I'm new (as everyone on here says) and i'm being thrown by an error message. Here is my test code:
@isTest
public class emailTriggerTestClass {
    static testMethod void isIncomingEmailTest() {
        case newCase = new Case();
        insert newCase;
        emailMessage newEmail = new emailMessage();
            newEmail.FromAddress = 'Email@No.com';
            newEmail.ToAddress = 'Feedback@no.com';
            newEmail.CcAddress = 'ccAddress@verify.com';
            newEmail.FromName = 'Someones Name';
            newEmail.Incoming = True;
            newEmail.ParentId=newCase.id;
        test.startTest();
            insert newEmail;
        test.stopTest();
           newCase=[select Id, Email_To_Address__c,From_Address__c,Email_CC_Address__c,Email_From_Name__c from case where id =:newEmail.ParentId];
    System.assertEquals ('email@no.com', newCase.From_Address__c);
    System.assertEquals ('Feedback@no.com', newCase.Email_To_Address__c);
    System.assertEquals ('ccAddress@verify.com', newCase.Email_CC_Address__c);
    System.assertEquals ('Someones Name', newCase.Email_From_Name__c);
    }
}
I'm getting the following error when I try to run my test:
"No tests to run. Add test methods to your test class."

This makes no sense to me. Any ideas?

Thanks - Mike



 
Hey, 

I have a simple trigger that puts basic emailMessage data on the related parent case after the parent case is inserted. See the relevant trigger snippet below:
if (Trigger.isAfter)
	{
    List<case> cases=new List<Case>();
	List<EmailMessage> email=[select ToAddress, FromAddress, CcAddress, FromName from EmailMessage where ParentId IN : trigger.newMap.keyset() and Incoming = true and Parent.Number_of_Messages_Received__c = 0];
	map<Id,EmailMessage> mapCaseId_Email = new map<Id,EmailMessage>();
	for(EmailMessage objEmail : email){
		mapCaseId_Email.put(objEmail.ParentId, objEmail); 
		}	
	if(!mapCaseId_Email.isEmpty()){
		for(Case c:Trigger.new){
			if(mapCaseId_Email.containsKey(c.Id)){}
				c.Email_To_Address__c=email[0].ToAddress;
				c.From_Address__c=email[0].FromAddress;
				c.Email_CC_Address__c=email[0].CcAddress;
				c.Email_From_Name__c=email[0].FromName;
				cases.add(c);
			}
		if(cases.size()>0){
			update cases;
			}
		}
	}

The trigger is great, but I can't get good code coverage because I need to insert the case with a related emailMessage record and I have no idea how to make that happen. Here's what I have now:
@isTest
private class caseTriggerAllTestIsAfter {
    static testMethod void isAfterCaseTest(){
        case newCase = new case ();
        	newCase.Email_CC_Address__c = '';
        	newCase.Email_From_Name__c = '';
        	newCase.Email_To_Address__c = '';
        	newCase.From_Address__c = '';
        emailMessage newEmail = new emailMessage();
           	newEmail.FromAddress = 'Email@No.com';
    		newEmail.ToAddress = 'Feedback@no.com';
    		newEmail.CcAddress = 'ccAddress@verify.com';
    		newEmail.FromName = 'Someones Name';
    		newEmail.Incoming = True;
    		newEmail.ParentId = newCase.id;
	insert newCase;
    insert newEmail;
	System.assertEquals ('Email@No.com', newCase.From_Address__c);
   	System.assertEquals ('Feedback@no.com', newCase.Email_To_Address__c);
   	System.assertEquals ('ccAddress@verify.com', newCase.Email_CC_Address__c);
   	System.assertEquals ('Someones Name', newCase.Email_From_Name__c);
    }
}

I know that if I create the case first, i can populate the emailMessage.ParentId as the newCase.id, but at that point, the trigger will have already fired because the case has already been inserted. Does anyone know how to create the child emailMessage first, and then associate it to the case at insert? Any help will be greatly appreciated! 

Thanks, Mike.


 
Hey, 

I'm trying to update a case with some details of the related email message. I need the case to be updated before Assignment rules are run. I would also like to limit the trigger to only fire when this is the first email associated to the case. I've been able to get it this far, but it's not working and I have no idea where I've gone wrong. Any help is greatly appreciated. 

Once this is working I'll move much of the work into a helper class and refactor the trigger into the existing case trigger. 
 
trigger EmailToCaseTrigger on Case(after insert) {
    system.debug('Here in Trigger');
    List<case> cases=new List<Case>();
    for(Case c:Trigger.new){
        List<EmailMessage> em=[select ToAddress,FromAddress,CcAddress,FromName from EmailMessage 
                               where ParentId=:c.Id and Incoming=true and Parent.Number_of_Messages_Received__c=0];
        	if(em!=null && em.size()>0){
            	c.Email_To_Address__c=em[0].ToAddress;
            	c.From_Address__c=em[0].FromAddress;
            	c.Email_CC_Address__c=em[0].CcAddress;
            	c.Email_From_Name__c=em[0].FromName;
            	cases.add(c);
        	}
    	}
    if(cases.size()>0)
    update cases;         
}

 
Hello, 

I need to make my trigger fire only when the case description is not null. is this the right way to approach this? I've really new to APEX and it's really throwing me for a loop. 

trigger descUpdate on Case (before update) {    
    case[] cases = Trigger.new;
    if(case.description !=null)
    caseDescUpdate.descUpdate(cases);
}

Thanks!

-Mike