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
Sanjana_singh12Sanjana_singh12 

When Amount field on Opportunity object is greater than $50k, mark is_goldenAccount to TRUE.

ANUTEJANUTEJ (Salesforce Developers) 
Hi Sanjana,

One way is to simply make a formula field as mentioned in below link: https://salesforce.stackexchange.com/questions/178681/workflow-rule-to-check-a-checkbox-when-the-purchase-date-is-bigger-than-7-days

Another way is to use workflow rule that checks the amount and checks the checkbox, below is a screenshot of a sample:

User-added imageUnder immediate Workflow Actions, you can make an update workflow action that can make the checkbox true.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Sanjana_singh12Sanjana_singh12
No, I need an apex code for solving this.
public class trigger_class{
public void trigger_method(List<Opportunity> ops)
{
for(Opportunity o:[SELECT Amount  FROM OPPORTUNITY where amount > 50000])
{
if(o.Amount>50000)
{
Account acnt = new Account();    I  struck on this step.can you please help me out?The pre-requisite for that question is to Create a field on Account called “isAccount_golden”, checkbox, default off
ANUTEJANUTEJ (Salesforce Developers) 
You can try the below snippet and modify the API names and as you are creating new account instance I am assuming you are inserting new account record for every opportunity reacord that is above 50000. Incase you want to update the existing records you need to fetch them and make a update list statement.
 
public class trigger_class{
public void trigger_method(List<Opportunity> ops)
{
list<account> aclist= new list<account>();
for(Opportunity o:[SELECT Amount  FROM OPPORTUNITY where amount > 50000])
{
Account acnt = new Account(); 
acnt.name=o.id+'Account';
acnt.isAccount_golden__c = true;
aclist.add(acnt);
}
if(aclist.size()>0)
{
insert aclist;}
}

To create a custom field check the below steps:

Go to setup > click on object manager > open account object > navigate to fields and relationships by clicking on it on the left side > click on new button > choose type as checkbox > add necessary fields like name and default option etc > now choose field-level security > add this field to page layouts > click save 

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Sanjana_singh12Sanjana_singh12
Thanks for the response."acnt.name=o.id+'Account';" I cant understand this line of code..why this code is used..can we use "update aclist;" instead of "insert aclist;" because i think the question says us to update something not to insert..what say
ANUTEJANUTEJ (Salesforce Developers) 
acnt.name=o.id+'Account'; 

this was a sample line you need not change the name you can simply replace this line with getting the account id and using it in the for loop.

for(Opportunity o:[SELECT Amount,AccountId  FROM OPPORTUNITY where amount > 50000])
{
Account acnt = new Account(); 
acnt.id=o.AccountId;
acnt.isAccount_golden__c = true;
aclist.add(acnt);
}

if(aclist.size()>0)
{
update aclist;}


again, as mentioned it is a sample snippet you need to modify it as per your use case and as per your scenario.

Please mark this as best answer so that it can be useful to others in the future.
Sanjana_singh12Sanjana_singh12
Pre-requisite
Create a field on Account called “is_goldenAccount”, checkbox, default off
Assignment
When Amount field on Opportunity object is greater than $50k, mark is_goldenAccount to TRUE. Help me in solving this..I am newbie to apex 
ANUTEJANUTEJ (Salesforce Developers) 
So first to create a custom field of type checkbox which is unchecked by default on the account object you need to follow these steps:

Go to setup > click on object manager > open account object > navigate to fields and relationships by clicking on it on the left side > click on new button > choose type as checkbox > add necessary fields like name and default option etc > now choose field-level security > add this field to page layouts > click save 

For the second step as you have mentioned you want to go with apex code you need to create a trigger opportunity that checks this amount and calls the helper method accordingly for this you can use the below code:
 
trigger OPP_trigger on opportunity(before insert, before update)
{
if(trigger.isbefore && (trigger.isupdate || trigger.isinsert))
{
set<id> ac_id = new set<id>();
for(Opportunity o:trigger.new)
{
if(o.amount>50000){ac_id.add(o.AccountId);}
}
trigger_classHelper.HelperMethod(ac_id);
}

}
 
public class trigger_classHelper{
public static void HelperMethod(set<id> accountsToUpdate)
{
list<account> aclist= new list<account>();
for(id i: accountsToUpdate)
{
	Account acnt= new Account();
	acnt.id=i;
	acnt.is_goldenAccount__c=true;
	aclist.add(acnt);
}
if(aclist.size()>0)
{
update aclist;}
}

}

Note: I am assuming the accountid is not null.

Please enhance this to fit your needs and select this as the best answer so that it can be useful to others in the future.