+ Start a Discussion
nikkeynikkey 

Trigger to Auto update child record when parent is created

Hi Friends ,

 

I have two custom objects one is Student and the other is hostel .There do have a look up relation (child : hostel , parent:student).

Scenario is : I have a Confirm hostel as checkbox field in student Object.when i create a record in student and check the confirm hostel field a record should be created in hostel.The code works fine.

The other criteria is i have a gender as picklist field in student object.when i select the gender value as MALE and check the confirm hostel field a record should be created in hostel .The code works fine.

But when i edit and do some changes like changing the gender to female  then the record gets saves as MISSMRSTUDent or unchecking the confirm hostel field then the record gets saved as MISSMISSMRSTUDENT.

Can any one help me in this code.

 

CODE :

trigger autoupdatehostel on Student__c (before insert,before update,after insert,after update)
 {
if(trigger.isbefore)
{
if(trigger.isinsert || trigger.isupdate)
{
 // In before insert or before update triggers, the trigger accesses the new records
    // with the Trigger.new list.

for(Student__c st : trigger.new)
{
if(st.Gender__c=='Male')
{st.Name = 'Mr'+st.Name;
}
else if(st.Gender__c=='Female')
{
st.Name ='Miss'+st.Name;
}
}
}
}
// If the trigger is not a before trigger, it must be an after trigger.
else if(trigger.isafter)
{
if(trigger.isinsert || trigger.isupdate)
{
list <Hostel__c> newhost = new list<Hostel__c>();
{
for(Student__c st : trigger.new)
{
 if(st.ConfirmHostel__c==true)
{
//Hostel__c ht = new Hostel__c();
//ht.Name = st.Name+'Hostel';
//ht.RefStud__c=st.id;
newhost.add(new Hostel__c(Name = st.Name+'Hostel',
                          RefStud__c=st.id));
}
insert newhost;
}
//if(newhost.size()>0 && newhost!=null)
//{
//insert newhost;
//}
}
}}
}

I Shall appreciate ur help

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989
if(st.Gender__c=='Male'){
	if(!st.Name.contains('Mr')){
		st.Name = st.Name.replaceFirst('Miss','');
		st.Name = 'Mr'+st.Name;
	}
}else if(st.Gender__c=='Female'){
	if(!st.Name.contains('Miss')){
		st.Name = st.Name.replaceFirst('Mr','');
		st.Name ='Miss'+st.Name;
	}	
}

 

All Answers

hitesh90hitesh90

Hi nikkey,

 

do below change in your trigger as per your requirement.

you have to replace Mr and MISS as per condition.

 

if(st.Gender__c=='Male'){
	st.Name = st.Name.replaceFirst('Miss','');
	st.Name = 'Mr'+st.Name;
}else if(st.Gender__c=='Female'){
	st.Name = st.Name.replaceFirst('Mr','');
	st.Name ='Miss'+st.Name;
}

 

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

asish1989asish1989
if(st.Gender__c=='Male'){
	if(!st.Name.contains('Mr')){
		st.Name = st.Name.replaceFirst('Miss','');
		st.Name = 'Mr'+st.Name;
	}
}else if(st.Gender__c=='Female'){
	if(!st.Name.contains('Miss')){
		st.Name = st.Name.replaceFirst('Mr','');
		st.Name ='Miss'+st.Name;
	}	
}

 

This was selected as the best answer
nikkeynikkey

Hey hi Hitesh & Asish ,

 

 

 

Thanks for ur help in resolving my code