-1

Im new in salesforce programming.. I have a trigger called CR_Approved and test class called CR_Approved_Test.. The trigger purpose = if Change_Request_Item__c.Status__c = Approved and recordType = New Account, then insert, else if Update Account, then update..

trigger CR_Approved on Change_Request_Item__c (Before Update) {
if (trigger.isBefore) {
    for (Change_Request_Item__c cri : trigger.new){
        // check apakah New atau Update.
        if (cri.Status__c == 'Approved' && trigger.old[0].Status__c != 'Approved') {
            if (cri.Request_Type__c == 'New') {
                // Insert Account
                Account ac = new Account();
                ac.Ownerid = cri.OwnerId;
                ac.Company_Birthday__c = cri.New_Acc_Company_Birthday__c;
                ac.Company_Establishment_Deed__c = cri.New_Acc_Company_Establishment_Deed__c;
                ac.Company_Type__c = cri.New_Acc_Company_Type__c;
                ac.Customer_Group__c = cri.New_Acc_Customer_Group__c;
                ac.Customer_Type__c = cri.New_Acc_Customer_Type__c;
                ac.Employee_Number__c = cri.New_Acc_Employee_Number__c;
                ac.Joining_Since__c = cri.New_Acc_Joining_Since__c;
                ac.Name = cri.New_Acc_Name__c;
                ac.Parentid = cri.New_Acc_Parent_Account__c;
                ac.Register_Type__c = cri.New_Acc_Register_Type__c;
                ac.Service_Type__c = cri.New_Acc_Service_Type__c;
                ac.VIP_Type__c = cri.New_Acc_VIP_Type__c;
                ac.Website = cri.New_Acc_Website__c;

                insert ac;
                cri.Account__c = ac.id;
                cri.Reff_Account__c = ac.id;

                // Insert Contact
                Contact co = new Contact();

                co.AccountId = ac.id;
                co.Address__c = cri.New_C_Address__c;
                co.Birthdate = cri.New_C_Birthdate__c;
                co.Email = cri.New_C_Email__c;
                co.Email_2__c = cri.New_C_Email2__c;
                co.Fax = cri.New_C_Fax__c;
                co.FirstName = cri.New_C_First_Name__c;
                co.Kewarganegaraan__c = cri.New_C_Kewarganegaraan__c;
                co.LastName = cri.New_C_Last_Name__c;
                co.Masa_Berlaku_Identitas__c = cri.New_C_Masa_Berlaku_Identitas__c;
                //co.MiddleName = cri.New_C_Middle_Name__c;
                co.MobilePhone = cri.New_C_Mobile__c;
                co.Mobile_2__c = cri.New_C_Mobile_2__c;
                co.Nomor_Identitas__c = cri.New_C_Nomor_Identitas__c;
                co.Phone = cri.New_C_Phone__c;
                co.Phone_2__c = cri.New_C_Phone_2__c;
                co.Religion2__c= cri.New_C_Religion__c;
                co.Salutation = cri.New_C_Salutation__c;
                co.Street__c = cri.New_C_Street__c;
                //co.Suffix = cri.New_C_Suffix__c;
                co.Posisi__c = cri.New_C_Title__c;
                insert co;
                cri.Contact__c = co.id;

                // Insert Account NPWP
                Account_NPWP__c an = new Account_NPWP__c();
                an.Ownerid = cri.OwnerId;
                an.NPWP_Address__c = cri.New_Acc_N_NPWP_Address__c;
                an.NPWP_Name__c = cri.New_Acc_N_NPWP_Name__c;
                an.Name = cri.New_Acc_N_NPWP_No__c;
                an.Account__c = ac.id;

                insert an;
                cri.Account_NPWP__c = an.id;
                cri.Reff_Account_NPWP__c = an.id;

                // Insert Billing Account
                Billing_Account__c ba = new Billing_Account__c();
                ba.Ownerid = cri.OwnerId;
                ba.Billing_Street__c = cri.New_BA_Billing_Street__c;
                ba.Billing_Address__c = cri.New_BA_Billing_Address__c;
                ba.Branch__c = cri.New_BA_Branch__c;
                ba.Name = ac.Name+'-'+cri.New_BA_Branch__c; //cri.New_BA_Name__c;
                ba.No_SIOPSUS__c = cri.New_BA_No_SIOPSUS__c;
                ba.No_SIUPAL__c = cri.New_BA_No_SIUPAL__c;
                ba.NPWP__c = an.id; //cri.New_BA_NPWP__c;
                ba.Three_Party_Code__c = cri.New_BA_Payment_Type__c;
                ba.SKPT__c = cri.New_BA_SKPT__c;
                ba.SKTD_No__c = cri.New_BA_SKTD_No__c;
                ba.SKTD_Tgl_Berlaku__c = cri.New_BA_SKTD_Tgl_Berlaku__c;
                ba.SKTD_Tgl_Berakhir__c = cri.New_BA_SKTD_Tgl_Berakhir__c;
                ba.SKTD_Tgl_Terbit__c = cri.New_BA_SKTD_Tgl_Terbit__c;
                ba.Tgl_Berlaku_SIOPSUS__c = cri.New_BA_Tgl_Berlaku_SIOPSUS__c;
                ba.Tgl_Terbit_SIOPSUS__c = cri.New_BA_Tgl_Terbit_SIOPSUS__c;
                ba.Tgl_Terbit_SIUPAL__c = cri.New_BA_Tgl_Terbit_SIUPAL__c;
                ba.Named_User__c = cri.Named_User__c;
                ba.Account__c = ac.id;
                if (cri.New_BA_Primary__c == 'Yes') ba.Primary__c = TRUE;

                insert ba;
                cri.Billing_Account__c = ba.id;
                cri.Reff_Billing_Account__c = ba.id;

                // Insert Related Contact
                Related_Contact__c rc = new Related_Contact__c();
                rc.Name = cri.New_C_First_Name__c+' '+cri.New_C_Last_Name__c+' - '+cri.New_BA_Branch__c;
                rc.Ownerid = cri.OwnerId;
                rc.Account__c = ac.id;
                rc.Billing_Account__c = ba.id;
                rc.Contact__c = co.id;
                rc.Status_Customer__c = 'Draft';

                insert rc;
                cri.Related_Contact__c = rc.id;

                // Insert Billing Account Bank
                Billing_Account_Bank__c bab = new Billing_Account_Bank__c();
                bab.Account__c = ac.id;
                bab.Billing_Account__c = ba.id;
                bab.Bank_Name__c = cri.New_BAB_Bank_Name__c;
                bab.Account_Number__c = cri.New_BAB_Account_Number__c;
                bab.AutoCollection__c = cri.New_BAB_Auto_Collection__c;
                bab.AutoCollection_Barang2__c = cri.New_BAB_AutoCollection_Barang__c;
                bab.AutoCollection_BM2__c = cri.New_BAB_AutoCollection_BM__c;
                bab.AutoCollection_Kapal2__c = cri.New_BAB_AutoCollection_Kapal__c;

                insert bab;
                cri.Billing_Account_Bank__c = bab.id;
                ba.PIC__c = rc.id;
                ba.Billing_Account_Bank_Primary__c = bab.id;
                //ba.Status_Approval__c = 'Approved';
                update ba;

                // update Custom File to Billig Account
                List<Custom_File__c> cf = [SELECT id, Billing_Account__c, Account__c FROM Custom_File__c WHERE Request_No__c =: cri.id];
                for (integer i = 0; i < cf.size(); i++) {
                    cf[i].Billing_Account__c = ba.id;
                }
                if (cf.size() > 0) update cf;
            }
            else if (cri.Request_Type__c == 'Extend') {

                // Insert Account NPWP
                Account_NPWP__c an = new Account_NPWP__c();
                an.OwnerId = cri.OwnerId;
                an.NPWP_Address__c = cri.New_Acc_N_NPWP_Address__c;
                an.NPWP_Name__c = cri.New_Acc_N_NPWP_Name__c;
                an.Name = cri.New_Acc_N_NPWP_No__c;
                an.Account__c = cri.Reff_Account__c;

                insert an;
                cri.Account_NPWP__c = an.id;
                cri.Reff_Account_NPWP__c = an.id;

                // Insert Billing Account
                Billing_Account__c ba = new Billing_Account__c();
                ba.OwnerId = cri.OwnerId;
                ba.Billing_Street__c = cri.New_BA_Billing_Street__c;
                ba.Billing_Address__c = cri.New_BA_Billing_Address__c;
                ba.Branch__c = cri.New_BA_Branch__c;
                ba.Name = cri.Reff_Account_Name__c+'-'+cri.New_BA_Branch__c; //cri.New_BA_Name__c;
                ba.No_SIOPSUS__c = cri.New_BA_No_SIOPSUS__c;
                ba.No_SIUPAL__c = cri.New_BA_No_SIUPAL__c;
                ba.NPWP__c = cri.New_BA_NPWP__c;
                ba.Three_Party_Code__c = cri.New_BA_Payment_Type__c;
                ba.SKPT__c = cri.New_BA_SKPT__c;
                ba.SKTD_No__c = cri.New_BA_SKTD_No__c;
                ba.SKTD_Tgl_Berlaku__c = cri.New_BA_SKTD_Tgl_Berlaku__c;
                ba.SKTD_Tgl_Berakhir__c = cri.New_BA_SKTD_Tgl_Berakhir__c;
                ba.SKTD_Tgl_Terbit__c = cri.New_BA_SKTD_Tgl_Terbit__c;
                ba.Tgl_Berlaku_SIOPSUS__c = cri.New_BA_Tgl_Berlaku_SIOPSUS__c;
                ba.Tgl_Terbit_SIOPSUS__c = cri.New_BA_Tgl_Terbit_SIOPSUS__c;
                ba.Tgl_Terbit_SIUPAL__c = cri.New_BA_Tgl_Terbit_SIUPAL__c;
                ba.Named_User__c = cri.Named_User__c;
                ba.Account__c = cri.Reff_Account__c;

                insert ba;
                cri.Billing_Account__c = ba.id;
                cri.Reff_Billing_Account__c = ba.id;

                // Insert Contact

                if (cri.Contact__c == NULL) {                
                    Contact co = new Contact();
                    co.AccountId = cri.Reff_Account__c;
                    co.Address__c = cri.New_C_Address__c;
                    co.Birthdate = cri.New_C_Birthdate__c;
                    co.Email = cri.New_C_Email__c;
                    co.Email_2__c = cri.New_C_Email2__c;
                    co.Fax = cri.New_C_Fax__c;
                    co.FirstName = cri.New_C_First_Name__c;
                    co.Kewarganegaraan__c = cri.New_C_Kewarganegaraan__c;
                    co.LastName = cri.New_C_Last_Name__c;
                    co.Masa_Berlaku_Identitas__c = cri.New_C_Masa_Berlaku_Identitas__c;
                    //co.MiddleName = cri.New_C_Middle_Name__c;
                    co.MobilePhone = cri.New_C_Mobile__c;
                    co.Mobile_2__c = cri.New_C_Mobile_2__c;
                    co.Nomor_Identitas__c = cri.New_C_Nomor_Identitas__c;
                    co.Phone = cri.New_C_Phone__c;
                    co.Phone_2__c = cri.New_C_Phone_2__c;
                    co.Religion2__c= cri.New_C_Religion__c;
                    co.Salutation = cri.New_C_Salutation__c;
                    co.Street__c = cri.New_C_Street__c;
                    //co.Suffix = cri.New_C_Suffix__c;
                    co.Posisi__c = cri.New_C_Title__c;
                    insert co;
                    cri.Contact__c = co.id;
                }
                // Insert Related Contact
                Related_Contact__c rc = new Related_Contact__c();
                if (cri.Contact__c == NULL) rc.Name = cri.New_C_First_Name__c+' '+cri.New_C_Last_Name__c+' - '+cri.New_BA_Branch__c;
                else {
                    Contact co2 = [SELECT id, Name FROM Contact WHERE id =: cri.Contact__c ];
                    rc.Name = co2.Name+' - '+cri.New_BA_Branch__c;
                }
                rc.Account__c = cri.Reff_Account__c;
                rc.Billing_Account__c = cri.Reff_Billing_Account__c;
                rc.Contact__c = cri.Contact__c;
                rc.Status_Customer__c = 'Draft';

                insert rc;
                cri.Related_Contact__c = rc.id;

                // Insert Billing Account Bank
                Billing_Account_Bank__c bab = new Billing_Account_Bank__c();
                bab.Account__c = cri.Reff_Account__c;
                bab.Billing_Account__c = cri.Reff_Billing_Account__c;
                bab.Bank_Name__c = cri.New_BAB_Bank_Name__c;
                bab.Account_Number__c = cri.New_BAB_Account_Number__c;
                bab.AutoCollection__c = cri.New_BAB_Auto_Collection__c;
                bab.AutoCollection_Barang2__c = cri.New_BAB_AutoCollection_Barang__c;
                bab.AutoCollection_BM2__c = cri.New_BAB_AutoCollection_BM__c;
                bab.AutoCollection_Kapal2__c = cri.New_BAB_AutoCollection_Kapal__c;


                insert bab;
                cri.Billing_Account_Bank__c = bab.id;

                ba.PIC__c = rc.id;
                ba.Billing_Account_Bank_Primary__c = bab.id;
                ba.Status_Approval__c = 'Approved';
                update ba;

                // update Custom File to Billig Account
                List<Custom_File__c> cf = [SELECT id, Billing_Account__c, Account__c FROM Custom_File__c WHERE Request_No__c =: cri.id];
                for (integer i = 0; i < cf.size(); i++) {
                    cf[i].Billing_Account__c = ba.id;
                }
                if (cf.size() > 0) update cf;
            }
            else if (cri.Request_Type__c == 'Update') {
                //cri.RecordType__c
                if (cri.RecordTypeName__c == 'Update Account'){
                    Account ac = [SELECT id, Parentid, Website, Name, Company_Type__c, Customer_Group__c, Register_Type__c, Customer_Type__c, Service_Type__c,  Employee_Number__c,
                    Company_Birthday__c, Joining_Since__c, Company_Establishment_Deed__c
                        FROM Account WHERE id =: cri.Reff_Account__c];


                    if (cri.New_Acc_Name__c != NULL) ac.Name = cri.New_Acc_Name__c;
                    if (cri.New_Acc_Parent_Account__c != NULL) ac.ParentId = cri.New_Acc_Parent_Account__c;
                    if (cri.New_Acc_Website__c != NULL) ac.Website = cri.New_Acc_Website__c;
                    if (cri.New_Acc_Company_Type__c != NULL) ac.Company_Type__c = cri.New_Acc_Company_Type__c;
                    if (cri.New_Acc_Customer_Group__c != NULL) ac.Customer_Group__c = cri.New_Acc_Customer_Group__c;
                    if (cri.New_Acc_Register_Type__c != NULL) ac.Register_Type__c = cri.New_Acc_Register_Type__c;
                    if (cri.New_Acc_Customer_Type__c != NULL) ac.Customer_Type__c = cri.New_Acc_Customer_Type__c;
                    if (cri.New_Acc_Service_Type__c != NULL) ac.Service_Type__c = cri.New_Acc_Service_Type__c;
                    if (cri.New_Acc_Employee_Number__c != NULL) ac.Employee_Number__c = cri.New_Acc_Employee_Number__c;

                    if (cri.New_Acc_Company_Birthday__c != NULL) ac.Company_Birthday__c = cri.New_Acc_Company_Birthday__c;
                    if (cri.New_Acc_Joining_Since__c != NULL) ac.Joining_Since__c = cri.New_Acc_Joining_Since__c;
                    if (cri.New_Acc_Company_Establishment_Deed__c != NULL) ac.Company_Establishment_Deed__c = cri.New_Acc_Company_Establishment_Deed__c;

                    update ac;
                } // end Update Account
                else if (cri.RecordTypeName__c == 'Update Billing Account'){
                    Billing_Account__c ba = [SELECT id, Branch__c, Billing_Address__c, NPWP__c, Three_Party_Code__c, Primary__c,  
                    Billing_Street__c, Billing_City__c, Billing_Kecamatan__c, Billing_Province__c, Billing_Village__c, Billing_Zip_Postal__c,
                    SKTD_No__c, SKTD_Tgl_Berlaku__c, SKTD_Tgl_Terbit__c, SKTD_Tgl_Berakhir__c, No_SIUPAL__c, TGL_TERBIT_SIUPAL__c, TGL_BERLAKU_SIUPAL__c, 
                    NO_SIOPSUS__c, TGL_TERBIT_SIOPSUS__c, TGL_BERLAKU_SIOPSUS__c, SKPT__c

                        FROM Billing_Account__c WHERE id =: cri.Reff_Billing_Account__c];

                    if (cri.New_BA_Branch__c != NULL) ba.Branch__c = cri.New_BA_Branch__c;
                    if (cri.New_BA_Billing_Address__c != NULL) ba.Billing_Address__c = cri.New_BA_Billing_Address__c;
                    if (cri.New_BA_NPWP__c != NULL) ba.NPWP__c = cri.New_BA_NPWP__c;
                    if (cri.New_BA_Payment_Type__c != NULL) ba.Three_Party_Code__c = cri.New_BA_Payment_Type__c;
                    if (cri.New_BA_Primary__c != NULL) {
                        if (cri.New_BA_Primary__c == 'YES') ba.Primary__c = TRUE;
                        else if (cri.New_BA_Primary__c == 'No') ba.Primary__c = FALSE;
                    }
                    if (cri.New_BA_Billing_Street__c != NULL) ba.Billing_Street__c = cri.New_BA_Billing_Street__c;

                    if (cri.New_BA_SKTD_No__c != NULL) ba.SKTD_No__c = cri.New_BA_SKTD_No__c;
                    if (cri.New_BA_SKTD_Tgl_Berlaku__c != NULL) ba.SKTD_Tgl_Berlaku__c = cri.New_BA_SKTD_Tgl_Berlaku__c;
                    if (cri.New_BA_SKTD_Tgl_Terbit__c != NULL) ba.SKTD_Tgl_Terbit__c = cri.New_BA_SKTD_Tgl_Terbit__c;
                    if (cri.New_BA_SKTD_Tgl_Berakhir__c != NULL) ba.SKTD_Tgl_Berakhir__c = cri.New_BA_SKTD_Tgl_Berakhir__c;

                    if (cri.New_BA_No_SIUPAL__c != NULL) ba.No_SIUPAL__c = cri.New_BA_No_SIUPAL__c;
                    if (cri.New_BA_Tgl_Terbit_SIUPAL__c != NULL) ba.TGL_TERBIT_SIUPAL__c = cri.New_BA_Tgl_Terbit_SIUPAL__c;
                    if (cri.New_BA_TGL_BERLAKU_SIUPAL__c != NULL) ba.TGL_BERLAKU_SIUPAL__c = cri.New_BA_TGL_BERLAKU_SIUPAL__c;
                    if (cri.New_BA_No_SIOPSUS__c != NULL) ba.NO_SIOPSUS__c = cri.New_BA_No_SIOPSUS__c;
                    if (cri.New_BA_Tgl_Berlaku_SIOPSUS__c != NULL) ba.TGL_TERBIT_SIOPSUS__c = cri.New_BA_Tgl_Berlaku_SIOPSUS__c;
                    if (cri.New_BA_Tgl_Terbit_SIOPSUS__c != NULL) ba.TGL_BERLAKU_SIOPSUS__c = cri.New_BA_Tgl_Terbit_SIOPSUS__c;

                    if (cri.New_BA_SKPT__c != NULL) ba.SKPT__c = cri.New_BA_SKPT__c;

                    /** add hendrik alias rival **/
                    ba.Billing_Address__c = cri.New_BA_Billing_Address__c;
                    update ba;
                    /** end **/
                }
            }
        } // end Status == Approved
    }
} // end is Before
} // end trigger

This is the test class I made

@isTest
public class CR_Approved_Test {

static testMethod void myTest() {
Test.startTest(); 


    Named_User__c nu = new Named_User__c();
    nu.name = 'saskeh';
    nu.User__c = UserInfo.getUserId();
    insert nu;

    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 

    User us = new User();
    us.Alias = 'standt';
    us.Email ='arieftestclass@test.com'; 
    us.EmailEncodingKey ='UTF-8';
    us.LastName ='Testing';
    us.LanguageLocaleKey ='en_US';
    us.LocaleSidKey ='en_US';
    us.ProfileId = p.Id; 
    us.TimeZoneSidKey ='America/Los_Angeles';
    us.UserName ='arieftestclass@test.com';
    insert us;

    Master_Address__c ma = new Master_Address__c();
        ma.Name = 'test';
        insert ma;

    Master_Data__c md = new Master_Data__c();
        md.Name = 'bank test';
        md.CONTEXT_TYPE__c = 'BANK';
        insert md;

    String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Change_Request_Item__c' and Name = 'New Account'].Id;

    Change_Request_Item__c cri = new Change_Request_Item__c();
        cri.RecordTypeId = strRecordTypeId;
        cri.Named_User__c = nu.Id;
        cri.New_Acc_Name__c = 'Naruto123321';
        cri.New_Acc_Company_Type__c = 'PT';
        cri.New_Acc_Customer_Group__c = 'BUMN';
        cri.New_Acc_Register_Type__c = 'Mitra';
        cri.New_Acc_Service_Type__c = 'Kapal';
        cri.New_Acc_Joining_Since__c = date.today();
        cri.New_Acc_Company_Establishment_Deed__c = date.today();
        cri.New_BA_Branch__c = 'Tanjung Priok';
        cri.New_BA_Billing_Street__c = 'Jl. Konoha';
        cri.New_BA_Billing_Address__c = ma.Id;
        cri.Billing_Email__c = 'sasukevsnaruto@konoha.com';
        cri.New_BA_Primary__c = 'Yes';
        cri.New_Acc_N_NPWP_No__c = '12.123.123.1-123.123';
        cri.New_Acc_N_NPWP_Name__c = 'rasengan';
        cri.New_Acc_N_NPWP_Address__c = 'konoha';
        cri.New_Acc_Company_Birthday__c = date.today()-10000;
        cri.New_C_Last_Name__c = 'Uchiha';
        cri.New_BAB_Account_Number__c = '1213019';
        cri.New_BAB_Bank_Name__c = md.Id;
        cri.New_BAB_AutoCollection_Barang__c = false;
        cri.New_BAB_Autocollection_Kapal__c = true;
        cri.New_BAB_AutoCollection_BM__c = false;
        cri.OwnerId = us.id;
        insert cri;

    Change_Request_Item__c updatecr = [SELECT Id, Status__c FROM Change_Request_Item__c WHERE Id = :cri.Id];
        updatecr.Status__c = 'Approved';
        update updatecr;

     Account ac = new Account();
                ac.Ownerid = cri.OwnerId;
                ac.Company_Birthday__c = cri.New_Acc_Company_Birthday__c;
                ac.Company_Establishment_Deed__c = cri.New_Acc_Company_Establishment_Deed__c;
                ac.Company_Type__c = cri.New_Acc_Company_Type__c;
                ac.Customer_Group__c = cri.New_Acc_Customer_Group__c;
                ac.Customer_Type__c = cri.New_Acc_Customer_Type__c;
                ac.Employee_Number__c = cri.New_Acc_Employee_Number__c;
                ac.Joining_Since__c = cri.New_Acc_Joining_Since__c;
                ac.Name = cri.New_Acc_Name__c;
                ac.Parentid = cri.New_Acc_Parent_Account__c;
                ac.Register_Type__c = cri.New_Acc_Register_Type__c;
                ac.Service_Type__c = cri.New_Acc_Service_Type__c;
                ac.VIP_Type__c = cri.New_Acc_VIP_Type__c;
                ac.Website = cri.New_Acc_Website__c;
                insert ac;

     Contact co = new Contact();   
                co.AccountId = ac.id;
                co.Address__c = cri.New_C_Address__c;
                co.Birthdate = cri.New_C_Birthdate__c;
                co.Email = cri.New_C_Email__c;
                co.Email_2__c = cri.New_C_Email2__c;
                co.Fax = cri.New_C_Fax__c;
                co.FirstName = cri.New_C_First_Name__c;
                co.LastName = cri.New_C_Last_Name__c;
                co.Kewarganegaraan__c = cri.New_C_Kewarganegaraan__c;
                co.LastName = cri.New_C_Last_Name__c;
                co.Masa_Berlaku_Identitas__c = cri.New_C_Masa_Berlaku_Identitas__c;
                //co.MiddleName = cri.New_C_Middle_Name__c;
                co.MobilePhone = cri.New_C_Mobile__c;
                co.Mobile_2__c = cri.New_C_Mobile_2__c;
                co.Nomor_Identitas__c = cri.New_C_Nomor_Identitas__c;
                co.Phone = cri.New_C_Phone__c;
                co.Phone_2__c = cri.New_C_Phone_2__c;
                co.Religion2__c= cri.New_C_Religion__c;
                co.Salutation = cri.New_C_Salutation__c;
                co.Street__c = cri.New_C_Street__c;
                //co.Suffix = cri.New_C_Suffix__c;
                co.Posisi__c = cri.New_C_Title__c;
                insert co;

    Account_NPWP__c an = new Account_NPWP__c();
                an.Ownerid = cri.OwnerId;
                an.NPWP_Address__c = cri.New_Acc_N_NPWP_Address__c;
                an.NPWP_Name__c = cri.New_Acc_N_NPWP_Name__c;
                an.Name = cri.New_Acc_N_NPWP_No__c;
                an.Account__c = ac.id;
                insert an;

    Billing_Account__c ba = new Billing_Account__c();
                ba.Ownerid = cri.OwnerId;
                ba.Billing_Street__c = cri.New_BA_Billing_Street__c;
                ba.Billing_Address__c = cri.New_BA_Billing_Address__c;
                ba.Branch__c = cri.New_BA_Branch__c;
                ba.Name = ac.Name+'-'+cri.New_BA_Branch__c; 
                ba.No_SIOPSUS__c = cri.New_BA_No_SIOPSUS__c;
                ba.No_SIUPAL__c = cri.New_BA_No_SIUPAL__c;
                ba.NPWP__c = an.id; 
                ba.Three_Party_Code__c = cri.New_BA_Payment_Type__c;
                ba.SKPT__c = cri.New_BA_SKPT__c;
                ba.SKTD_No__c = cri.New_BA_SKTD_No__c;
                ba.SKTD_Tgl_Berlaku__c = cri.New_BA_SKTD_Tgl_Berlaku__c;
                ba.SKTD_Tgl_Berakhir__c = cri.New_BA_SKTD_Tgl_Berakhir__c;
                ba.SKTD_Tgl_Terbit__c = cri.New_BA_SKTD_Tgl_Terbit__c;
                ba.Tgl_Berlaku_SIOPSUS__c = cri.New_BA_Tgl_Berlaku_SIOPSUS__c;
                ba.Tgl_Terbit_SIOPSUS__c = cri.New_BA_Tgl_Terbit_SIOPSUS__c;
                ba.Tgl_Terbit_SIUPAL__c = cri.New_BA_Tgl_Terbit_SIUPAL__c;
                ba.Named_User__c = cri.Named_User__c;
                ba.Account__c = ac.id;
                if (cri.New_BA_Primary__c == 'Yes') ba.Primary__c = TRUE;
                insert ba;

    Related_Contact__c rc = new Related_Contact__c();
                //rc.Name = cri.New_C_First_Name__c+' '+cri.New_C_Last_Name__c+' - '+cri.New_BA_Branch__c;
                rc.Ownerid = cri.OwnerId;
                rc.Account__c = ac.id;
                rc.Billing_Account__c = ba.id;
                rc.Contact__c = co.id;
                rc.Status_Customer__c = 'Draft';
                insert rc;

    Billing_Account_Bank__c bab = new Billing_Account_Bank__c();
                bab.Account__c = ac.id;
                bab.Billing_Account__c = ba.id;
                bab.Bank_Name__c = cri.New_BAB_Bank_Name__c;
                bab.Account_Number__c = cri.New_BAB_Account_Number__c;
                bab.AutoCollection__c = cri.New_BAB_Auto_Collection__c;
                bab.AutoCollection_Barang2__c = cri.New_BAB_AutoCollection_Barang__c;
                bab.AutoCollection_BM2__c = cri.New_BAB_AutoCollection_BM__c;
                bab.AutoCollection_Kapal2__c = cri.New_BAB_AutoCollection_Kapal__c;

                insert bab;


Test.stopTest(); 
}
}

But everytime I run the test class, there`s an error

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Account Name is Duplicate: []

Class.CR_Approved_Test.myTest: line 85, column 1

and if I remove the insert on account line

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: duplicates value on record with id: : []

Class.CR_Approved_Test.myTest: line 118, column 1

if I remove the insert acc; and insert an; .. no error.. nothing happen also on my trigger

0

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Account Name is Duplicate: []

Class.CR_Approved_Test.myTest: line 85, column 1

For this exception, I think you set up Account Name field is unique. Therefore, when you run trigger, the related account is created. After that, you continue to create another account with the same Account Name. This leads to exception. Please check you configuration again.

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: duplicates value on record with id: : []

Class.CR_Approved_Test.myTest: line 118, column 1

About this, I think the same. Beside that, the LastName field of Contact is duplicated when you set value in your test class.

However, I don't understand why you have to create again related records when you did it in trigger.

One more thing, I think you need to refactor your trigger code. Because Salesforce has the governor limits, when you use DML statements inside loop, it is easy to hit the governor limits. Please refer this

0

I would suggest performing a check to ensure that the account does not already exist before trying to perform an insert. Would also suggest if it does exist to set the account appropriately. This is illustrated in the below Pseudocode...

    trigger CR_Approved on Change_Request_Item__c ( before update ) {

        Map<Id, Account> accountMap = new Map<Id, SObject>();

        Set<String> accNames = new Set<String>();

        if( Trigger.isBefore ) {

            for( Change_Request_Item__c item : Trigger.new ){
                String accName = item.New_Acc_Name__c;

                if( !String.isBlank( accName ) 
                    && item.Status__c == 'Approved' 
                    && Trigger.oldMap.get( item.Id ).Status__c != 'Approved'
                    && !accNames.contains( accName )){
                    accNames.add( accName );
                }
            }

            // get any existing accounts matching accounts in your Trigger Context
            List<Account> accs = [ SELECT Id, Name FROM Account WHERE Name IN: accNames ];

            if( !accs.isEmpty() ){

                for( Account acc : accs ){
                    accountMap.put( acc.Name, acc );
                }
            }


            for( Change_Request_Item__c item : trigger.New ){

                if( item.Status__c == 'Approved' && Trigger.oldMap.get( item.Id ).Status__c != 'Approved' ){

                    // set your account
                    Account acc = ( accountMap.containsKey( item.New_Acc_Name__c ) ? accountMap.get( acc.Name ) : new Account() );

                    if( item.Request_Type__c == 'New' ){


                        ...
                        upsert acc; // upsert the account 

                        ... do some other stuff you need to do here 

                    }
                    else if( item.Request_Type__c == 'Extended' ){
                        ...
                        upsert acc; // upsert the account 

                        ... do some other stuff you need to do here 
                    }
                    else if( item.Request_Type__c == 'Update' ){
                        ...
                        upsert acc; // upsert the account 

                        ... do some other stuff you need to do here 
                    }
                    ....


                }
            }
        }
    }

The above trigger code would gather the names of accounts in the trigger if the status has been changed to "Approved". Using that set of names you can collect the existing accounts and set your account as new / to the account that needs to be updated. This would avoid account name duplication. Another that you can also use here is to add an error to the account in the event that this is meant to be a unique Account name and your user has entered a duplicate value onto the inbound Change_Request_Item__c. This is illustrated in the below Pseudocode.

    trigger CR_Approved on Change_Request_Item__c ( before update ) {

        Map<Id, Account> accountMap = new Map<Id, SObject>();

        Set<String> accNames = new Set<String>();

        if( Trigger.isBefore ) {

            for( Change_Request_Item__c item : Trigger.new ){
                String accName = item.New_Acc_Name__c;

                if( !String.isBlank( accName ) 
                    && item.Status__c == 'Approved' 
                    && Trigger.oldMap.get( item.Id ).Status__c != 'Approved'
                    && !accNames.contains( accName )){
                    accNames.add( accName );
                }
            }

            // get any existing accounts matching accounts in your Trigger Context
            List<Account> accs = [ SELECT Id, Name FROM Account WHERE Name IN: accNames ];

            if( !accs.isEmpty() ){

                for( Account acc : accs ){
                    accountMap.put( acc.Name, acc );
                }
            }


            for( Change_Request_Item__c item : trigger.New ){

                if( item.Status__c == 'Approved' && Trigger.oldMap.get( item.Id ).Status__c != 'Approved' ){

                    // set your account
                    Account acc = ( accountMap.containsKey( item.New_Acc_Name__c ) ? accountMap.get( acc.Name ) : new Account() );

                    if( item.Request_Type__c == 'New' ){

                        // perform a check for duplicates and add an error to the object and move to the next object in the trigger
                        if( accountMap.containsKey( item.New_Acc_Name__c )){
                            item.addError( 'Duplicate Account name exists. This record cannot be created.' );

                            continue;
                        }

                        ...
                        insert acc; // insert the account 

                        ... do some other stuff you need to do here 

                    }
                    else if( item.Request_Type__c == 'Extended' ){

                        // perform a check for duplicates and add an error to the object and move to the next object in the trigger
                        if( accountMap.containsKey( item.New_Acc_Name__c )){
                            item.addError( 'Duplicate Account name exists. This record cannot be created.' );

                            continue;
                        }
                        ...
                        insert acc; // insert the account 

                        ... do some other stuff you need to do here 
                    }
                    else if( item.Request_Type__c == 'Update' ){
                        ...
                        upsert acc; // upsert the account 

                        ... do some other stuff you need to do here 
                    }
                    ....


                }
            }
        }
    }

Managing what you have already that would potentially be matching inbound data would be helpful in heading off these issues. Now your test class should be able to perform what is needed. Be sure to take into account your Query limits as well when doing this level of work. If you have more than 60 records here you likely will end up hitting limits. I would suggest as much as possible setting up your code to handle this efficiently. However in the way that you're connecting accounts and contacts, adding your items to a map / list to insert at the end of trigger functions may become problematic long term.

0

In the line no 41 use 'Naruto123321'+String.valueOf(Date.Today()); for New_Acc_Name__c field.

And what error are you getting at Line 118 means which field is duplicate.

Also, you are making the DML inside the For Loop which is not a best practice. You need to refract your code.

See this link Apex Code Best Practices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.