1

Class:

public pagereference updateEngagements(){          
         if(wrapList!=null && wrapList.size()==0 ) { 
         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'ERROR: Nothing to Update '));
                 return null;  
                 }
         Site__c siteSpecialities =[Select Id,Name,Tracks__c from Site__c WHERE Name=:selectedSite AND Cohort__r.Name=:selectedCohort LIMIT 1];
         try{ 
              list<Engagement__c> enList=new list<Engagement__c>();              
                 for(WrapperClass wr: wrapList){ 
                    if(wr.selectedLC!='' && wr.selectedLC!=null && wr.selectedLC!='--Unassigned--' ){  
                         Engagement__c en= new Engagement__c();
                         en.id = wr.eng.Id; 
                         en.Learning_Community__c= wr.selectedLC;
                         if(JSON.serialize(siteSpecialities.Tracks__c).contains(wr.eng.Track_Assignment__c)){                               
                             wr.specialCheck ='';
                             en.Track_Assignment__c = wr.eng.Track_Assignment__c; 
                         }else {                              
                             wr.specialCheck ='border-radius: 10px 10px 10px 10px; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border: 3px solid red;';
                             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'ERROR: Please select the correct Speciality. Available Specialities for this Site are: '+siteSpecialities.Tracks__c));

                         }
                         enList.add(en);             
                     }  

                 }                  
                 if(enList.size()>0 ){
                     System.debug(enList);
                     itFailed = true;   
                     Database.update(enList);
                     }
                  }

         catch(System.DmlException e){
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'ERROR: ' +e));
                 return null;
             }

         return null;
     }

Page:

<tbody>
                            <apex:repeat value="{!wrapList}" var="e">
                                <tr class="slds-hint-parent">
                                  <td role="gridcell" class="slds-cell-shrink" data-label="Select row Engagement">
                                    <label class="slds-checkbox">                          
                                      <apex:inputCheckbox value="{!e.selected}" id="inputId" />          
                                      <span class="slds-checkbox--faux"></span>
                                      <span class="slds-assistive-text">Select row Engagement</span>
                                    </label>
                                  </td>         
                                  <td role="gridcell" data-label="Account Name">
                                    <div class="slds-truncate" title="Cloudhub"><apex:outputText value="{!e.eng.name}" /></div>
                                  </td> 
                                  <td><apex:outputText value="{!e.eng.Student_Full_Name__c}" id="studentName" /></td>   
                                  <td>
                                    <apex:selectList styleclass="slds-input" value="{!e.selectedLC}" size="1" id="lcoption">                                                                              
                                          <apex:selectOptions value="{!e.selectedCLC}"/> 
                                          <apex:param value="{!e.rowcount}" assignTo="{!count}" name="count"/> 
                                    </apex:selectList>
                                  </td>                   
                                  <td><apex:outputText value="{!e.eng.Prospect_Track_Interest__c }" /></td>  
                                  <td>
                                      <apex:outputPanel id="trackassignment">                                    
                                              <div style="{!e.specialCheck}" class="slds-form-element">                                      
                                              <div class="slds-form-element__control slds-picklist">
                                                <apex:inputfield styleclass="slds-select " value="{!e.eng.Track_Assignment__c}"/>
                                             </div>
                                            </div>
                                    </apex:outputPanel> 
                                  </td>  
                                  <td> 
                                  <apex:outputPanel id="Strackassignment">
                                       <div style="{!e.specialCheck}" class="slds-form-element">                                      
                                              <div class="slds-form-element__control slds-picklist">
                                     <apex:inputfield styleclass="slds-select " value="{!e.eng.Secondary_Specialty_Assignment__c}"/>
                                      </div>
                                       </div>
                                   </apex:outputPanel>      
                                  </td>                                  
                                </tr>  
                            </apex:repeat>
                          </tbody>

Issue:

I am printing the error message in case of wr.eng.Track_Assignment__c not matching with siteSpecialities. However I am not using return null, becuase I need to show the red border for each row in case of error. Since I am not using the return null, system is updating the records. How do I stop updating the records if there is a mismatch for one of the records?

Multiple fields:

    if(JSON.serialize(siteSpecialities.Tracks__c).contains(wr.eng.Track_Assignment__c)){                               
                                 wr.specialCheck ='';
                                 en.Track_Assignment__c = wr.eng.Track_Assignment__c; 
                             }else {                              
                                 wr.specialCheck ='border-radius: 10px 10px 10px 10px; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border: 3px solid red;';
                                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'ERROR: Please select the correct Speciality. Available Specialities for this Site are: '+siteSpecialities.Tracks__c));

                             }

if(wr.eng.Secondary_Specialty_Assignment__c !=null && JSON.serialize(siteSpecialities.Tracks__c).contains(wr.eng.Secondary_Specialty_Assignment__c)){  
                                 wr.specialCheck ='';
                                 en.Track_Assignment__c = wr.eng.Secondary_Specialty_Assignment__c; 
                             }else{

                                 wr.specialCheck ='border-radius: 10px 10px 10px 10px; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border: 3px solid red;';
                                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'ERROR: Please select the correct Speciality. Available Specialities for this Site are: '+siteSpecialities.Tracks__c));

                             }
0

there are few different ways you can address this..

one option is to declare a boolean variable with default value false

bError = false;

then Set a boolean variable to true in the block of code where you set the error message..

else {                              
      wr.specialCheck ='border-radius: 10px 10px 10px 10px; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border: 3px solid red;';
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'ERROR: Please select the correct Speciality. Available Specialities for this Site are: '+siteSpecialities.Tracks__c));
      bError = true;                      
}

and in the section where you do the update check if that boolean variable is false

if(enList.size()>0 && bError == false){
     System.debug(enList);
     itFailed = true;   
     Database.update(enList);
}

the other option is you can directly check the ApexPages.getMessages() list and see if its empty or not before doing the update

  • That is wonderful idea, however how about incase I have same validation for two fields in the wrapper. Please refer my updated description "Multiple fields". If I use that code, border highlight is not working as expected. Any suggestion? – Ajay Nov 18 '16 at 4:17

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.