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
sairam v 9sairam v 9 

Create an Apex class that returns contacts based on incoming parameters.?

I tried with below code but i am getting some error message .

Error Message
public with sharing class ContactSearch {
    public static List<Contact> searchForContacts(String lastName,  String postalCode) {
        return [ select Name from Contact where LastName = :lastName and  MailingPostalCode = :postalCode ];
    }
}
 
Best Answer chosen by sairam v 9
Dilip_VDilip_V
Sairam,

How about trigger?Any active triggers on contact object?

Try this code
public class ContactSearch {
    Public static List<Contact> searchForContacts(String name,string PosatalCode)
    {
        List<Contact> Contacts=new List<Contact>();
        Contacts=[select id,name from contact where name=:name or MailingPostalCode=:PosatalCode];
        return Contacts;
    }
}



Thanks.

All Answers

Dilip_VDilip_V
Hi Sairam,

It seems you have custom validation rule on the Contact.

Please check validation rules on contact with error message 'Hey!You are not authorised to perform this action'.

Deactivate it and try again.

Thanks.
sairam v 9sairam v 9
Hi Sony Piyush,
I tried your code seems it showing same error message .
Hi Dilip,
In my org no Validation rules on  Contacts object.
Dilip_VDilip_V
Sairam,

How about trigger?Any active triggers on contact object?

Try this code
public class ContactSearch {
    Public static List<Contact> searchForContacts(String name,string PosatalCode)
    {
        List<Contact> Contacts=new List<Contact>();
        Contacts=[select id,name from contact where name=:name or MailingPostalCode=:PosatalCode];
        return Contacts;
    }
}



Thanks.
This was selected as the best answer
sairam v 9sairam v 9
Thanks for your suppor Dillip veluri,Sony Piyush.


I didn't inactive related trigger .
 
Veena GopalVeena Gopal
how can i display a list in my code? The below code does not return a display when i debug
public class ContactSearch {
    
    public static List<Contact> searchForContacts(String lastnm, String postcd){
   
                List<Contact> cont = New List<Contact>();
        cont = [Select Id, Name from Contact 
                              WHERE (LastName =:lastnm and MailingPostalCode =:postcd)];
        for (contact c:cont){
            system.debug(c);
        }
                return cont;
    }
}
sfdcMonkey.comsfdcMonkey.com
hi Veena Gopal
for see the result of above apex class --> go to developer console --> debug--> open Execute Anonymouse window(crtl + E)
call you class static method by
ContactSearch.searchForContacts('Barr', '123456');
make sure you have a contact with last name 'Barr' and mailing postal code is '123456'. or you have any other contact.
execute this line and then check debug log wher you can see the result return by this apex class  method
User-added image
******
User-added image
****
User-added image
thanks hope it helps you :)
http://sfdcmonkey.com


 
Veena GopalVeena Gopal
hi piyush_soni, what if i have blanks in postal code field? i gave the following
ContactSearch.searchForContacts('ram', ' ');
Veena GopalVeena Gopal
even if i give valid value in postal code it gives me this screen. it still does not give me valuesdebug screen
Veena GopalVeena Gopal
no it worked. Thanks piyush_soni
sfdcMonkey.comsfdcMonkey.com
sounds good @Veena Gopal :)
http://sfdcmonkey.com