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
MattwestMattwest 

Need help on a Trigger to populate lookup fields

Hello All,

I am new to Apex and I am trying to build a trigger that will pull field values from a single record in a custom object called system_policy__c. There is only one record for that object and it is intended to set default values for new records created. 

 

So basically I'm trying to pull a lookup field value from system_policy__c.field1__c to sample_object__c.field1__c

 


I'm trying to populate these fields (before insert,before update)

 

I am just learning, and any help will be much appreciated!!! Thanks in advance for any help you may be able to give!

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

Below is code snippet which may help you:-

 

trigger SampleObjectTrigger on sample_object__c(before insert, before update){
	system_policy__c systemPolicy = [Select Field1__c from System_Policy__c limit 1];
	for(sample_object__c sampleObject : trigger.new){
		sampleObject.Field1__c = systemPolicy.Field1__c
	}
}

 Please let me know if you have any question around this.

 

Happy to help you!

 

 

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Below is code snippet which may help you:-

 

trigger SampleObjectTrigger on sample_object__c(before insert, before update){
	system_policy__c systemPolicy = [Select Field1__c from System_Policy__c limit 1];
	for(sample_object__c sampleObject : trigger.new){
		sampleObject.Field1__c = systemPolicy.Field1__c
	}
}

 Please let me know if you have any question around this.

 

Happy to help you!

 

 

This was selected as the best answer
MattwestMattwest

Thank you! I'll try it shortly and let you know how it works!

MattwestMattwest

Worked Perfectly, Thank you... You've helped me learn a lot!

digamber.prasaddigamber.prasad

Happy that it worked for you! :)

 

Could you please be kind enough to give me KUDOS for this, if it helped you.