function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nicholas MelonasNicholas Melonas 

Apex Trigger to Apex Class

Hello,

I am trying to create an Apex Class for the Apex Trigger below. How would I go about doing this? Here is a link to my original post: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BWej.  Thanks for your help!
 
trigger fillInAccount on MPM4_BASE__Milestone1_Project__c(after insert, after update) {
    Set<Id> oppIds = new Set<Id>();
    
    for(MPM4_BASE__Milestone1_Project__c proj : trigger.new){
        if(proj.Opportunity_Name__c != null){
            oppIds.add(proj.Opportunity_Name__c);
        }
    }
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select AccountId from Opportunity where id in :oppIds]);
    
    Set<Id> accIds = new Set<Id>();
    
    for(Opportunity opp : oppMap.values()){
        accIds.add(opp.AccountId);
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([Select Project__c from Account where Id In :accIds]);//Replace Project__c with your field API name
    
    for(MPM4_BASE__Milestone1_Project__c proj : trigger.new){
        if(oppMap.containsKey(proj.Opportunity_Name__c)) {
            accMap.get(oppMap.get(proj.Opportunity_Name__c).AccountId).Project__c  = proj.id; //Replace Project__c with your field API name
        }
    }
    update accMap.values();
}
Best Answer chosen by Nicholas Melonas
Abhishek BansalAbhishek Bansal
Hi Nicholas,

I have a workaround for this problem.
Just remove that line from apex test class which is giving the error and try to deploy the trigger and test class.
I hope it will definitely resolve the issue.

Modify your test class as below :
@isTest
private class TestFillAccount {
  static testMethod void validateFillAccount() {
	  Account testAcc = new Account(Name = 'Test Account');
	  insert testAcc;
	  
	  Opportunity testOpp = new Opportunity(Name = 'Test Opp', AccountId = testAcc.Id, StageName = 'Test Stage', CloseDate = Date.today().addDays(10));
	  insert testOpp;
	  
	  MPM4_BASE__Milestone1_Project__c testProj = new MPM4_BASE__Milestone1_Project__c(Name = 'Test Project', Opportunity_Name__c = testOpp.id);
	  insert testProj;
 
  }
}


Please let me know the status of your deployment process after doing the changes in test class .

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Nicholas,

Please find test class for your trigger :
 
@isTest
private class TestFillAccount {
  static testMethod void validateFillAccount() {
	  Account testAcc = new Account(Name = 'Test Account');
	  insert testAcc;
	  
	  Opportunity testOpp = new Opportunity(Name = 'Test Opp', AccountId = testAcc.Id, StageName = 'Test Stage', CloseDate = Date.today().addDays(10));
	  insert testOpp;
	  
	  MPM4_BASE__Milestone1_Project__c testProj = new MPM4_BASE__Milestone1_Project__c(Name = 'Test Project', Opportunity_Name__c = testOpp.id);
	  insert testProj;
	  
	  Account acc = [Select Project__c from Account where Id = :testAcc.id];//Replace Project__c with your field API name
	  system.assertEquals(acc.Project__c,testProj.id);//Replace Project__c with your field API name
 
  }
}
Please run this test class and see the code coverage of your trigger in developer console.
If it runs successfully and code coverage is above or equal to 75% than please include this test class along with trigger in your deployment process and migrate it to production.
You will not face any issues then.

Let me know if you need any help on this.

Regards,
Abhishek 
 
Nicholas MelonasNicholas Melonas
Hello Abishek,

So the test class worked great! Now I am doing a change set to get the class and trigger into our production org. However when I validated the class in our production org, it came back with this error:

System.AssertException: Assertion Failed: Expected: null, Actual: a0H4000000GQMdzEAH 
Stack Trace: Class.TestFillAccount.validateFillAccount: line 14, column 1

What did we do wrong? Thanks!

Nick
Abhishek BansalAbhishek Bansal
Hi Nicholas, Have you run this test class on your sandbox. If yes, than does is showing the same error or it ran sucessfully. If it ran successfully on sandbox than than the validate should not fail Please confirm that whether the test class is running fine or not on sandbox. If it is also failing on sandbox than o have to debug your code in order to find out the issue. So if it is possible for you than please share your sandbox credentials or you can contact me on my mail id : abhishek.bansal@metacube.com OR skype Id : abhishek.bansal2790 I will surely help you out. Regards, Abhishek
Nicholas MelonasNicholas Melonas
The Apex Class is running fine in the Sandbox, but when I tried to deploy in Production, it came up with that error! I can email you to discuss more. Thanks, Abishek!
Abhishek BansalAbhishek Bansal
Hi Nicholas,

I have a workaround for this problem.
Just remove that line from apex test class which is giving the error and try to deploy the trigger and test class.
I hope it will definitely resolve the issue.

Modify your test class as below :
@isTest
private class TestFillAccount {
  static testMethod void validateFillAccount() {
	  Account testAcc = new Account(Name = 'Test Account');
	  insert testAcc;
	  
	  Opportunity testOpp = new Opportunity(Name = 'Test Opp', AccountId = testAcc.Id, StageName = 'Test Stage', CloseDate = Date.today().addDays(10));
	  insert testOpp;
	  
	  MPM4_BASE__Milestone1_Project__c testProj = new MPM4_BASE__Milestone1_Project__c(Name = 'Test Project', Opportunity_Name__c = testOpp.id);
	  insert testProj;
 
  }
}


Please let me know the status of your deployment process after doing the changes in test class .

Thanks,
Abhishek Bansal.
This was selected as the best answer
Nicholas MelonasNicholas Melonas
Abhishek,

It works perfect! Thank you so much! This is going to be a huge help to our company!!

Thanks,

Nick