+ Start a Discussion

Test code with Validation Rule issues

In my Validation Rule I am referring to an Opportunity field. The Opportunity is a grandparent to the record. My Validation Rule looks like this :

ISPICKVAL( Parent__r.Opportunity__r.Product__c,"")


The Validation Rule works fine. But it is still kicking off when I run my test code which is populating that field :

private class testProjectInitiationTrigger {

    static testMethod void ProjectInitiationTriggerTest() {

        Account account = TestHelper.createAccount();
        Opportunity objOpp = TestHelper.createOpp(account);
        objOpp.Manager__c = objOpp.CreatedById;
        objOpp.Product__c = 'test';
        update objOpp;

        //this is the parent record being created
        Project__c objC = TestHelper.createCBC(objOpp);
        objC.Supplier__c = 'Test Supplier';
        update objC;

        //this is the child record which the trigger will run off
        Initiation__c oPI = new Initiation__c();
        oPI.Name =  objC.Name;
opI.Employee__c = opI.CreatedById; oPI.Parent__c = objC.Id; insert oPI; //this is the child record field which will initiate the post update trigger oPI.Initiate_Implementation__c = true; update oPI; } }

Why is the Validation Rule for the Opptunity Product__c field still kicking off when I run this test code ?

Thank you for any help.

You are setting product on opportunity.But your validation  is checing on parent opportunity...."field name Parent__r.Opportunity__r.Product__c".You have to set this field.You have to create one more opportunity.

Also add a validation a rule on validation to fire only if parent__r in not null.Also use Text() while dealing with picklist.
objOpp.Product__c = 'test';

Thank you - one more question : Should this line work :


objOpp.Manager__c = objOpp.CreatedById;


One of the validation rules is for the Manager__c field and I am still getting it. Should 'objOpp.CreatedById' work ?


If not, what can I use for that ?


Thank you very much


Can you please post the error message....


Thank you very much. I have now gotten the code to refer to the proper field in the Opp but I am still getting an error which is telling me I am using an incorrect form of data. This is the line : 

objOpp.Manager__c = objOpp.CreatedById;

For some reason it is not accepting objOpp.CreatedById in the code as a value. I am using that type of method later in the code for fields in the other objects involved such as this :

oPI.Business_Owner__c = objC.CreatedById;

 And that works. But for some reason it is not accepting objOpp.Manager__c = CreatedById in this code. 

Does anybody know how to populate that field to satisfy the validation rule ? 


Thank you very much for your help.



objOpp.Manager__c = objOpp.CreatedById;


Manager__c :  Seems like you have a manager object in salesforce .Check the field, is this field parent is user?

You error message says ,its parent is different than user....

For this you have to do:


Manager__c mang=new Manager__();

//Set all the required field


insert mang;