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
alok7049alok7049 

help me with test case for the trigger...

I have written test case for this trigger.but can anyone tell me where is wrong in this...

 

trigger copystreetaddress on JunctionObject__c (be​fore insert)
{
      
    if(trigger.isInsert)
    {
        Set<Id> accountId = new Set<Id>();
        Set<Id> contactId = new Set<Id>();
        for(JunctionObject__c tempJ : Trigger.new)
        {
            accountId.add(tempJ.Account__c);
            contactId.add(tempJ.Contact__c);
        }
        
        List<Account> AccountList = [Select id,Nam​e,BillingStreet from Account where id in: accountI​d];
        
        List<Contact> ContactList = [Select id,Nam​e,OtherStreet from Contact where id in: contactId]​;
        
        for(JunctionObject__c tempJ : Trigger.new)
        {
            for(Account tempAcc : AccountList)
            {
                if(tempJ.Account__c == tempAcc.Id)
                {
                    for(Contact tempCon : ContactL​ist)
                    {
                        if(tempJ.Contact__c == tem​pCon.Id)
                        {
                            tempCon.OtherStreet = ​tempAcc.BillingStreet;
                        }                   
                    
                    }
                
                }
            
            }   
            
        }       
            update ContactList;
    }
       
}

 

@isTest
private class copyStreetAddress {   
static testMethod void testStreetAddress() {

Contact con = new Contact(Lastname = 'test contact1',MAILINGSTREET = 'street',otherStreet='Hi', MAILINGCITY = 'city', MAILINGSTATE = 'state', MAILINGPOSTALCODE = 'code', MAILINGCOUNTRY = 'country');  
insert con;
Account acc=new Account(Name='roger federer1' ,billingStreet='hello');
insert acc;
JunctionObject__c Jo=new JunctionObject__c(Name='test12',Account__c=acc.id,contact__c=con.id);
insert Jo;

System.assertEquals(con.otherStreet,acc.billingStreet);

}
}


        

Alex.AcostaAlex.Acosta

From what you're attempting to do here in this example a formula field will do the job just fine. There's no need for a trigger.