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
simon chaosimon chao 

before insert trigger help

I am wondering how I can come about this trigger.

I want to write a before insert trigger that fires whenever a new benefit record is created. It will take in value from a look up object, company__c, 
and prepopulate its custom fields medical_location__c, date_of_subscription__c and medical_description__c field and stamp it on the benefit form. Any help on how I can go about this?
Best Answer chosen by simon chao
Raj VakatiRaj Vakati
Try this
 
trigger MyTrigger on Benifit__c (before insert) 
{
    Set<Id> lookupIds = new Set<Id>();
    for (Benifit__c b : Trigger.new) 
	{
        if (b.company__c != null && b.medical_location__c!=null && b.date_of_subscription__c!=null && b.medical_description__c!=null) 
		{
            lookupIds.add(b.company__c);
        }
    }

    Map<Id, company__c> compRec = new Map<Id, company__c>([
            select Id, medical_location__c,date_of_subscription__c,medical_description__c
            from company__c
            where Id in :lookupIds
            ]);
    for (Benifit__c b : Trigger.new) 
	{
        if (b.company__c != null) 
		{
            company__c c = compRec.get(b.company__c);
			b.medical_location__c = c.medical_location__c;
            b.date_of_subscription__c = r.date_of_subscription__c;
            b.medical_description__c = r.medical_description__c; 
			
        }
    }
}

 

All Answers

Raj VakatiRaj Vakati
Use like this 
 
trigger MyTrigger on Benifit__c (before insert) {
    Set<Id> lookupIds = new Set<Id>();
    for (Benifit__c : Trigger.new) {
        if (c.Lookup__c != null) {
            lookupIds.add(c.Lookup__c);
        }
    }
    // Handy special map constructor
    Map<Id, Lookup__c> alLkps = new Map<Id, Lookup__c>([
            select Id, medical_location__c,date_of_subscription__c 
            from Lookup__c
            where Id in :lookupIds
            ]);
    for (Benifit__c : Trigger.new) {
        if (c.Lookup__c != null) {
            Lookup__c a = alLkps.get(c.Lookup__c);
            // Use a with c
        }
    }
}

 
Shivam Yadav 8Shivam Yadav 8
Hi Simon,

trigger TestTrigger on Benifit__c (before insert) {
Set<Id> cmpnyIds = new Set<Id>();
for (Benifit__c : Trigger.new) {
if (c.Lookup__c != null) {
cmpnyIds .add(c.Lookup__c); }
} // Handy special map constructor
Map<Id, company__c> cmpnyRecords = new Map<Id, company__c>([ select Id, medical_location__c,date_of_subscription__c from company__cwhere Id in :cmpnyIds ]);
for (Benifit__c b : Trigger.new) {
if (b .company__c!= null) {
company__ca = cmpnyRecords .get(c.company__c); // Use a with c
}
}
}

Thanks!
simon chaosimon chao
trigger MyTrigger on Benifit__c (before insert) 
{
    Set<Id> lookupIds = new Set<Id>();
    for (Benifit__c b : Trigger.new) 
	{
        if (b.company__c != null) 
		{
            lookupIds.add(b.company__c);
        }
    }

    Map<Id, company__c> compRec = new Map<Id, company__c>([
            select Id, medical_location__c,date_of_subscription__c,medical_description__c
            from company__c
            where Id in :lookupIds
            ]);
    for (Benifit__c b : Trigger.new) 
	{
        if (b.company__c != null) 
		{
            company__c c = compRec.get(b.company__c);
			b.medical_location__c = c.medical_location__c;
            b.date_of_subscription__c = r.date_of_subscription__c;
            b.medical_description__c = r.medical_description__c; 
			
        }
    }
}
Thanks, I am able to get it to work. Any idea on how I can start on the test cases? I want to make sure that I have good code coverage. Since this trigger is run on creation of record, how can I replicate that?
 
Raj VakatiRaj Vakati
Try this
 
@isTest
private class BenifitTestCLass {
    static testMethod void MyTest(){
		
		company__c cmp = new company__c() ;
		cmp.medical_location__c ='Test';
		cmp.date_of_subscription__c =System.today() ;
		cmp.medical_description__c ='workng';
		insert cmp ;
		
        Benifit__c  b = new Benifit__c () ; 
		b.Name='Test';
		b.company__c  =cmp.Id ;
		insert b ;
		
	 
    }
}

 
simon chaosimon chao
How can i make it so my trigger doesn't overwrite user input? Right now the record does populate those field after i press save on the record. But if I have user input the trigger overwrites it. Is there a way that I can have the trigger fire only if there is no user input?
I tried this, but trigger still overwrites

 
if (b.company__c != null) 
{
	company__c c = compRec.get(b.company__c);
	if(b.medical_location__c == null)
	{
		b.medical_location__c = c.medical_location__c;
	}
	if(b.date_of_subscription__c == null)
	{
		b.date_of_subscription__c = c.date_of_subscription__c;
	}
	if(b.medical_description__c == null)
	{
		b.medical_description__c = c.medical_description__c;
	}		
}
Raj VakatiRaj Vakati
Try this
 
trigger MyTrigger on Benifit__c (before insert) 
{
    Set<Id> lookupIds = new Set<Id>();
    for (Benifit__c b : Trigger.new) 
	{
        if (b.company__c != null && b.medical_location__c!=null && b.date_of_subscription__c!=null && b.medical_description__c!=null) 
		{
            lookupIds.add(b.company__c);
        }
    }

    Map<Id, company__c> compRec = new Map<Id, company__c>([
            select Id, medical_location__c,date_of_subscription__c,medical_description__c
            from company__c
            where Id in :lookupIds
            ]);
    for (Benifit__c b : Trigger.new) 
	{
        if (b.company__c != null) 
		{
            company__c c = compRec.get(b.company__c);
			b.medical_location__c = c.medical_location__c;
            b.date_of_subscription__c = r.date_of_subscription__c;
            b.medical_description__c = r.medical_description__c; 
			
        }
    }
}

 
This was selected as the best answer
simon chaosimon chao
trigger still overwrite the user input
simon chaosimon chao
thank you, turns out i wasn't saving properly