You need to sign in to do that
Don't have an account?
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;
}
}
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.
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:
Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required Accounts"
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.
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 ***
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.
Hi,
Just wanted to thank you for the help. I was able to successfully deploy my changes to our production site.
Cheers