+ Start a Discussion
chrishillchrishill 

Trigger to update field across object.

Hi all,

 

I'm working for a non-profit and new to apex. I'm having a hard time thinking through how to do a SOQL query and update fields inside a trigger.

 

I have a custom object Application__c which is related to a Contact (field name on application Contact__c) and has a couple fields I want to update from related fields on the Contact. Both fields are lookup relationships so I cannot use workflow.

 

the two fields are: on application Specific_Organization__c = on Contact Specific_Organization__c

                                 on application Specific_Visit__c = on Contact Specific Visit__c

 

and this needs to happen if a field on the application Status__c = "Applicant" and one on the contact Alum__c = False.

 

Any help to get me started in the right direction would be appreciated.

 

Thanks!

Chris

 

 

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof

Hey Chris,

 

Since you're non-profit, I'll write you the whole trigger! :)

trigger ApplicationBeforeTrigger on Application__c (before insert, before update) {

	Set<ID> contactIds = new Set<ID>();
	for ( Application app : trigger.new ) {
		if ( app.Contact__c != null && app.Status__c == 'Applicant' ) {
			contactIds.add(app.Contact__c);
		}
	}

	Map<ID, Contact> contactsMap =
		new Map<ID, Contact>(
			[SELECT Specific_Organization__c, Specific_Visit__c 
			FROM Contact
			WHERE Id in :contactIds AND Alum__c = false]);

	for ( Application app : trigger.new ) {
		if ( app.Contact__c != null && app.Status__c == 'Applicant' ) {
			Contact cont = contactsMap.get(app.Contact__c);
			if ( cont != null ) {
				app.Specific_Organization__c = cont.Specific_Organization__c;
				app.Specific_Visit__c = cont.Specific Visit__c;
			}
		}
	}
}

 

The trigger is not compiled/tested (b/c I don't have your data model), so you'll surely see a few syntax/logical errors, but minor ones.  The trigger is bulk-safe.

 

I hope this helps.

 

Thanks.

All Answers

soofsoof

Hey Chris,

 

Since you're non-profit, I'll write you the whole trigger! :)

trigger ApplicationBeforeTrigger on Application__c (before insert, before update) {

	Set<ID> contactIds = new Set<ID>();
	for ( Application app : trigger.new ) {
		if ( app.Contact__c != null && app.Status__c == 'Applicant' ) {
			contactIds.add(app.Contact__c);
		}
	}

	Map<ID, Contact> contactsMap =
		new Map<ID, Contact>(
			[SELECT Specific_Organization__c, Specific_Visit__c 
			FROM Contact
			WHERE Id in :contactIds AND Alum__c = false]);

	for ( Application app : trigger.new ) {
		if ( app.Contact__c != null && app.Status__c == 'Applicant' ) {
			Contact cont = contactsMap.get(app.Contact__c);
			if ( cont != null ) {
				app.Specific_Organization__c = cont.Specific_Organization__c;
				app.Specific_Visit__c = cont.Specific Visit__c;
			}
		}
	}
}

 

The trigger is not compiled/tested (b/c I don't have your data model), so you'll surely see a few syntax/logical errors, but minor ones.  The trigger is bulk-safe.

 

I hope this helps.

 

Thanks.

This was selected as the best answer
chrishillchrishill

Thanks a lot! That's exactly what I needed.