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
o rajuo raju 

How to give Error messages Related to Metadata api object in salesforce?

Hi,

            Using metadata api i created object from visualforce page click save button.Again i try to create same object name from visualforce page using save button.
I got following error

mahesh.CreateObjectCls.MetadataServiceExamplesException: Error occured processing component MForm__c. The object name you specified already exists for your organization. You must specify a unique name across custom objects and article types. (DUPLICATE_DEVELOPER_NAME). Fields DeveloperName.
Error is in expression '{!createObject}' in component <apex:commandButton> in page bisoftsols:createobject

My requirement is when i click save button automatocally display error message as "Already exist your object name and  with yes and No buttons"
click Yes button:connect to another visualforce page
Click NO:Stay in that page only.
How to give please help me................
Best Answer chosen by o raju
o rajuo raju
Hi Tony,
                     Main thing is this url ./apex/FieldCreation?FN={!formname} is not added after pagereferece of the URL location.
give your mail i will send my code.......
help me...

All Answers

Tony TannousTony Tannous
Hello,

you need to surround the code inside the method createObject with a try {} catch, and the catch you can add the message that you want to the VF page and you need to set the 2 button visible .

step 1:Insert the below tag inside the visualforce page within page block

<apex:pageMessages ></apex:pageMessages>

step 2:

public PageReference createObject()
   {
      PageReference retVal= null;
   try
    {
 //  your code to create the object.
    }
   catch(exception ex)
    {
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Already exist your object name'));
     // make the 2 button visible 
    }
  }
 Regards



o rajuo raju
Hi Tony Tannous,
                                     Thank you for Reply.It's Working but noerror message.I wnat to display Messagebox as  'Already exist your object name' and this message box as two buttons one is YES and another one is NO

click YES button -->redirect to another visualforce page
click NO button-->redirect to same page

help me............
Tony TannousTony Tannous
add the below code in the VF page , at the end of  page before the </apex:form>

<script type="text/javascript">
      
        (function(){
        if({!IsError})
        {
            if (confirm("Already exist your object name!") == true)
            {
                window.open('your vf page');
            }
             else
             {
                x = "You pressed Cancel!";
             }
        }
        })();
    </script>

and now in the apex class you need to do the below changes:

1-add public Boolean IsError {get;set;}
2- public PageReference createObject()
   {
      PageReference retVal= null;
   try
    {
             //  your code to create the object.
    }
   catch(exception ex)
    {
       IsError= true;
    }
  }


but pay attention that this is not a best practice Salesforce , it is better to have a panel and to render the pannel with an error message and 2 buttons.

Good Luck
o rajuo raju
Hi ,

I don't get any message box and error also same as before only there is no change
           
  Here window.open('your vf page');



My visualforce page name like https://mahesh.ap1.visual.force.com/apex/FieldCreation?FN=MForm

Note:Here this name FN=MForm every time change Dynamically

help me sir
Tony TannousTony Tannous
Hello,

try this example :

<apex:page standardController="Account" extensions="VFC01AccExt" >
    <apex:form >
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!SaveAlert}" value="Save"/>
            </apex:pageBlockButtons>
      </apex:pageBlock>
       
        <script type="text/javascript">
        (function(){
        if({!IsError})
        {
             if (confirm("Press a button!") == true)
             {
              window.location.href ='../apex/FieldCreation?FN=MForm';
             } else
             {
               alert("please remove me just for test");
             }
        }
        })();
    </script>
   
    </apex:form>
</apex:page>


public with sharing class VFC01AccExt {

    public VFC01AccExt(ApexPages.StandardController controller) {

    }
   
    public Boolean IsError {get;set;}
   
    public PageReference SaveAlert()
    {
       PageReference retVal= null;
       IsError= true;
       return retVal;
    }

}


in your case you should have the same functionality.

Regards


o rajuo raju
Hi Tony Tannous,
                                       Error message will came but in my apex class object name is

pageReference ref=new pageReference('/apex/FieldCreation?FN=' + formName);

formname is a Object name in apex class (created object assign to this formname dynamically and open that object related URL)

How to assign this variable(form) to here

window.location.href ='../apex/FieldCreation?FN=MForm';//MForm is not fixed form name thats why i am asking.How to assign apex class variable to here


help me.....
     
 
Tony TannousTony Tannous
Hi,

you didn't mentioned that before in your question.

anyway you need to do the below :


<apex:page standardController="Account" extensions="VFC01AccExt" >
    <apex:form >
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!SaveAlert}" value="Save"/>
            </apex:pageBlockButtons>
<apex:inputText value="{!FormName}"/>
      </apex:pageBlock>
      <apex:input
        <script type="text/javascript">
        (function(){
        if({!IsError})
        {
             if (confirm("Press a button!") == true)
             {
              window.location.href ='../apex/FieldCreation?FN={!FormName}';
             } else
             {
               alert("please remove me just for test");
             }
        }
        })();
    </script>
  
    </apex:form>
</apex:page>

public with sharing class VFC01AccExt {

    public VFC01AccExt(ApexPages.StandardController controller) {

    }
  
    public Boolean IsError {get;set;}
   public string FormName {get;set;} 
    public PageReference SaveAlert()
    {
       PageReference retVal= null;
       IsError= true;
       return retVal;
    }

}

o rajuo raju
Hi,
          page success but Url location i gave URL is not added in an URL('../apex/FieldCreation?FN={!formname}') Location

window.location.href ='../apex/FieldCreation?FN={!formname}';//only first page Only not added next page url after click ok bottom

above url is not added and at a time two visualforce related codes edited a window.
Tony TannousTony Tannous
Try to disable development mode on the user, so the new page will open only.
the url will not change but the new page will open with the correct parameter.

Regards
o rajuo raju
Hi Tony,
                     Main thing is this url ./apex/FieldCreation?FN={!formname} is not added after pagereferece of the URL location.
give your mail i will send my code.......
help me...
This was selected as the best answer
o rajuo raju
Hi sir,
               below code one return method compulsory.
My Requirement is when i click save button  object is create and returns to another visualforce page.Suppose i gave same object name(before created object name) display one error message box as"already this object name there do you want to create fields(this is another visualforce page Like /apex/FieldCreation?FN={!FormName})" .Click YES page reference to FieldCreation page,Click NO  Page stay in that page only.


public with sharing class VFC01AccExt {

    public VFC01AccExt(ApexPages.StandardController controller) {

    }
 
    public Boolean IsError {get;set;}
   public string FormName {get;set;}
    public PageReference SaveAlert()
    {
       PageReference retVal= null;
       IsError= true;//Here only main problem i gave here this page not refer to another page
       return retVal;//Here return method compulsory (I gave pagereference )
    }

}

help me.......