Tuesday, November 15, 2016

Insert a Validation / Warning in the Account Merge Wizard

Use Case

We may want to insert a Validation / Warning in the Account Merge Wizard. If a certain criteria met during Account Merge, we may want to show a Validation Error Message or a Warning to the user in the Account Merge Wizard itself.

Solution

Step 1:

We can achieve this functionality by checking the Account that is going to be deleted after the merge for the criterion in Account after delete Trigger and from there we will set a global constant with a value ‘TRUE’.

Global Class

Global Class GlobalConstants
{

    Global Static Boolean masterAccountActiveFlag  = false;
    Global Static Boolean mergeAccount  = false;   

}

Trigger – Account After Delete

trigger MergeValidationAfterDelete on Account (after delete) {
    for(Account temp: Trigger.old)

    {

        if (temp.MasterRecordId != null) ;                   

            {

                GlobalConstants.mergeAccount = true;

                if(temp.Status__c == 'Suspend')

                {   

                    GlobalConstants.masterAccountActiveFlag = true;  

                }

            }

    }

}

Step 2:

After this we check the Account to be updated(Surviving Account) for the condition and also we check the value of the static variable. If the Static Variable is true, we throw the warning message via a popup.(Using Javascript). All this handled through account before update trigger.
 
The Popup is a VF Page where we are showing the warning message. If the user clicks OK Button she/he redirected to the merge wizard and now if we click the merge button, warning message will not be shown as on the OK button VF controller update a Custom setting and check for the value of this custom setting before throwing the warning message.

Custom Setting

Label
MergeAcchelper
Object Name
MergeAcchelper
API Name
MergeAcchelper__c
Setting Type
List
Visibility
Public
Description
 
 
 
 
Field in Custom Setting

Field Label
Mergeflag
Object Name
MergeAcchelper
Field Name
Mergeflag
Data Type
Checkbox
API Name
Mergeflag__c
Default Value
Checked

Visual Force Page

<apex:page controller="vfc_mergeHelperPage" sidebar="false" showHeader="false">
 
<script>
 
 function openInParent() {
   window.opener.location.href = '/001/o';
   window.close();
  }
 
</script>
<apex:form >
<center>
<p style="font-family:helvetica;font-size:15px;">
One or both of the accounts to be merged have a Status of "Suspend".<br/>
Please review with accounting before proceeding. 
<br/>
(If you want to continue with merge, please click OK and then Click Merge)</p>
 
<apex:commandButton value="OK" oncomplete="window.close();" action="{!setId}"/>
<apex:commandButton value="Cancel" onclick="openInParent()"/>
</center>
</apex:form>
</apex:page>

Controller

public class vfc_mergeHelperPage {

public string account1{get;set;}

public string account2{get;set;}

public vfc_mergeHelperPage()

{

GlobalConstants.redirect = 'mergeHelperPage';

}

public void setId()

{

List<MergeAcchelper__c> clearlist = [select id from MergeAcchelper__c where name = 'StopMerge'];

      clearlist[0].Mergeflag__c = False;

      update clearlist;

      



system.debug('setting inserted');

}

}

Trigger – Account Before Update


trigger MergeValidationBeforeUpdate on Account (before update) {

map<string,MergeAcchelper__c> helpMap=new map<string,MergeAcchelper__c>();

helpMap = MergeAcchelper__c.getall();

Public MergeAcchelper__c flag;

    if(GlobalConstants.mergeAccount)

    {  flag=helpMap.get('StopMerge');

      if ((trigger.old[0].Status__c == 'Suspend' || GlobalConstants.masterAccountActiveFlag == true)&& flag.Mergeflag__c==True)

      { 

       trigger.new[0].addError('<html> <script> function confirmGetMessage() {showModalDialog("/apex/mergeHelperPage","popUpWindow","dialogHeight:140px;dialogWidth:600px;scrollbar:no;");}window.onload = confirmGetMessage;</script></html>');

                                 

      }

      else

      {

      

      List<MergeAcchelper__c> clearlist = [select id from MergeAcchelper__c where name = 'StopMerge'];

      clearlist[0].Mergeflag__c = True ;

      update clearlist;

         

      GlobalConstants.mergeAccount = false;

      GlobalConstants.masterAccountActiveFlag = false;

    }     

}

}


No comments:

Post a Comment