0

The requirement is On the std layout of opportunity,there is a field probability which has some default value based on the stage that you use. When I modify the default value of probability,I have to display a Waring message on screen. e.g.At Initial Stage Probability Value is 10% i have to change it 15%.As soon as I change to 15%,it should display some warning message which will display via inline VF.

for this I have written below code:

public static void probabilityDefaultValueChange(List<Opportunity>opt,Map<Id,Opportunity> optt)
     {
         List<Id>oppIds=new List<Id>();     

             for(Opportunity o:opt)
             {
                 if(o.Probability!=null && o.Id!=null && optt.get(o.Id).Probability!=10 )
                 {

                    oppIds.add(o.Id); 

                 }

             }


         List<Opportunity>opToUpdate=[Select Id,StageName,Probability,Custom_Probability_Change__c from Opportunity where Id in:oppIds];
         for(Opportunity o1:opToUpdate)
         {
             o1.Custom_Probability_Change__c=true;
         }
        Update opToUpdate;
     }

Custom_Probability_Change__c is a checkbox field That I have to send it Inline VF in order to display warning message and the condition optt.get(o.Id).Probability!=10 it checks whether Probability default value is default(10% here) or not.If i change default value(10%) to 15% it should update Custom_Probability_Change__c checkbox field to true.But it is not updating checkbox field value.

Can someone please guide what I am missing in this code?

Also I am new to VF page and not sure how to pass the Field value to VF page to display warning message(In this case I will send Custom_Probability_Change__ value to VF and once it checkbox field value is true,it displays warning message).It would be great if someone can help me with that.

0

First of all you code has some issues, kindly consider the below code:

Apex

public static void probabilityDefaultValueChange(List<Opportunity>opt)
{
    List<Opportunity>opToUpdate = new List<Opportunity>();     
    // why do you have a map of opp and a list of opp? consider using one. 
    for(Opportunity o:opt)
    {
        if(o.Probability!=null && o.Id!=null && o.Probability!=10 )
        {
            Opportunity opp = new opportunity();
            opp.Custom_Probability_Change__c = true;        
            opToUpdate.add(opp); 
        }
    }

    /* below code not required, but make sure you query "Custom_Probability_Change__c" field in your map or list
        List<Opportunity>opToUpdate=[Select Id,StageName,Probability,Custom_Probability_Change__c from Opportunity where Id in:oppIds];
        for(Opportunity o1:opToUpdate)
        {
            o1.Custom_Probability_Change__c=true;
        }
        */
    Update opToUpdate;
}

VF Page

<apex:page>
       <apex:form>
         <apex:selectList value="{!selectedItem}" onchange="callMethod()">
           <apex:selectOption value="{!itemList}"
         </apex:selectList>
       <apex:actionfunction name="callMethod" action="{!controllerMethod}" oncomplete="showAlert({!returnValue })"/>
      <apex:form>
  <script>
    function showAlert(t) {
      if(t) {
        alert('Correct');
      } else {
        alert('No value entered !');
      } 
    }
  </script>
</apex:page>

You should change the VF page script code to match your needs. But this should give you a head-start.

Above VF page code is courtesy of this post. Thanks to Ashwini.

  • It is not updating the value. – Dhananjay Patil Nov 10 '16 at 13:50
  • Exact code might not work, since I dont know from where "probabilityDefaultValueChange" method is called and what is being passed to it. I made those amends to give you an idea on how to write efficient code and conform to the best practices. – Mahmood Nov 10 '16 at 13:52
0

What about doing something like this ?

Apex: `

trigger OpportunityTrigger on Opportunity (after insert, after update, before insert, before update, before delete, after delete) {
    if(Trigger.isUpdate) {
        if(Trigger.isBefore) {
            OpportunityTriggerController.probabilityDefaultValueChange(Trigger.new, Trigger.oldMap);
        }
    }
}


public class OpportunityTriggerController {
    public static void probabilityDefaultValueChange(List<Opportunity> lstOpportunity, Map<Id,Opportunity> mapOld) {
        for(Opportunity objOpportunity : lstOpportunity) {
            if(objOpportunity.Probability != mapOld.get(objOpportunity.Id) && mapOld.get(objOpportunity.Id).Probability == 10) {
                objOpportunity.addError('Default Opportunity Probability has changed');
            }   
        }
       }
}

` Since method is called on before update, you dont need to call to update

update lstOpportunity;

VForce:
<apex:pageMessages ></apex:pageMessages>

  • Can you please tell me how can i modify in my existing code? – Dhananjay Patil Nov 10 '16 at 13:25
  • 1
    @ransommule: you can't use apex page message in trigger. – Mahmood Nov 10 '16 at 13:57
  • you are right.. you should use objOpportunity.addError('Your custom error message'); – ransommule Nov 10 '16 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.