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
Zhenyu LiuZhenyu Liu 

Default Date field based on different situation

Hi,

I have a field is called "Close Date" and user want to default that field based a pick list filed. For example, the picklist is abc, the close data should be today + 10 days. if picklist is bcd, the close date should be today + 20 days.

Is there any export in coding could help me out?

Thanks in advance 
Best Answer chosen by Zhenyu Liu
Neetu_BansalNeetu_Bansal
Hi Zhenyu,

Use the below trigger, Here I am assuming that your picklist API name is 'Type':
trigger OppTrg on Opportunity( before insert, before update )
{
	for( Opportunity opp : trigger.new )
	{
		if( opp.Type != null )
		{
			if( opp.Type.equalsIgnoreCase( 'abc' ))
			{
				opp.CloseDate = Date.today().addDays( 10 );
			}
			else if( opp.Type.equalsIgnoreCase( 'bcd' ))
			{
				opp.CloseDate = Date.today().addDays( 20 );
			}
		}
	}
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Ketankumar PatelKetankumar Patel
If both fields are on same object then you can use workflow and field update to update your closed date. 

You may need to create seperate workflow for each of your update. 

Workflow Rule Criterial
ISPICKVAL(your_picklist_field, "Picklist Value")

User-added image


Select Closed date as field to update and select  "Use a formula to set the new value" and use below formula.
Today() + 10

User-added image

 
Neetu_BansalNeetu_Bansal
Hi Zhenyu,

Use the below trigger, Here I am assuming that your picklist API name is 'Type':
trigger OppTrg on Opportunity( before insert, before update )
{
	for( Opportunity opp : trigger.new )
	{
		if( opp.Type != null )
		{
			if( opp.Type.equalsIgnoreCase( 'abc' ))
			{
				opp.CloseDate = Date.today().addDays( 10 );
			}
			else if( opp.Type.equalsIgnoreCase( 'bcd' ))
			{
				opp.CloseDate = Date.today().addDays( 20 );
			}
		}
	}
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer