function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
learn_cloudsflearn_cloudsf 

vf page gives error while saving

Hi all ,
I am trying to implemnt the codde from below link to test the prevention of avoiding duplicates from accounts.
https://developer.salesforce.com/page/Avoid_Creation_of_Duplicate_Account_Names
However while saving the vf page i get an error. what wrong I am doing?
Thanks inadvance
Joe
Best Answer chosen by learn_cloudsf
Arunkumar RArunkumar R
Hi Joe,

The content you are trying from this link (https://developer.salesforce.com/page/Avoid_Creation_of_Duplicate_Account_Names) is Support only on S-Control NOT for Visualforce Page. S-Control is already expired, you cannot use this no more.

So i would like to suggest go for trigger, Find the below link it will helpful to prevent duplicate records.

http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving

All Answers

Arunkumar RArunkumar R
Hi joe,

What error you are getting?
Shrikant BagalShrikant Bagal
What is the error?
learn_cloudsflearn_cloudsf
Hi Arun/ Shri,
Error: Content is not allowed in prolog.
Error: joepage12 line 1, column 1: Content is not allowed in prolog

Thanks.
Shrikant BagalShrikant Bagal
Hello JDiaz,

Please try following codel:

 
<apex:page>
<style type="text/css">

.STYLE2 {
        font-size: 12px;
        font-weight: bold;
}
.STYLE3 {color: #FFFFFF}
.STYLE4 {font-size: 12px; font-weight: bold;}
</style>
<link href="https://emea.salesforce.com/dCSS/Theme2/default/common.css" type="text/css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet" >

<script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>
<script language="JavaScript">
var has_CompleteSame = false;
var searched = false;

function CheckAccounts()
{
        var has_CompleteSame = false;
        var searched = false;

        var AccKeyName = document.FilterForm.AccName.value;
   if(AccKeyName == ""){
                return;
   }
  
   sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}");
   sforceClient.batchSize = 20; 
  
   SearchSameAccount();
   SearchLikeAccount();
       
        searched = true;
}

function SearchSameAccount(){
        var AccKeyName = document.FilterForm.AccName.value;
        var output = "";
  
        var sq = "select ID, Name, atype__c from Account where Name = '" + AccKeyName + "'";//} IN NAME FIELDS RETURNING Account(ID, Name, atype__c)";
        var sr = sforceClient.Query(sq);
        if(sr.className == "QueryResult") {
                var len = sr.records.length;
               
                if(len>0){
               
                        has_CompleteSame = true;
                        output = "<div class='bAccount'><div class='bPageBlock secondaryPalette'><div class='pbHeader'><div class='pbBody'>";
                        output += "<table class='sortable' id='unique_id'><tr class='headerRow'>";
                        output += "<td>Account</td>";
                        output += "<td>Account Record Type</td>";
                        output += "</tr>";
               
                        for(var i=0;i<len;i++){
                                var accId = sr.records[i].get("ID");
                                var accName = sr.records[i].get("Name");
                                var accType = sr.records[i].get("atype__c");
                                               
                                output += "<tr><td><a href='/"+ accId +"' target=_parent>" + accName + "</a></td>";
                                output += "<td>" + accType  + "</td></tr>";
                        }
                       
                        output += "</table></div></div>";
                }
                else
                        has_CompleteSame = false;              
        }
       
       
        document.getElementById("SameResultsHolder").innerHTML = output;       
}

function SearchLikeAccount(){
       
        var AccKeyName = document.FilterForm.AccName.value;
        var output = "";
  
        var sq = "FIND {*" + AccKeyName + "*} IN NAME FIELDS RETURNING Account(ID, Name, atype__c)";
        var sr = sforceClient.Search(sq);
        if(sr.className == "SearchResult") {
                var len = sr.searchRecords.length;
               
                if(len>0){
                       
                        output = "<div class='bAccount'><div class='bPageBlock secondaryPalette'><div class='pbHeader'><div class='pbBody'>";
                        output += "<table class='sortable' id='unique_id'><tr class='headerRow'>";
                        output += "<td>Account</td>";

                        // if you use record type, use this
                        //output += "<td>Account Record Type</td>";
                        output += "</tr>";
               
                        for(var i=0;i<len;i++){
                                var accId = sr.searchRecords[i].get("ID");
                                var accName = sr.searchRecords[i].get("Name");
                                var accType = sr.searchRecords[i].get("atype__c");
                                               
                                output += "<tr><td><a href='/"+ accId +"' target=_parent>" + accName + "</a></td>";
                                //output += "<td>" + accType  + "</td></tr>";
                        }
                       
                        output += "</table></div></div>";
                }
        }
       
        document.getElementById("ResultsHolder").innerHTML = output;
}

function CreateAccount(){

        if(!searched)
                CheckAccounts();
        else
                sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}");
       
        if(!has_CompleteSame){
       
        var AccKeyName = document.FilterForm.AccName.value;
        if(AccKeyName == ""){
                return;
   }

        // if you use record type, use this
        //var hrf = parent.location.href;
        //hrf = hrf.split("=");
        //var type = hrf[2].substring(0,15);

        var newAcc = new Sforce.Dynabean("Account");
        newAcc.set("Name", AccKeyName);

        //if you use record type, use this
        //newAcc.set("RecordTypeId", type);
       
        var sr = sforceClient.Create([newAcc])[0];
       
        var id = sr.id;
        var hr = "https://emea.salesforce.com/" + id +"/e?retURL=%2F" +id;
        parent.location.href = hr;     
        }
        else{
                alert("Duplicated Account Name found! Pleas change the account name.");
        }
}

function AccChanged(){
        searched = false;
}
</script>
<BODY class="home homepage" id="homePage">
<table width="100%" border="0" cellspacing="0">
  <tr>
    <td height="32" colspan="2" bgcolor="#4579B5"><p class="STYLE3"><span class="STYLE4">Prompt:</span>:<span class="STYLE2">Before you create a new account name, input the name and click Check button to check...</span></p>      </td>
  </tr>
  <tr>
    <td height="17" colspan="2" bgcolor="#FFFFFF">&nbsp;</td>
  </tr>
  <tr>
  <form name=FilterForm method="POST" action="javascript:CheckAccounts()">
    <td width="312" height="55" bgcolor="#FFFFCC">
     
      <div align="center">
      Account Name: <input type="text" OnChange="javascript:AccChanged()" name="AccName"/>
      </div></td>
    <td width="659" bgcolor="#FFFFCC">
      <div align="left">
        <input type="submit" name="Submit" value="Check" />
        <input name="createAcc" type="button" id="createAcc" OnClick="javascript:CreateAccount()" value="New" />
      </div></td>
      </form>
  </tr>

<tr>
    <td height="45" colspan="2">Duplicated Name:
    <div id="SameResultsHolder"></div>
        </td>
</tr>
<tr>
    <td height="2" colspan="2">&nbsp;</td>
</tr>
<tr>
    <td height="45" colspan="2">Similar Name:
    <div id="ResultsHolder"></div>
        </td>
</tr>
<table>

</apex:page>

If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks!  
omm ommomm omm
Hi  JDiaz
If you want to avoid duplicates you can write a trigger . the below trigger may help you 

trigger dupeacc on Account (before insert) 
{
 for(account a:trigger.new)
 {
 list <account > ac=[select id, name from account where name=:a.name];
 if(ac.size()>0)
 {
  a.addError('duplicates are not entertained');
 }
 }
}
 
learn_cloudsflearn_cloudsf
hi shikant,
I did paste your code.
it gave Error: vfnew line 159, column 14: Open quote is expected for attribute "name" associated with an element type "form"
Error: Open quote is expected for attribute "name" associated with an element type "form".

 
Arunkumar RArunkumar R
Hi Joe,

The content you are trying from this link (https://developer.salesforce.com/page/Avoid_Creation_of_Duplicate_Account_Names) is Support only on S-Control NOT for Visualforce Page. S-Control is already expired, you cannot use this no more.

So i would like to suggest go for trigger, Find the below link it will helpful to prevent duplicate records.

http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
This was selected as the best answer
learn_cloudsflearn_cloudsf
Thanks Arun.