In the account object Say we have to maintain uniqueness on 2 fields(name and phone).
we need to create a new Text field that has been set as unique. This field can then be updated based on a workflow rule.
Create a workflow
In the Rule Criteria section in the first drop down menu choose "Account: phone" not equal to and then leave the value blank. Click the “Save & Next” button.
click the "Add Workflow Action" drop down and click "New Field Update". Give the name to update the field name. In the Field to update dropdown choose the Field created. In the "Specify New Field Value" section choose "Use a formula to set new value". Click the “Show Formula Editor” link. Click "Insert Field". Find the "External Field" field. Click the "Insert" button. Click the "Save" button. Click the "Done" button. Click the "Activate" button.
This Can be achieved even with trigger. Please find the below code
we need to create a new Text field that has been set as unique. This field can then be updated based on a workflow rule.
Create a workflow
In the Rule Criteria section in the first drop down menu choose "Account: phone" not equal to and then leave the value blank. Click the “Save & Next” button.
click the "Add Workflow Action" drop down and click "New Field Update". Give the name to update the field name. In the Field to update dropdown choose the Field created. In the "Specify New Field Value" section choose "Use a formula to set new value". Click the “Show Formula Editor” link. Click "Insert Field". Find the "External Field" field. Click the "Insert" button. Click the "Save" button. Click the "Done" button. Click the "Activate" button.
This Can be achieved even with trigger. Please find the below code
trigger updateaccountExternalField on Account (before insert, before update) { Map<String, Account> accountMap =new Map<String, Account>(); for (Account acc : Trigger.new){ if ((acc.phone !=null) &&(Trigger.isInsert ||(acc.phone != Trigger.oldMap.get(acc.Id).phone))){ if (accountMap.containsKey(acc.Name+acc.phone)){ acc.External_Field__c .addError('Another Account has the same phone.'); }else{ accountMap.put(acc.Name+acc.phone, acc); } } acc.External_Field__c = acc.Name+acc.phone; } for (Account account : [SELECT External_Field__c FROM Account WHERE External_Field__c IN :accountMap.KeySet()]){ Account newContact = accountMap.get(acc.External_Field__c); newContact.External_Field__c.addError('An Account with this phone already exists.'); } }
No comments :
Post a Comment