Skip to main content
Hi,

Below is my code

string OnHold = 'On Hold';

       string HoldwithResume = 'Hold with Resume Date';

       string Finished = 'Finished';

       string OnProgram = 'On Program';

       string programStatusOld, programStatusNew;

       //Loop through all records in the Trigger.new collection

       Set<ID> ids = accountNewMap.keySet();   

       List<Account> account = [SELECT Id, Name, Program_Status__c, Discount_Type__c,(SELECT Id, Account.Program_Status__c, Name, Auth_Payment_Status__c, StageName, Account.ParentId, DiscountTotal__c,Coupon_Codes__c  from Opportunities ORDER BY CreatedDate DESC LIMIT 1) FROM Account WHERE Id in :ids];

       List<Opportunity> oppMap = new List<Opportunity>{};

       for(Account record: account)

       {               

               Account accsError = accountNewMap.get(record.Id); 

               programStatusOld = accountOldMap.get(record.Id).Program_Status__c;

            programStatusNew = record.Program_Status__c;

               for(Opportunity opps : record.Opportunities)

               { 

                if(programStatusNew != 'New Customer' && programStatusNew != 'Returning Customer' && record.Id == opps.AccountId)

                {

                    runStatusUpdate = true;

                    if(opps.Account.Program_Status__c == OnHold || opps.Account.Program_Status__c == HoldwithResume)

                    { 

                        opps.StageName = 'On Hold';

                    }

                    else if(opps.Account.Program_Status__c == Finished || opps.Account.Program_Status__c == 'Never Started')

                    {

                        opps.StageName = 'Closed Lost';

                        opps.CloseDate = Date.today();

                    } 

                    else if((programStatusOld != programStatusNew) && programStatusNew == 'On Program' && (programStatusOld == 'New Customer' || programStatusOld == 'Returning Customer') && StatusController.runClosedWon != true)

                    {

                        runStatusUpdate = false;

                        if((record.Discount_Type__c == 'Date Specified' || record.Discount_Type__c == 'Life Time') &&  opps.Coupon_Codes__c == null)

                        {

                            accsError.addError(' Please provide coupon code for the discount type specified!');

                        } 

                        else

                        {

                            opps.StageName = 'Closed Won';

                            opps.CloseDate = Date.today();

                        }

                    }

                    else if(opps.Account.Program_Status__c == OnProgram) 

                    {

                        opps.StageName = 'On Program';

                    }

                    oppMap.Add(opps); 

                } 

                    }

                }                    

               } 

       } 

       if(oppMap.size() > 0)

       {

               update oppMap;

       }   

I am updating 35 records from data loader. If 2 of my records enters custom validation, it shows the validation messages correctly, opportunities are set to closed won for remaining 33 records. Whether my is code correct? Can anyone help me

Thanks,

Vetri
4 risposte
  1. 19 feb 2015, 14:22
    Hi,

    I dont want the Coupon code field in Opportunity should be empty, when account has set as "Life time" or "Date Specificed" when setting the opportunity stage as "Closed Won".

    Thanks,

    Vetri