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
Iswarya SekarIswarya Sekar 

Hii Everyone!! I have to create a custom checkbox field in account. when the field is true and saved, all its related opportunity should have stage=Closed won and its amount should not be blank. I tried this using apex triggers. Its not working.

Best Answer chosen by Iswarya Sekar
RKSalesforceRKSalesforce
Please try below code:
trigger UpdateStage on Account (before insert, before update, after update) {
    
    Set<Id> accountIds = new Set<Id>();
    
    for(Account acc : Trigger.new)
    {
        if(acc.All_Opportunities_Won__c==True)
            accountIds.add(acc.Id);
    }
    
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp : [select id, StageName, Amount from Opportunity where AccountId in: accountIds])
    {
        opp.StageName='Closed-Won';
		if(opp.Amount.isEmpty() || opp.Amount == null){
			opp.Amount = 0;
		}
        oppsToUpdate.add(opp);
    }
    
    update oppsToUpdate;
}
Please let me know if it works.

Regards,
Ramakant
 

All Answers

Arjun AccentureArjun Accenture
please provide your logic that you have written.
RKSalesforceRKSalesforce
Hi Iswarya,

We can achieve this by using two triggers:
1) Trigger On Account (After insert, after update) : Here you can check for condition of your check box
2) Trigger on Opportunity (Before insert, Before Update): here you can check for Stage Name and empty Amount field. If Amount field is empty then we can just change Amount to Zero.


Regards,
Ramakant 
Iswarya SekarIswarya Sekar
trigger UpdateStage on Account (before insert, before update, after update) {
    
    Set<Id> accountIds = new Set<Id>();
    
    for(Account acc : Trigger.new)
    {
        if(acc.All_Opportunities_Won__c==True)
            accountIds.add(acc.Id);
    }
    
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp : [select id, StageName, Amount from Opportunity where AccountId in: accountIds])
    {
        opp.StageName='Closed-Won';
        oppsToUpdate.add(opp);
    }
    
    update oppsToUpdate;
}

The amount field should not be blank. How to do this?
Iswarya SekarIswarya Sekar
Hi Ramakant,
How to change Amount field to zero when its empty?? Thanks in Advance!!
RKSalesforceRKSalesforce
Please try below code:
trigger UpdateStage on Account (before insert, before update, after update) {
    
    Set<Id> accountIds = new Set<Id>();
    
    for(Account acc : Trigger.new)
    {
        if(acc.All_Opportunities_Won__c==True)
            accountIds.add(acc.Id);
    }
    
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp : [select id, StageName, Amount from Opportunity where AccountId in: accountIds])
    {
        opp.StageName='Closed-Won';
		if(opp.Amount.isEmpty() || opp.Amount == null){
			opp.Amount = 0;
		}
        oppsToUpdate.add(opp);
    }
    
    update oppsToUpdate;
}
Please let me know if it works.

Regards,
Ramakant
 
This was selected as the best answer
Iswarya SekarIswarya Sekar
Hi Ramakant,
"Method does not exist or incorrect signature: void isEmpty() from the type Decimal". This is the error I'm getting!!
Iswarya SekarIswarya Sekar
Hi Ramakant,
 
if(opp.Amount == null){

            opp.Amount = 0;

        }

This worked for me!! Thank You for your help!!
RKSalesforceRKSalesforce
Please mark as a best answer!!!