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
Dilip KrDilip Kr 

Confirmation dialog box if new material is already exist in the material object.

Fetching all data from product record and creating new material records using the same information but i want  a confirmation dialog box if new material with same name is already exit in material object record. I tried code below but it is unable to check duplicate record in material.When i clicked "createMaterial" button,everytime it is created material record using product record data. Could you please see the code and let me know what i am missing. I thought that duplicate logic is not able to call.

APEX CLASS:

Public class ctrlCloneProductToMaterial{
        Public Id id;
        public List<Material__c> dupe;
        public boolean dupFound{get; set; }
        public static final Id Material_Rtype=Rtype.getIdByDevName('material__c','DEC');
        public static final Id Product_Rtype=Rtype.getIdByDevName('product2','DEC');

        public ctrlctrlCloneProductToMaterial(apexPage.StandardController contoller){
           id=apexPages.currentPage().getparameters().get('id');
          }
        
       public PageReference cloneProduct(){
           product2 prod=new product2();
           Material__c mat= new Material__c();
            prod=[select name,Id,Description,Productcode from Product2 where Id=:id and Recordtypeid=:Product_Rtype]   
        
           dupe=[select Name,id from material__c where Name=:prod.Name];
           if(dupe.size()>0){
           dupFound= true;
            }

          else{
             mat=new material__c(Name=prod.name,Description__c=prod.Description);
             insert mat;
             Id matid=mat.id;
             return new pageReference('/'+matid);  
         }       
        }
}

APEX PAGE:

<apex:page Standardcontroller="product2" extension="ctrlCloneProductToMaterial">
<script type="text/javascript">
function alertMessage(){
   if ({!dupFound}== 'true'){
      var r = confirm ("existing material Do u want to continue");
       if(r== true){
              clone();
              alert('product cloned');          
             }  
        else{
             // return back to detail page of product record
              } 
     }
else{
              clone();
              alert('product cloned')   
       }
}
</script>

<apex:form>
<apex:acctionFunction action="{!cloneProduct}" name="clone" oncomplete="alertmessage()"/>
</apex:form>
</apex:page>

 
BalajiRanganathanBalajiRanganathan
The problem you have is, your javascript code will not change when the action function is called. The javascript code is compiled only one time when the page is loaded and the Javascript line  if ({!dupFound}== 'true'){ always will be false.

You can try to check whether duplicate exists or not using @remoteAction