+ Start a Discussion
k practicek practice 

URGENT:More than one contact in an object how to display error message using standard button?

Hi,
I tried like below but its not working
BUTTON CODE:
----------
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}
var result = sforce.apex.execute('MyClass', 'myMethod', {Accountid:'{!Account.Id}'});
Alert('More than one contact not allowed'+result);
Class:
======
 webService static String myMethod(ID Accountid) {
String BusinessAccountid = Businessaccount.id;
String Errorstring='More than one contact not allowed';
List<Contact> checkBusinessAccountContactList=[SELECT id FROM Contact WHERE Accountid = :BusinessAccountid];
                System.debug(checkBusinessAccountContactList);
                Account BusinessAccountrelatedContacts=new Account();
                if(checkBusinessAccountContactList.size() > 1)
                {
                   return Errorstring;
                }

}
help me..
Best Answer chosen by k practice
Suneel#8Suneel#8
It will show up in the popup dialog box.Try below code
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}
var result = sforce.apex.execute('MyClass', 'myMethod', {Accountid:'{!Account.Id}'});
alert(result);

 

All Answers

Suneel#8Suneel#8
What is Businessaccount.id?? Are you getting any error message?If so can you post the same? Condition should be checkBusinessAccountContactList.size() >= 1 . Isn't it?
k practicek practice
Hi Suneel,
                    Actually i try to convert Business account to Personal account using button.When user click this button it will check Contacts in business account.Suppose contacts more than one in Business account i will through an error.
1)Businessaccount.id-->This is Accountid(I passed in Button creation time that id)
2)I get Error but its displaying every time
3)When this conditon will reach its not goto next line

please help me..


 
Suneel#8Suneel#8
I see that it is working with following code.Can you check that
Button Code
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}
var result = sforce.apex.execute('MyClass', 'myMethod', {Accountid:'{!Account.Id}'});
alert('More than one contact not allowed'+result);//modified
Class Code
global class MyClass{
     webService static String myMethod(ID Accountid) {
        String BusinessAccountid = Accountid;//Modified
        String Errorstring='More than one contact not allowed';
        List<Contact> checkBusinessAccountContactList=[SELECT id FROM Contact WHERE Accountid = :BusinessAccountid];
                System.debug(checkBusinessAccountContactList);
                Account BusinessAccountrelatedContacts=new Account();
                if(checkBusinessAccountContactList.size() > 1)
                {
                   return Errorstring;
                }
            return 'Success';
        }
}


 
k practicek practice
First i gave 2 contacts its working after i removed 1 contact on that time also this error message displayed and next lines will be executed.
How to stop error message 1 contact time..
Suneel#8Suneel#8
You have given -'More than one contact not allowed'+result - in the alert dialog.So everytime it is displayed.Check for result variable.That should give you the exact message you are looking for
k practicek practice
Hi Suneel,
                        How to check result variable?
Suneel#8Suneel#8
It will show up in the popup dialog box.Try below code
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}
var result = sforce.apex.execute('MyClass', 'myMethod', {Accountid:'{!Account.Id}'});
alert(result);

 
This was selected as the best answer
k practicek practice
Thank you suneel Its working

Before i asked one question you know .I deleted you know please help me...

 
Suneel#8Suneel#8
Sorry,I a not able to understand your requirement.Try using System.debug at some checkpoints and find where the issue is.
k practicek practice
Hi,
Here i was getting Contact object Custom fields.If suppose one custom field is not there in below comparing object(Person object).How to avoid this custom fields and same fields how to update?
  for (Schema.SObjectField objField: Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().values()) {
                DescribeFieldResult fr = objField.getDescribe();
                if (fr.isUpdateable()) //isCreateable())
                {
                    if (fr.getName().endsWith('__c')) {
                        String str = fr.getName();
                        String contactcustomFields = str.replaceAll('__c', '__pc');
                        System.debug(contactcustomFields);
                        pAccount.put(contactcustomFields, Ocontact.get(str));
                        //update pAccount;
                        System.debug(pAccount.put(contactcustomFields, Ocontact.get(str)));
                    }
                
                }
            }

Help me.....
Suneel#8Suneel#8
Can you create a separate post for this if you still didn't get the solution yet.It is confusing for others to follow if you use the same post for all of your queries.Also if you create a new post,everyone will check it as it is an unsolved question