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
OcoBriOcoBri 

Need help with first trigger

Hello,

I am new to coding on the force platform.  I am using the Enterprise Edition with Non-Profit Starter Pack.

I've written a trigger in my Sandbox:

 

 

 

trigger MakeParentsActiveTrigger on Contact (after insert, after update) {

//Get list of Consumer IDs that the trigger is acting upon
List<ID> consumerids = New List<ID>();
List<ID> ParentIds = New List<ID>();
for(Contact o : Trigger.new) {

//Add Parents Ids to a list if the contact is an active consumer
if(o.QSAC_active__c == TRUE && o.RecordTypeID == '012A000000179ETIAY') {
ParentIds.add(o.Primary_Parent_Guardian__c);
ParentIds.add(o.Secondary_Parent_Guardian__c);
}
}

//Retrieve Parents Active status
List<Contact> ParentInfo = [SELECT id, QSAC_active__c FROM Contact WHERE id in :ParentIds];

//Update the Parents Active status
for(integer i = 0 ; i < ParentInfo.size(); i++){
ParentInfo[i].QSAC_active__c = FALSE;
}
update ParentInfo;
}

 

 

But I have no idea how to tell if it is working, or if it is ever called.  Furthermore, I'm not sure if I need a class when it seems the trigger should be able to stand alone.  I'm also confused about "reaching 75%" of something before I can copy it into my production database.  Any help would be appreciated.

 

sfdcfoxsfdcfox

You do need a class. Why? For the 75% you heard about. What they're referring to is "code coverage", which is achieved by one or more "test methods", which empirically "prove" that your trigger is indeed firing in the manner which you expect.

 

You have a few small problems with your code, though:

 

trigger MakeParentsActiveTrigger on Contact (after insert, after update) {
	Id compareRecordTypeId = [SELECT Id FROM RecordType WHERE SObjectType = 'Contact' AND DeveloperName='Consumer'].Id; // Change DeveloperName, if necessary.

	//Get list of Consumer IDs that the trigger is acting upon
	Set<ID> ParentIds = New Set<ID>();

	for(Contact o : Trigger.new) {
		//Add Parents Ids to a list if the contact is an active consumer
		if(o.QSAC_active__c && o.RecordTypeID == compareRecordTypeId) {	// checkbox = boolean = no need to compare to true; also, do not hardcode ids
			ParentIds.add(o.Primary_Parent_Guardian__c);
			ParentIds.add(o.Secondary_Parent_Guardian__c);
		}
	}

	// Just in case a null value slipped in, remove it here.
	parentIds.remove(null);

	Contact[] contacts = new Contact[0];
	for(Id parentId:parentIds) {
		contacts.add(new contact(id=parentid,qsac_active__c=false)); // Since I know the IDs, I can skip a query here, for efficiency.
			// All other fields will be left alone.
	}
	update contacts;
}

Now, given that, your test method just has to:

 

1) Insert two new contacts, a mother and a father (or whatever), set the flags to true.

2) Insert one new contact, with the specified mother/father, and of the correct record type.

3) Query back the two contacts, and make sure the flag is set to false.