+ Start a Discussion
Navneeth RajNavneeth Raj 

Trigger Program for my field

Hi, there. I have a custom obj named "Student", in that i have field named "Country", so whenever a new student is created i want the country field has to be entered automatically. How can i write trigger for that?
Best Answer chosen by Navneeth Raj
Amit Chaudhary 8Amit Chaudhary 8
Hi Navneeth,

You need to write below line only when you want to create the object or instance of Student object.
Student__c std = new Student__c();
or if you want to create new Record base on old record etc. And Trigger will fire only when DML will happened. 

But in our case we dnt need to create any object. Only below trigger we need to use.
 
trigger StudentTrigger on Student__C( before Insert)
{
	for(Student__c sc : Trigger.New)
	{
		sc.Country__C = 'INDIA';
	}
}


Please mark this as solution if this will help you. So that is some one has same issue this post can help other

Thanks
Amit Chaudhary


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Navneeth,

You can do this by below solution
1) Set Default country
2) Workflow field update
3) Trigger
Please add all valid API name according to your org
trigger StudentTrigger on Student__C( before Insert)
{
	for(Student__c sc : Trigger.New)
	{
		sc.Country__C = 'INDIA';
	}
}

Please mark this as solution if this will help.

Thanks
Amit Chaudhary
 
Navneeth RajNavneeth Raj
Ok, thank you, Bt  when will you write the code like this   "Student__c std = new Student();"    i mean what could be the requirement so that we write code like this!
Amit Chaudhary 8Amit Chaudhary 8
Hi Navneeth,

You need to write below line only when you want to create the object or instance of Student object.
Student__c std = new Student__c();
or if you want to create new Record base on old record etc. And Trigger will fire only when DML will happened. 

But in our case we dnt need to create any object. Only below trigger we need to use.
 
trigger StudentTrigger on Student__C( before Insert)
{
	for(Student__c sc : Trigger.New)
	{
		sc.Country__C = 'INDIA';
	}
}


Please mark this as solution if this will help you. So that is some one has same issue this post can help other

Thanks
Amit Chaudhary


 
This was selected as the best answer
Navneeth RajNavneeth Raj
Yes, sorry. 
That's great & cool. thanks Amit for quick replies, now i came to know why coding is fun.