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
SFDC12SFDC12 

help for testclass scenario

Hi everyone,
Below trigger and testclass ,trigger working fine ,there is no code coverage for the trigger,what am missing in testclass,can someone plz help me out.

Trigger,
trigger updatephonebasedonaddress1 on Account (after update) {
List<contact>conlist=new List<contact>();
set<Id>accid=new set<id>();
    for(Account a:trigger.new){
        accid.add(a.id);
    }
    List<Account>acclist=[select id,name,phone,BillingCountry,(select id,Phone,LastName,MailingCountry from contacts)from Account where id=:accid];
    for(Account a:acclist){
        for(contact c:a.contacts){
            if(c.MailingCountry==a.BillingCountry){
                c.phone=a.phone;
                conlist.add(c);
            }
        }
        update conlist;
    }
}

Testclass:

@isTest
public class Testupdatephonebasedonaddress1 {
@isTest
static void clme(){
Account a=new Account();
a.Name='test';
a.BillingCountry='inr';
a.Phone='2328938';
insert a;
Contact c=new Contact();
c.LastName='conname';
c.MailingCountry='inr';
c.AccountId=a.Id;
insert c;
Test.startTest();
List<Contact> con=[select id,name,phone from Contact where Accountid=:a.Id];
con[0].phone=a.Phone;
update con;

Test.stopTest();
}
}
Best Answer chosen by SFDC12
CharuDuttCharuDutt
Hii AnshiSFDC

Trigger Is Working If Account BillingCountry And Contact MailingCountry Are Same then update Contact phone With Account Phone okay
//First We Create Account With BillingCountry India
Account a=new Account();
a.Name='test';
a.BillingCountry='India';
a.Phone='2328938';
insert a;

//First We Create Contact With MailingCountry USA
Contact c=new Contact();
c.LastName='conname';
c.MailingCountry='USA';
c.AccountId=a.Id;
insert c;

//Then We Update Account BillingCountry here Cause If We Update Account Before Creating Contact The Trigger Will Work But It Will Not Be Able To Find  Related Contact Hence Contact Phone Will not Be Updated
a.BillingCountry = 'USA';
update a;
Please Close Your Query By Marking It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Anshi SFDC
Try Below Test Class
@isTest
public class Testupdatephonebasedonaddress1 {
@isTest
static void clme(){

Account a=new Account();
a.Name='test';
a.BillingCountry='India';
a.Phone='2328938';
insert a;
    
Contact c=new Contact();
c.LastName='conname';
c.MailingCountry='USA';
c.AccountId=a.Id;
insert c;

a.BillingCountry = 'USA';
update a;

}
}
Please Mark It As Best Answer If It Helps
Thank You!
SFDC12SFDC12
Hi thanks for the reply May I know why we are updating billing country here,they should be equal right.
CharuDuttCharuDutt
Hii AnshiSFDC

Trigger Is Working If Account BillingCountry And Contact MailingCountry Are Same then update Contact phone With Account Phone okay
//First We Create Account With BillingCountry India
Account a=new Account();
a.Name='test';
a.BillingCountry='India';
a.Phone='2328938';
insert a;

//First We Create Contact With MailingCountry USA
Contact c=new Contact();
c.LastName='conname';
c.MailingCountry='USA';
c.AccountId=a.Id;
insert c;

//Then We Update Account BillingCountry here Cause If We Update Account Before Creating Contact The Trigger Will Work But It Will Not Be Able To Find  Related Contact Hence Contact Phone Will not Be Updated
a.BillingCountry = 'USA';
update a;
Please Close Your Query By Marking It As Best Answer If It Helps
Thank You!
This was selected as the best answer
SFDC12SFDC12
Thankyou so much..