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
Arun KArun K 

help in Trigger testmethod

trigger Trigger_ValidateGrandTotal on Account (before update) {
public decimal GrandtotalLimit=0;
Admin__c adminvar = [select Grand_Total_Limit__c from Admin__c where Name='CallPlan' and ownerId= :UserInfo.getUserId()  ];
GrandtotalLimit=adminvar.Grand_Total_Limit__c;

     for( Account acc:trigger.new)
     {
    
              if(acc.Grand_Total__c > GrandtotalLimit)
              {
                  
                  acc.addError('You have exceeded the limit of '+GrandtotalLimit+' for Account '+acc.name+'. Please review and modify the allocation');
                 
                  break;
                    
              }                
     }

asish1989asish1989

HI

This is the code, You can refer, You need to change your trigger little bit.

trigger Trigger_ValidateGrandTotal on Account (before update) {
    public decimal GrandtotalLimit=0;
    List<Admin__c > adminvar = [select Name,Grand_Total_Limit__c from Admin__c  where Name='CallPlan' and ownerId= :UserInfo.getUserId()];
    if(adminvar !=null && adminvar.size()>0){
        GrandtotalLimit = adminvar.get(0).Grand_Total_Limit__c;
        for( Account acc:trigger.new){
             if(acc.Grand_Total__c > GrandtotalLimit){
                  acc.addError('You have exceeded the limit of '+GrandtotalLimit+' for Account '+acc.name+'. Please review and modify the allocation');
                  break;
                    
              }                
         }
    }     
 }

 

@isTest(SeeAllData = true)
private class AccountTriggerTestClass {
    static testMethod void validateHelloWorld() {
        Admin__c b = new Admin__c(Name='Behind the Cloud', Grand_Total_Limit__c = 12.00);
        insert b;
        User userId = [SELECT id FROM USER WHERE id =:UserInfo.getUserId()];
        b.OwnerId = userId.id;
        upsert b;
        Account act = new Account(name = 'testaccount',Grand_Total__c = 7.00);
        insert act;
        act.Grand_Total__c = 20.00;
        upsert act;
    }  
}     

 If this post answers your questions, please mark it as solved and give kudos if this helps you

 

  Thanks

Arun KArun K

thanks for the reply.

But I am getting an error that 

"

Error: Compile Error: Field is not writeable: Account.AxtriaCallPlan__Grand_Total__c at line 9 column 89

that is in this line--

Account act = new Account(name = 'testaccount',Grand_Total__c = 7.00);

how to make it pass..

 

"

Arun KArun K
As it is a formula field
asish1989asish1989

HI

Formula fields are always readonly , so we will never add some value to that field.

So change this line

     Account act = new Account(name = 'testaccount',Grand_Total__c = 7.00);

      to

    Account act = new Account(name = 'testaccount');

    insert act;

 

 If this post answers your questions, please mark it as solved and give kudos if this helps you

 

  Thanks

 

itsawsitsaws
Hi Kada,

Don't use Grand_Total__c while creating account as its a formula field it will automatically gets populated..Just put name and other mandatory fields to create an account. in testclass.

Reagrds,
Itsaws
Arun KArun K

thanks for the reply.

 

I removed Grand_total field and try to run the testclass .

 

the testclass has failed and I couldnt get the required code coverage.

Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AxtriaCallPlan__Account__c]: [AxtriaCallPlan__Account__c] Stack Trace Class.AxtriaCallPlan.AccountTriggerTestClass.validateHelloWorld: line 10, column 1

 

What is the thing that I am missing.

 

 

 

asish1989asish1989

check which object contains [AxtriaCallPlan__Account__c field and about field type ,now assign some value to that field

then run the test.