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
yash mehta 12yash mehta 12 

Can anyone help to write test class ?

trigger majorIdonApplication on Application__c (before insert,before update) {
    Set<String>majorName = new Set<string>();
    Set<Id>majorNameset = new Set<Id>();
    for(Application__c application : Trigger.new){
        if(application.Intended_Major__c !=null){
            majorName.add(application.Intended_Major__c);
        }
    }
    for(Major__c major : [SELECT id,Name FROM Major__c WHERE name In : majorName]){
        majorNameset.add(major.Id);
    }
    for(Application__c app : Trigger.new){
        for(Id Child : majorNameset){
            app.Major_Interest__c = Child;
        }
    }
}
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Yash Mehta,
  • Hope it will be helpful.
Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar

 
PawanKumarPawanKumar
Hi Yes,
Please try below code and let me know if it helps you.


@isTest
private class majorIdonApplicationTest{

static testMethod void testTriggerBeforeInsertforApp(){
Major__c majorObject = new Major__c();
majorObject.Name='Test Major';
insert majorObject;

Application__c applicationObj = new Application__c();
applicationObj.Intended_Major__c=majorObject.Name;
// the insert call : will test before insert
insert applicationObj;

Major__c majorObjectForUpdate = new Major__c();
majorObjectForUpdate.Name='Test Major1';
insert majorObjectForUpdate;

Application__c insertedApp = [Select Id,Intended_Major__c from Application__c Limit 1];
insertedApp.Intended_Major__c=majorObjectForUpdate.Name;
// the insert call : will test before update
update insertedApp;

}
}

Regards,
Pawan Kumar
PawanKumarPawanKumar
please let me know if it works for you.