+ 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
RamakantRamakant
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.
RamakantRamakant
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!!
RamakantRamakant
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!!
RamakantRamakant
Please mark as a best answer!!!