+ Start a Discussion
raysfdc1988raysfdc1988 

Trigger-

trigger Attachmentant on Student_Profile__c (before insert, after insert,after update) {

if(trigger.isupdate)
{

list<Referral_Points__c> referral=new list<Referral_Points__c>();

for(Student_Profile__c s: trigger.new){
 
if(stoprecurssion.runonce()){

// when courcse_program name is updated, Create a new record of Referral Points///////

if(s.Course_Program__c !=null)
{
Referral_Points__c rp=new Referral_Points__c();
//////// Get the id and emailid of Referral from student_Profile //////////
Student_Profile__c referedpersonid=[select id ,Student_Profile__c.email__c from Student_Profile__c where UserName__c=:s.Referred_By_email__c limit 1];

rp.Member__c=referedpersonid.id;
rp.Refered_Person__c =s.id;
rp.Referred_by_Email__c=s.UserName__c;

referral.add(rp);
}
}
if ( !referral.isEmpty())
        insert referral;
}          
}

Hi,

Here I need to create a child record when only s.Course_Program__c  is changed but now its creating a child record every time when other fields are updated.
I need to creat a new child record only when s.Course_Program__c is updated... Pl help me to sortout this..
Thanks
Anoop yadavAnoop yadav
Try Like this, this will help you

trigger Attachmentant on Student_Profile__c (before insert, after insert,after update) {

	if(trigger.isupdate) {

		list<Referral_Points__c> referral=new list<Referral_Points__c>();

		for(Student_Profile__c s: trigger.new) {
			
			Student_Profile__c oldStProfile = new Student_Profile__c();
			if(Trigger.isafter && Trigger.isUpdate) {
				oldStProfile = System.trigger.oldMap.get(s.Id);
			}
		 
		if(stoprecurssion.runonce()) {

		// when courcse_program name is updated, Create a new record of Referral Points///////

		if(s.Course_Program__c != oldStProfile.Course_Program__c) {
			Referral_Points__c rp=new Referral_Points__c();
			//////// Get the id and emailid of Referral from student_Profile //////////
			Student_Profile__c referedpersonid=[select id ,Student_Profile__c.email__c from Student_Profile__c where UserName__c=:s.Referred_By_email__c limit 1];

			rp.Member__c=referedpersonid.id;
			rp.Refered_Person__c =s.id;
			rp.Referred_by_Email__c=s.UserName__c;

			referral.add(rp);
		}
	}
	
	if(!referral.isEmpty()) {
		insert referral;
	}          
}

Cloud_forceCloud_force
Hi see if this helps you, an example that show how to compare new and old field values in trigger, http://www.cloudforce4u.com/2013/07/compare-old-and-new-values-in-trigger.html

t
hanks,
http://www.forcexplore.com/2014/01/salesforce-interview-questions-2.html
sushant sussushant sus
Hi,
replace this line 
if(s.Course_Program__c !=null) to 
if(s.Course_Program__c !=System.trigger.oldMap.get(s.Id).Course_Program__c)
raysfdc1988raysfdc1988
thanks Anoop yadav,
sushant sus,
sfdc_wave..........
Its working now ,...thanks a lot