0

Here is my trigger:

trigger Trigger_Update on Account_Setup__c (After insert){ 

Schema.DescribeFieldResult fieldResult = Account_Setup__c.status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

for (Account_Setup__c B : Trigger.new) {

   Account ac = new Account();

    if(B.status__c == 'Approved'){  

      ac.Name = B.Name;
      ac.industry = B.industry__c;
      ac.region__c = B.region__c;
      ac.Fax = B.Fax__c;
      ac.phone = B.Phone__c;
      ac.website = B.website__c;          
      ac.Account_Email__c = B.email__c; 

      insert ac;

        }

    Else if (B.status__c == 'Rejected'){ 

        }
    }
}

Here is my Class:

@isTest
public class Account_creation_Class{
    // Status of 'Approved'

    static testMethod void approved() {

        Account_Setup__c B = new Account_Setup__c();

        B.status__c = 'Approved';
        B.Name = 'Name' ;
        B.industry__c = 'Industry';
        B.region__c = 'USA';
        B.Fax__c = '111-111-1111';
        B.Phone__c ='222-222-2222';
        B.website__c = 'www.salesforce.com' ;          
        B.email__c = '[email protected]'; 

        insert B;
     }
}

My code coverage shows 100%, but the IF statement is not working. I have a custom field "Status", in my custom object "Account_Set_up". When sales creates account_setup, it goes through an approval process. if approved, it creates an account in the account object; if rejected, nothing happen

the problem I am facing, is that the account gets created either way, whether it was approved, pending or rejected. I guess something is wrong with the code

Also, in the trigger section in the if rejected section, I didn't put a code, because I don't want anything to happen. is that the proper way to tell the code "don't do anything"

Can someone help please? thanks a lot

1
  • Add a few system.debug statements to see where the code is going. Also you shouldn't do any DML inside of a loop like that. You should build a list of Accounts to insert then insert them after the loop.
    – Phil B
    Commented Jul 16, 2014 at 13:41

1 Answer 1

0

If your Account_Setup__c.status__c field had a default value of "Approved" or if users always set that value to start with, that would cause the Account to always be created. The way for you to find out what is going on is to make use of the platform's debug logs as described in e.g. How do I start to debug my own Apex code?.

Normally approval processes are started after an object has been inserted and result in an update of the object with the status outcome of e.g. "Approved" or "Rejected". Your trigger is an "after insert" trigger so it will not fire when that status is set at the end of the approval process. If you do add "after update" to your trigger you will need to be careful to only create the Account when the status field changes from some other value to "Approved" and presumably once only too.

There are lots of "don't want anything to happen" conditions in code so generally it is cleaner to not start trying to code them - I would remove the B.status__c == 'Rejected' branch.

4
  • Keith, the default value was "Pending", I went ahead and removed it. I will check the rest. Thanks a lot
    – Kim
    Commented Jul 16, 2014 at 13:37
  • @Kamal "Pending" should be fine and may be part of the entry criteria for the approval process so I wouldn't suggest that you remove it.
    – Keith C
    Commented Jul 16, 2014 at 13:43
  • Keith, I didn't remove the entry, it was set to be the default, I just unchecked " Make this value the default". that's all
    – Kim
    Commented Jul 16, 2014 at 13:48
  • the code is fine. what I did to fix the issue is go to approval steps, show actions, put a check-mark on"Re-evaluate Workflow Rules after Field Change"
    – Kim
    Commented Jul 16, 2014 at 15:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .