• yearzero
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
Hi, 

I'm getting a failure in my test class. Below is my sample. The piece of code that is being tested updates an Account's Email Domain field with the email domains tied to related Campaigns. For example, if we have an Account X and two Contacts related to Account X with email addresses: cmlam@cmlam.com skry@sky.com, the email domain field should be 'cmlam.com,sky.com'. 

In my test class below, the System.Debug comes back with the correct email domains related to the Account but the test class is apparently failing because in the 'System.AssertsEquals' portion, the actual result is Null. Here is the error message: 
System.AssertException: Assertion Failed: Expected: cmlam.com,sky.com, Actual: null
Stack Trace: Class.ContactTrigger_tests.updateAccountEmailDomains_test: line 34, column 1

Any ideas? 
 
public static testmethod void updateAccountEmailDomains_test(){
        //initialize data
        Account a = new Account(Name = 'Anthem', BillingCountry = 'US', BillingState = 'CA');
        insert a;
        Account a2 = new Account(Name = 'Synergy', BillingCountry = 'US', BillingState = 'CA');
        insert a2;        
        
        Contact c = new Contact(LastName = 'CM Lam', Email = 'cmlam@cmlam.com', AccountId = a.Id, MailingCountry = 'US', MailingState = 'CA');
        Contact c2 = new Contact(LastName = 'Stephanie Kry', Email = 'skry@sky.com', AccountId = a.Id, MailingCountry = 'US', MailingState = 'CA');        
        List<Contact> contactList = new List<Contact>();
        contactList.add(c);
        contactList.add(c2);
        insert contactList;
        
        a = [SELECT Email_Domains__c FROM Account WHERE Id = :a.Id];
        system.assertEquals('cmlam.com,sky.com', a.Email_Domains__c);
        system.debug(a.Email_Domains__c);
        
        //test delete case
        delete c2;
        a = [SELECT Email_Domains__c FROM Account WHERE Id = :a.Id];
        system.assertEquals('cmlam.com', a.Email_Domains__c);
        system.debug(a.Email_Domains__c);

 
Still pretty new to coding so please excuse me if my questions are rudimentary :)

I'd like to keep an audit of our OpportunitySplits. For each Opportunity that has been deleted, I would like to create a record of a custom object I created called "Opportunity Split History" that retains the history of the Opportunity Split that was deleted. I wrote some code to do this but my test class is giving me the following error message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing value for required field(s): U#239.14ff (Opportunity): []. Based on the error message, I'm assuming that there is a field that is missing on the Opportunity but I can't seem to figure out which field it is. I've been looking at this for hours and I have no idea what is wrong. Any help would be appreciated. 

Here is my Test Class
@isTest(seeAllData = True)
public class OpportunitySplit_Trigger_Test {

    @isTest
	public static void Test_createOpportunitySplitHistoryDelete() {
        
		// Data setup
        User u = [SELECT Id FROM User WHERE Profile.Name = 'System Administrator' AND isActive = True Limit 1];
        User u2 = [SELECT Id FROM User WHERE Profile.Name = 'Nativo Standard User' AND isActive = True Limit 1];
		
        Id acctholdingCoRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Holding Company').getRecordTypeId();
        Id acctClientRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client').getRecordTypeId();
        Id acctAgencyRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Agency').getRecordTypeId();
        
        Id oppManagedRT = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Managed Service').getRecordTypeId();
        
        OpportunitySplitType oppSplitType1 = [SELECT ID FROM OpportunitySplitType WHERE DeveloperName = 'Revenue' Limit 1];
        
        List <OpportunitySplit> oppSplitList = new List<OpportunitySplit>();
        
		Account acc1 = new Account();
        acc1.Name = 'Nike';
        acc1.BillingState = 'CA';
        acc1.BillingCountry = 'US';
        acc1.RecordTypeId = acctClientRt;
        acc1.Credit_Status__c = 'Approved';
        insert acc1;
        
        Account holdingco1 = new Account();
        holdingco1.Name = 'WPP';
        holdingco1.BillingState = 'NY';
        holdingco1.BillingCountry = 'US';
        holdingco1.RecordTypeId = acctHoldingCoRT;
        holdingco1.Credit_Status__c = 'Approved';
        insert holdingco1;
        
        Account agcypar = new Account();
        agcypar.Name = 'OMG';
        agcypar.BillingState = 'NY';
        agcypar.BillingCountry = 'US';
        agcypar.RecordTypeId = acctAgencyRT;
        agcypar.Credit_Status__c = 'Approved';
        agcypar.Is_Parent_Agency__c = True;
        agcypar.Holding_Company__c = holdingco1.id;
        insert agcypar;
        
        Account agcy1 = new Account();
        agcy1.Name = 'Client Direct Child';
        agcy1.BillingState = 'NY';
        agcy1.BillingCountry = 'US';
        agcy1.RecordTypeId = acctAgencyRT;
        agcy1.ParentId = agcypar.id;
        agcy1.Holding_Company__c = holdingco1.id;
        agcy1.Credit_Status__c = 'Approved';
        insert agcy1;
        
        Opportunity opp1 = new Opportunity();
        opp1.Account = acc1;
        opp1.Agency__c = agcy1.id;
        opp1.Parent_Account__c = agcypar.id;
        opp1.Holding_Company__c = holdingco1.id;
        opp1.Agency_Credit_Status__c = agcy1.Credit_Status__c;
        opp1.Advertiser_Credit_Status__c = acc1.Credit_Status__c;
        opp1.Credit_Status__c = 'Approved';
        opp1.Amount = 100000;
        opp1.LeadSource = 'Cold Email';
        opp1.CloseDate = Date.today();
        opp1.Contract_End_Date__c = system.date.today().addMonths(2);
        opp1.StageName = 'Prospecting';
        opp1.Name = 'Nike Opportunity 07-31-2020';
        opp1.Type = 'New Business';
        opp1.RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Managed Service' AND sObjectType = 'Opportunity' LIMIT 1].id;

        insert opp1;
        
        OpportunitySplit oppSplit1 = new OpportunitySplit();
        oppSplit1.Opportunity = opp1;
        oppSplit1.SplitPercentage = 50;
        oppSplit1.SplitOwnerId = u.id;
        oppSplit1.SplitTypeId = oppSplitType1.id;
        oppSplit1.SplitNote = 'Blah';
        
        
        OpportunitySplit oppSplit2 = new OpportunitySplit();
        oppSplit2.Opportunity = opp1;
        oppSplit2.SplitPercentage = 25;
        oppSplit2.SplitTypeId = oppSplitType1.id;
        oppSplit2.SplitNote = 'Blah Blah';
        
        OpportunitySplit oppSplit3 = new OpportunitySplit();
        oppSplit3.Opportunity = opp1;
        oppSplit3.SplitPercentage = 25;
        oppSplit3.SplitOwnerId = u2.id;
        oppSplit3.SplitTypeId = oppSplitType1.id;
        oppSplit3.SplitNote = 'Blah Blah Blah';
        

        oppSplitList.add(oppSplit1);
        oppSplitList.add(oppSplit2);
        oppSplitList.add(oppSplit3);
        
        insert oppSplitList;
        
		// Run the code
		Test.startTest();
        delete oppSplit3;
        oppSplit1.SplitPercentage = 75;
        update oppSplit1;
		
        List<Opportunity_Split_History__c> oppSplitHistList = new List<Opportunity_Split_History__c>();
        Opportunity_Split_History__c oppSplitHist1 = new Opportunity_Split_History__c();
        oppSplitHist1.Previous_Split__c = 25;
        oppSplitHist1.Previous_Split_Amount__c = 25000;
        oppSplitHist1.Previous_Split_Member__c = 'Mike Crowley';
        oppSplitHistList.add(oppSplitHist1);
        insert oppSplitHistList;
		Test.stopTest();
		
        System.assertEquals(oppSplitHistList.size(), 1);

	}
    
}
Here is my Handler Class
public class OpportunitySplit_Handler {
	
    public static void createOpportunitySplitHistoryDel(List<OpportunitySplit> oldList) {
        List<Opportunity_Split_History__c> oppSplitHistoryList = new List<Opportunity_Split_History__c>();
        for (OpportunitySplit oppSplit : oldList) {
            Opportunity_Split_History__c oppSplitHistory = new Opportunity_Split_History__c();
            oppSplitHistory.Previous_Split__c = oppSplit.SplitPercentage;
            oppSplitHistory.Previous_Split_Amount__c = oppSplit.SplitAmount;
            oppSplitHistory.Previous_Split_Member__c = oppSplit.SplitOwner.Name;
            oppSplitHistory.Date__c = Date.today();
            oppSplitHistory.Opportunity__c = oppSplit.OpportunityId;
            oppSplitHistory.History_Type__c = 'Delete';
			
            oppSplitHistoryList.add(oppSplitHistory);
            
        }
        
        if(oppSplitHistoryList.size()>0){           
    }
        insert oppSplitHistoryList;
}
    public static void createOpportunitySplitHistoryNew(List<OpportunitySplit> oldList){
        
    }
}

Here is my trigger
trigger OpportunitySplit_Trigger on OpportunitySplit (after delete) {    
    
    if (Trigger.isAfter && Trigger.isDelete) {
        OpportunitySplit_Handler.createOpportunitySplitHistoryDel(Trigger.old);
    }
    
    
}


 
Hi everyone, 

I'm a new developer so please bear that in mind! I'm trying to update an Account's custom fields from a Custom Object called Pub_Opportunity__c. The account has a lookup relationship to the pub_opportunity__c. But I only want the Account to update once with these fields as sometimes there are many pub opportunities related to one Account. I just want to retain the original values. 

When I use the code below, it keeps updating the Account every single time with the new Pub Opportunity's fields regardless of the condition. For example, when I change the Account_Status__c field on the Account to 'Active', when I create a new Pub Opportunity, it still updates the Account_Status__c to 'Closed Won - Implementing'. Even when the Link_to_Contract__c field on the Account is filled out, it still updates the field with the new value from the Pub Opportunity. 

Would appreciate your help!


public static void updatePubs(Map<Id,Pub_Opportunity__c> pubOppMap){
        List<Account> aList = new List<Account>();
        
        for(Pub_Opportunity__c p : pubOppMap.values()){
            Account a = new Account();
            if(a.Account_Status__c != 'Active'){
                a.Account_Status__c = 'Closed Won - Implementing';
                a.id = p.Publisher__c;
               if(String.Isblank(a.Link_to_Contract__c)){
                a.Link_To_Contract__c = p.Link_to_Contract__c;
            }
          
            aList.add(a);
        }
        
        if(aList.size() > 0){
            update alist;
        }
    }
    }
    
Hi, 

I'm getting a failure in my test class. Below is my sample. The piece of code that is being tested updates an Account's Email Domain field with the email domains tied to related Campaigns. For example, if we have an Account X and two Contacts related to Account X with email addresses: cmlam@cmlam.com skry@sky.com, the email domain field should be 'cmlam.com,sky.com'. 

In my test class below, the System.Debug comes back with the correct email domains related to the Account but the test class is apparently failing because in the 'System.AssertsEquals' portion, the actual result is Null. Here is the error message: 
System.AssertException: Assertion Failed: Expected: cmlam.com,sky.com, Actual: null
Stack Trace: Class.ContactTrigger_tests.updateAccountEmailDomains_test: line 34, column 1

Any ideas? 
 
public static testmethod void updateAccountEmailDomains_test(){
        //initialize data
        Account a = new Account(Name = 'Anthem', BillingCountry = 'US', BillingState = 'CA');
        insert a;
        Account a2 = new Account(Name = 'Synergy', BillingCountry = 'US', BillingState = 'CA');
        insert a2;        
        
        Contact c = new Contact(LastName = 'CM Lam', Email = 'cmlam@cmlam.com', AccountId = a.Id, MailingCountry = 'US', MailingState = 'CA');
        Contact c2 = new Contact(LastName = 'Stephanie Kry', Email = 'skry@sky.com', AccountId = a.Id, MailingCountry = 'US', MailingState = 'CA');        
        List<Contact> contactList = new List<Contact>();
        contactList.add(c);
        contactList.add(c2);
        insert contactList;
        
        a = [SELECT Email_Domains__c FROM Account WHERE Id = :a.Id];
        system.assertEquals('cmlam.com,sky.com', a.Email_Domains__c);
        system.debug(a.Email_Domains__c);
        
        //test delete case
        delete c2;
        a = [SELECT Email_Domains__c FROM Account WHERE Id = :a.Id];
        system.assertEquals('cmlam.com', a.Email_Domains__c);
        system.debug(a.Email_Domains__c);

 
Still pretty new to coding so please excuse me if my questions are rudimentary :)

I'd like to keep an audit of our OpportunitySplits. For each Opportunity that has been deleted, I would like to create a record of a custom object I created called "Opportunity Split History" that retains the history of the Opportunity Split that was deleted. I wrote some code to do this but my test class is giving me the following error message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing value for required field(s): U#239.14ff (Opportunity): []. Based on the error message, I'm assuming that there is a field that is missing on the Opportunity but I can't seem to figure out which field it is. I've been looking at this for hours and I have no idea what is wrong. Any help would be appreciated. 

Here is my Test Class
@isTest(seeAllData = True)
public class OpportunitySplit_Trigger_Test {

    @isTest
	public static void Test_createOpportunitySplitHistoryDelete() {
        
		// Data setup
        User u = [SELECT Id FROM User WHERE Profile.Name = 'System Administrator' AND isActive = True Limit 1];
        User u2 = [SELECT Id FROM User WHERE Profile.Name = 'Nativo Standard User' AND isActive = True Limit 1];
		
        Id acctholdingCoRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Holding Company').getRecordTypeId();
        Id acctClientRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client').getRecordTypeId();
        Id acctAgencyRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Agency').getRecordTypeId();
        
        Id oppManagedRT = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Managed Service').getRecordTypeId();
        
        OpportunitySplitType oppSplitType1 = [SELECT ID FROM OpportunitySplitType WHERE DeveloperName = 'Revenue' Limit 1];
        
        List <OpportunitySplit> oppSplitList = new List<OpportunitySplit>();
        
		Account acc1 = new Account();
        acc1.Name = 'Nike';
        acc1.BillingState = 'CA';
        acc1.BillingCountry = 'US';
        acc1.RecordTypeId = acctClientRt;
        acc1.Credit_Status__c = 'Approved';
        insert acc1;
        
        Account holdingco1 = new Account();
        holdingco1.Name = 'WPP';
        holdingco1.BillingState = 'NY';
        holdingco1.BillingCountry = 'US';
        holdingco1.RecordTypeId = acctHoldingCoRT;
        holdingco1.Credit_Status__c = 'Approved';
        insert holdingco1;
        
        Account agcypar = new Account();
        agcypar.Name = 'OMG';
        agcypar.BillingState = 'NY';
        agcypar.BillingCountry = 'US';
        agcypar.RecordTypeId = acctAgencyRT;
        agcypar.Credit_Status__c = 'Approved';
        agcypar.Is_Parent_Agency__c = True;
        agcypar.Holding_Company__c = holdingco1.id;
        insert agcypar;
        
        Account agcy1 = new Account();
        agcy1.Name = 'Client Direct Child';
        agcy1.BillingState = 'NY';
        agcy1.BillingCountry = 'US';
        agcy1.RecordTypeId = acctAgencyRT;
        agcy1.ParentId = agcypar.id;
        agcy1.Holding_Company__c = holdingco1.id;
        agcy1.Credit_Status__c = 'Approved';
        insert agcy1;
        
        Opportunity opp1 = new Opportunity();
        opp1.Account = acc1;
        opp1.Agency__c = agcy1.id;
        opp1.Parent_Account__c = agcypar.id;
        opp1.Holding_Company__c = holdingco1.id;
        opp1.Agency_Credit_Status__c = agcy1.Credit_Status__c;
        opp1.Advertiser_Credit_Status__c = acc1.Credit_Status__c;
        opp1.Credit_Status__c = 'Approved';
        opp1.Amount = 100000;
        opp1.LeadSource = 'Cold Email';
        opp1.CloseDate = Date.today();
        opp1.Contract_End_Date__c = system.date.today().addMonths(2);
        opp1.StageName = 'Prospecting';
        opp1.Name = 'Nike Opportunity 07-31-2020';
        opp1.Type = 'New Business';
        opp1.RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Managed Service' AND sObjectType = 'Opportunity' LIMIT 1].id;

        insert opp1;
        
        OpportunitySplit oppSplit1 = new OpportunitySplit();
        oppSplit1.Opportunity = opp1;
        oppSplit1.SplitPercentage = 50;
        oppSplit1.SplitOwnerId = u.id;
        oppSplit1.SplitTypeId = oppSplitType1.id;
        oppSplit1.SplitNote = 'Blah';
        
        
        OpportunitySplit oppSplit2 = new OpportunitySplit();
        oppSplit2.Opportunity = opp1;
        oppSplit2.SplitPercentage = 25;
        oppSplit2.SplitTypeId = oppSplitType1.id;
        oppSplit2.SplitNote = 'Blah Blah';
        
        OpportunitySplit oppSplit3 = new OpportunitySplit();
        oppSplit3.Opportunity = opp1;
        oppSplit3.SplitPercentage = 25;
        oppSplit3.SplitOwnerId = u2.id;
        oppSplit3.SplitTypeId = oppSplitType1.id;
        oppSplit3.SplitNote = 'Blah Blah Blah';
        

        oppSplitList.add(oppSplit1);
        oppSplitList.add(oppSplit2);
        oppSplitList.add(oppSplit3);
        
        insert oppSplitList;
        
		// Run the code
		Test.startTest();
        delete oppSplit3;
        oppSplit1.SplitPercentage = 75;
        update oppSplit1;
		
        List<Opportunity_Split_History__c> oppSplitHistList = new List<Opportunity_Split_History__c>();
        Opportunity_Split_History__c oppSplitHist1 = new Opportunity_Split_History__c();
        oppSplitHist1.Previous_Split__c = 25;
        oppSplitHist1.Previous_Split_Amount__c = 25000;
        oppSplitHist1.Previous_Split_Member__c = 'Mike Crowley';
        oppSplitHistList.add(oppSplitHist1);
        insert oppSplitHistList;
		Test.stopTest();
		
        System.assertEquals(oppSplitHistList.size(), 1);

	}
    
}
Here is my Handler Class
public class OpportunitySplit_Handler {
	
    public static void createOpportunitySplitHistoryDel(List<OpportunitySplit> oldList) {
        List<Opportunity_Split_History__c> oppSplitHistoryList = new List<Opportunity_Split_History__c>();
        for (OpportunitySplit oppSplit : oldList) {
            Opportunity_Split_History__c oppSplitHistory = new Opportunity_Split_History__c();
            oppSplitHistory.Previous_Split__c = oppSplit.SplitPercentage;
            oppSplitHistory.Previous_Split_Amount__c = oppSplit.SplitAmount;
            oppSplitHistory.Previous_Split_Member__c = oppSplit.SplitOwner.Name;
            oppSplitHistory.Date__c = Date.today();
            oppSplitHistory.Opportunity__c = oppSplit.OpportunityId;
            oppSplitHistory.History_Type__c = 'Delete';
			
            oppSplitHistoryList.add(oppSplitHistory);
            
        }
        
        if(oppSplitHistoryList.size()>0){           
    }
        insert oppSplitHistoryList;
}
    public static void createOpportunitySplitHistoryNew(List<OpportunitySplit> oldList){
        
    }
}

Here is my trigger
trigger OpportunitySplit_Trigger on OpportunitySplit (after delete) {    
    
    if (Trigger.isAfter && Trigger.isDelete) {
        OpportunitySplit_Handler.createOpportunitySplitHistoryDel(Trigger.old);
    }
    
    
}


 
Hi everyone, 

I'm a new developer so please bear that in mind! I'm trying to update an Account's custom fields from a Custom Object called Pub_Opportunity__c. The account has a lookup relationship to the pub_opportunity__c. But I only want the Account to update once with these fields as sometimes there are many pub opportunities related to one Account. I just want to retain the original values. 

When I use the code below, it keeps updating the Account every single time with the new Pub Opportunity's fields regardless of the condition. For example, when I change the Account_Status__c field on the Account to 'Active', when I create a new Pub Opportunity, it still updates the Account_Status__c to 'Closed Won - Implementing'. Even when the Link_to_Contract__c field on the Account is filled out, it still updates the field with the new value from the Pub Opportunity. 

Would appreciate your help!


public static void updatePubs(Map<Id,Pub_Opportunity__c> pubOppMap){
        List<Account> aList = new List<Account>();
        
        for(Pub_Opportunity__c p : pubOppMap.values()){
            Account a = new Account();
            if(a.Account_Status__c != 'Active'){
                a.Account_Status__c = 'Closed Won - Implementing';
                a.id = p.Publisher__c;
               if(String.Isblank(a.Link_to_Contract__c)){
                a.Link_To_Contract__c = p.Link_to_Contract__c;
            }
          
            aList.add(a);
        }
        
        if(aList.size() > 0){
            update alist;
        }
    }
    }