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
conpasconpas 

How to build testmethod apex class for this apex trigger?

Helle, i want to build the testmethod apex class for a apex class trigger, but i dont obtain any result.

 

trigger t on Account (after update) {
      for (Account a : Trigger.new) {
      List<Contact> contactdata = [Select Id, AccountId, Name From Contact where AccountId = :a.Id];
if(contactdata.size()>0) {
List<User> userdata = [Select Id, ContactId From User where ContactId = :contactdata.get(0).Id];
if(userdata.size()>0) {
userdata.get(0).accspot__c = a.Spot_lead_time__c;
userdata.get(0).accstory__c = a.Storyboard_lead_time__c;
update userdata.get(0);
}
}
}
}

 

 

Thanks!!!

Best Answer chosen by Admin (Salesforce Developers) 
ashish raiashish rai

Hello, 

       Well use the below mention testmethod for that.Be sure to put the desired value for mendetory fields.

@isTestprivate class AccountTest

{public ststic testMethod void testAccount()

{

Profile p = [select id from profile where name='System Administrator'];

User u1 = new User(alias = 'standt2', email='standarduser@test12.com',emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,firstname='Heather',timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');   

insert u1;

account a=new account(name='test',Spot_lead_time__c=Some value,Storyboard_lead_time__c=Some value);

insert a;

contact con=new contact(firstname='testing',lastname='contact1',accountid=a.id,ownerId=u1.id);

insert con;

a.name='test1';

update a;

}

}

 

Think this will help you.

All Answers

ashish raiashish rai

Hello, 

       Well use the below mention testmethod for that.Be sure to put the desired value for mendetory fields.

@isTestprivate class AccountTest

{public ststic testMethod void testAccount()

{

Profile p = [select id from profile where name='System Administrator'];

User u1 = new User(alias = 'standt2', email='standarduser@test12.com',emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,firstname='Heather',timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');   

insert u1;

account a=new account(name='test',Spot_lead_time__c=Some value,Storyboard_lead_time__c=Some value);

insert a;

contact con=new contact(firstname='testing',lastname='contact1',accountid=a.id,ownerId=u1.id);

insert con;

a.name='test1';

update a;

}

}

 

Think this will help you.

This was selected as the best answer
Rohit_SFDCRohit_SFDC

is your code for portal user??

 

conpasconpas

Thank you very much

conpasconpas

Rohit_SFDC wrote:

is your code for portal user??

 


Is for standard enterprise users.