0

I am new to Salesforce. I have written a trigger to update contact based on account field. The Trigger code is below.

trigger Update_contact_based_on_acc on Account (before update) {
    List<contact> cont = new List<contact>();
    for(Account acc : Trigger.new){
        if(acc.Type == 'Prospect'){
            Contact con = new contact();
            con.AccountId = acc.Id;
            con.FirstName = acc.Name;
            con.LastName = acc.Phone;
            cont.add(con);
        }
    }
    if(cont.size()>0){
        insert cont; 
    }
}

I want to use System.assert,System.AssertEquals,Test.start,test.stop and system.runas to clear my concept. So how can I use these in my test class which is attached below?

@isTest
public class Update_contact_based_on_accTest {
    public static testmethod void PrimaryAccountTest(){

        Account acc = new Account();
        acc.Name = 'Test';
        acc.Type = 'Prospect';
        insert acc; 

        acc.Phone = '8404868903';
        update acc;

        contact con = new contact(FirstName='accnmame',LastName='test',
                                  AccountId=acc.Id);
        insert con; 
        system.assertEquals(expected, actual);
    }
}
  • Your trigger is creating a new Contact whenever any change is made to Account. – Keith C Dec 30 '19 at 11:52
1

A good pattern to follow when writing a testmethod is the Given-When-Then pattern

// Given a mock Account

// When Account Updated

// Then Verify Contact created

then fill in the details

// Given a mock Account
Account mockAccount = new Account(...);
insert mockAccount

// When Account Updated
Test.startTest();
update mockAccount;
Test.stopTest();

// Then Verify Contact created
Contact[] results = [SELECT ... FROM Contact];
System.assertEquals(1,results.size(),'sb 1 Contact per mockAccount');
System.assertEquals(mockAccount.Phone,results[0].LastName,'mockAccount phone becomes Contact LastName');
...

Of course, as this is a trigger, you should test with collections and, if record sharing/CRUD is a concern for certain profiles/permissionSets, then use the System.runAs(..) block to test those dependencies as well (as shown by Rashmi S's answer).

Trailhead is your best resource for testmethods and a deeper dive can be found here on StackExchange

0

Your trigger creates a new contact when Account is updated and if type = 'Prospect'

@isTest
public class Update_contact_based_on_accTest {
    public static testmethod void PrimaryAccountTest(){

        Account acc = new Account();
        acc.Name = 'Test';
        acc.Type = 'Prospect';
        insert acc; 


        Profile p = [SELECT Id FROM Profile WHERE Name='Prof1'];
        User u = new User(Alias = 'usr', Email='usr@testorg.com', EmailEncodingKey='UTF-8', LastName='test', LanguageLocaleKey='en_US',
                            LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='wqutest');

        System.runAs(u) {

            test.startTest();
            acc.Phone = '8404868903';
            update acc;
            test.stopTest();
        }
        contact objContact = [select firstname, lastname from Contact where AccountId = acc.Id ];

        system.assertEquals( 'Test', objContact.FirstName);
    }
}

system.assertEquals: Matches with expected and actual value. If bote are equal, test passes else fails. system.assert: Ex: system.assert(x==y, "value matches");- if value of x is equal to y, it displays "value matches" Test.startTest() and Test.stopTest() - this us used to get the fresh governor limit. For example if your class makes 90 SOQL queries before it calls startTest, you can make additional 100 SOQL within this. system.runas - is used when you want to check the part of functionality for specific profile user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.