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
CRM-plusCRM-plus 

Update Account data from event (@isTest)

Hello,

 

I am trying to write the test class for my trigger. But I can't get further then 60%. 

Can someone help me with this?

 

trigger updateAcct onEvent (afterupdate) {

   

Set<Id> acctIds = new Set<Id>();    

for(Event e: [SELECT Subject,WhatId, Sales_Reps__c FROM EVENT WHERE ID IN:trigger.new]){

String wId = e.WhatId;

if(wId!=null && wId.startsWith('001') && !acctIds.contains(e.WhatId) && e.Sales_Reps__c == '1-5'){

acctIds.add(e.WhatId);   

}     

}

    for(Account a:[SELECT Sales_Reps__c FROM Account WHERE ID IN:acctIds]){

                 a.Sales_Reps__c = '1-5';

        update a;  

    }        

}    

 

Test class:

public class TestClass {

 

    static testMethod void myUnitTest() {

        //Create new Account

        Account acc = new Account(Name ='test');

            Set<Id> acctIds = new Set<Id>();

            insert acc;

 

        // Create new Event with fiction data

        Event c = new Event(WhatId=acc.ID, Subject='test',Sales_Reps__c='1-5',durationinminutes = 1440,isalldayevent = true,activitydate = System.today());

        Set<Id> ownerIds = new Set<Id>();

        String wId = c.WhatId;

            if(c.Update_Event__c ==true){

    acctIds.add(c.WhatId);

}

insert c;

        Test.startTest();

        try {

        Account a = [SELECT Name, Sales_Reps__c,Sales_LY__c, Description FROM Account WHERE ID =:acc.Id];

        a.Name='test';

        a.Sales_Reps__c ='1-5';

        a.Sales_LY__c = '1.750.000 - 2.000.000';

        a.Description='test';

        update a;

}

 

 catch (System.DmlException e) {

    System.debug('Event record could not be updated.');

    

    }

        Test.stopTest();   

    }

}

 

Looking forward to the answer!

 

Kind regards,

Avidev9Avidev9
Well the trigger is on Event, I guess instead of updating Account, you hsould be updating the Event record.
CoolSurenCoolSuren

Hi,

 

With in your test method just insert one Account and one Event record. and pass that Account id into the Event insert operation.

 

for eg:

 

Account a = new Account(Name = 'Test');

insert a;

 

Event e = new Event(WhatId=a.ID, Subject='test', Sales_Reps__c='1-5', durationinminutes = 1440, isalldayevent = true,              activitydate = System.today());

insert a;

 

the event records need to satiesfty your IF condition in the trigger.

CRM-plusCRM-plus

Thanks for your help but it I still stuck on 60%. 

Can someone help me?