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
MaidyMaidy 

Help with Implementing Triggers

Hi,

 

I am new at using triggers but it seems its the only way I can populate Account custom fields with information from the User object.

 

I have successfully saved the following trigger in Sandbox and it doesn't seem to update my 2 custom fields. Appreciate the help.

 

Also, I have learned that to deploy this in our production I will need Eclipse which will require some test case to run. Will the test method have to be on a separate file? If yes, how do I associate them to make sure that the test method is run with the right trigger? Many thanks for the anticipated assistance.

 

* * *

 

trigger updateOwnerProfile on Account (before insert, before update) {
  Set<Id> AcctOwnerId = new Set<Id>();
  for (Account a:Trigger.new)
    AcctOwnerId.add(a.ownerid); 
      
  Map<Id,User> OwnerProfile = new Map<Id,User>(
    [SELECT id, title, phone FROM user 
     WHERE id = :AcctOwnerId]);
       
  for (Account a:Trigger.new)  
    if(String.valueOf(a.ownerid).substring(0,3)=='00G'){
      a.Account_Owner_Title__c = OwnerProfile.get(a.ownerid).title;
      a.Account_Owner_Phone__c = OwnerProfile.get(a.ownerid).phone;
    }   
}

ngabraningabrani

To test the Trigger, save an Account object. If it is not behaving as expected, the put some System.debugs in the Apex code, and then view the Debug log.

 

The test case needs to be writter in a separate Apex class. You will save an Account in the test case, so the Trigger will automatically get called. You will be able to run the test case by clicking on the Run Test button.

 

You will need to deploy the Trigger and the test case to production. This may be done through the Force.com IDE or by using the Salesforce native user interface.

MaidyMaidy

Hi, 

 

Thanks for the correction. The trigger is now working in our sandbox.

 

I have tried creating the following test class for my trigger but I am unable to save it due to the following error:

 

"System.AssertException: Assertion Failed: Expected: Business Development Manager, VIC, WA & TAS, Actual: null testSetOwnerProfile.cls /Accounts/src/classes line 52 Force.com run test failure


Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required Accounts"

<<Test Method >>
@isTest
private class testSetOwnerProfile {

    static testMethod void myUnitTest() {
     
       String testownid = '';
       Account testacct = new Account();
       testacct.status__c = 'Lead';
       testacct.name = 'Test Account';
       testacct.description = 'Test';
       
       //get one user of proper type, use the where clause to filter
       user u = [SELECT id, title, phone FROM user WHERE id = '00520000001Zb0LAAS'];

       system.runas(u){
         try {
          test.startTest();
          insert testacct;
          test.stopTest();
     testownid = testacct.id;  //if created correctly
         }
         catch(DMLException e){
          system.assert(false, 'Error occurred inserting test record:'+e.getDMLMessage(0));
            }
         }

         Account checkacct = [SELECT ownerid, account_owner_title__c, account_owner_phone__c FROM Account WHERE id = :testownid];

         //validate that the trigger updated the case correctly.
         system.assertequals(u.id, checkacct.ownerid);         
         system.assertequals(u.title, checkacct.account_owner_title__c);
         system.assertequals(u.phone, checkacct.account_owner_phone__c);
    }
}

I am using the Force.com IDE (Eclipse) to deploy the trigger and test class to production. Being new to this (no experience with web or open-source programming), do I simply by click the Run option? And how do I make sure that it is only compiling/running the specific trigger and test class that I want to deploy instead of a whole bunch of objects?
Appreciate the help. Thanks, heaps.

 

ngabraningabrani

As a temporary work-around just comment out the assert statement in the test cases that is generating the error.

 

Each time you deploy any file to production, all the test cases will be executed. This will happen whether you deploy using the IDE or using the Salesforce UI. This is the way Salesforce works.

 

To deploy from Force.com IDE, You can select the files that need to be deployed and right click and select the option Deploy to Production.

MaidyMaidy

Hi,

 

Sorry for the trouble. I did as you recommended to comment out the assert statement in the test case.

 

I selected the trigger and test case that I wanted to deploy and clicked on the Deploy to Server option available.

 

The deployment log result is still showing a FAILED even when the deployment action for the trigger and test case are both showing SUCCESS. It seems that the other objects are being validated as well. Is there a way to avoid this and just have the objects I have created deployed? I know that the trigger works because in sandbox it does.

 

Thanks heaps.

 

*** Deployment Log ***

 

Result: FAILED
Date: 24 February 2011 4:54:48 PM
# Deployed From:
   Project name: Accounts
   Username: maidy@netwealth.com.au
   Endpoint: www.salesforce.com
# Deployed To:
   Username: maidy@netwealth.com.au
   Endpoint: www.salesforce.com
# Deploy Results:
   File Name:    classes/testSetOwnerProfile.cls
   Full Name:  testSetOwnerProfile
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a
   File Name:    package.xml
   Full Name:  package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a
   File Name:    triggers/SetOwnerProfile.trigger
   Full Name:  SetOwnerProfile
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a
# Test Results:
  CreateChecklistItems Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
  DeleteChecklistItems Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
  UpdateDefaultText Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

ngabraningabrani

Salesforce will run all the test cases when any file is deployed to production. All test cases must pass, for the deployment to succeed. You will need to resolve the errors that are coming, before your Trigger can be deployed.

mtanmtan

Hi,

 

Just wanted to thank you for the help. I was able to successfully deploy my changes to our production site.

 

Cheers