You need to sign in to do that
Don't have an account?

Apex trigger using apex class
i have a custom field(Vendor_Rep__c) that i am trying tp update automatically when a record(Member Vendor) is created or edited.
I have created visualforce page, which i am starting to believe that i no longer need to acheive what i need.
Also, with some help, i have created the following code for an Apex Class:
public class contactsearch {
public Contact searchContact(string ContactType, string MemberState, String vendorId)
{
return[Select Name From Contact Where Contact_Type__c = 'Vendor Rep' AND States__c = :MemberState AND Vendor__c = :vendorId limit 1];
}
}
Lastly, after reading several questions already posted and a few Apex Developer Guide articles, i created the trigger below.
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before insert, after update) {
for (Member_Vendor_del__c u : trigger.new);{
u.Vendor_Rep__c = null;
}
}
What i want to do is only if Vendor_Rep__c is blank/null, have the trigger use the parameters from the apex class to return with the name of the contact into the Vendor_Rep__c field. I want to eventually add in an additional factor to check against, the name of the county the contact services vs. the account's county.
I have created visualforce page, which i am starting to believe that i no longer need to acheive what i need.
Also, with some help, i have created the following code for an Apex Class:
public class contactsearch {
public Contact searchContact(string ContactType, string MemberState, String vendorId)
{
return[Select Name From Contact Where Contact_Type__c = 'Vendor Rep' AND States__c = :MemberState AND Vendor__c = :vendorId limit 1];
}
}
Lastly, after reading several questions already posted and a few Apex Developer Guide articles, i created the trigger below.
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before insert, after update) {
for (Member_Vendor_del__c u : trigger.new);{
u.Vendor_Rep__c = null;
}
}
What i want to do is only if Vendor_Rep__c is blank/null, have the trigger use the parameters from the apex class to return with the name of the contact into the Vendor_Rep__c field. I want to eventually add in an additional factor to check against, the name of the county the contact services vs. the account's county.
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before update) {
for (Member_Vendor_del__c u : trigger.new){
if (u.Vendor_Rep__c == null){
u.Vendor_Rep__c = [Select Id,Name
From Contact
Where Contact_Type__c = 'Vendor Rep'
AND States__c = :u.Member_State__c
AND Contact_Vendor_ID__c = :u.Vendor_ID__c limit 1].Id;
}
}
}
This allows my Vendor_Rep__c field to auto update with the name of the contact that fits the criteria in my SOQL query as a lookup.
All Answers
Let us know if this will help you
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before insert, before update) {
for (Member_Vendor_del__c u : trigger.new){
if (u.Vendor_Rep__c == null){
contactsearch.searchContact(contact.Contact_Type__c, member_vendor_del__c.Member_State__c, member_vendor_del__c.Vendor_ID__c);{
update [Select contact.Name
From Contact
Where Contact_Type__c = 'Vendor Rep' AND States__c = :member_vendor_del__c.Member_State__c AND Contact_Vendor_ID__c = :member_vendor_del__c.Vendor_ID__c limit 1];
}
}
}
}
I am getting the following errors for the trigger:
Line 5 - Method does not exist or incorrect signature: void searchContact(Schema.SObjectfield,Schema.SObjectfield,Schema.SObjectfield) from the type contactsearch
Line 6 - Invalid bind expression type of Schema.SObjectfieldfor column of type String
I had to make an adjustment to my original Apex Class(No Errors):
public class contactsearch {
public Contact searchContact(string ContactType, string MemberState, String VendorID)
{
return[Select Name From Contact
Where Contact_Type__c = 'Vendor Rep' AND States__c = :MemberState AND Contact_Vendor_ID__c = :VendorID limit 1];
}
}
Is there an issue with Vendor_Rep__c being a lookup field? Maybe that it also has a filter to only lookup certain account types?
I have again modified my trigger but am still getting an error message.
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before insert, before update) {
for (Member_Vendor_del__c u : trigger.new){
if (u.Vendor_Rep__c == null){
member_vendor_del__c.Vendor_ID__c);{
u.Vendor_Rep__c = [Select Name
From Contact
Where Contact_Type__c = 'Vendor Rep' AND States__c = :member_vendor_del__c.Member_State__c AND Contact_Vendor_ID__c = :member_vendor_del__c.Vendor_ID__c limit 1];
}
}
}
The Error message(Line 6):
Invalid bind expression type of Schema.SObjectField for column of type String
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before update) {
for (Member_Vendor_del__c u : trigger.new){
if (u.Vendor_Rep__c == null){
u.Vendor_Rep__c = [Select Id,Name
From Contact
Where Contact_Type__c = 'Vendor Rep'
AND States__c = :u.Member_State__c
AND Contact_Vendor_ID__c = :u.Vendor_ID__c limit 1].Id;
}
}
}
This allows my Vendor_Rep__c field to auto update with the name of the contact that fits the criteria in my SOQL query as a lookup.