You need to sign in to do that
Don't have an account?
JAW99
Trigger Help
I'd like some help or to be directed to a helpful source on writing a simple trigger, my first that wasn't copied.
What i want to do is before insert, have leads of a specified record type put in the value "individual" for company name, to get around the requirements for a company name.
Any pointers much appreciated.
trigger insertLeadCompany on Lead (before insert)
{
String recID = [SELECT Id FROM RecordTYpe WHERE Name='specificrectype'].Id;
for(Integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].RecordTypeId == recID )
Trigger.new[i].CompanyName = 'Individual';
}
}
hey hope this will do it for you . I have not tested it, tell me if there is some problem.
Here's the error I got:
insertLeadCompany: execution of BeforeInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Trigger.insertLeadCompany: line 3, column 20
here's the code
trigger insertLeadCompany on Lead (before insert)
{
String recID = [SELECT Id FROM RecordType WHERE Name='012400000009bKL'].Id;
//here i guess name should not be like this, it might be the recordtypid u r using. then u caN directly take it as recId='012400000009bKL' no need to query. if it is always the same
for(Integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].RecordTypeId == recID )
Trigger.new[i].Company = 'Individual';
}
}
OK, works for an upload, but what I really wanted was to make so that when a person enters a new lead of the specified record type, the field company would be filled with "individual" for them when they hit save.
Is there a way i can do that?
trigger insertLeadCompany on Lead (before insert)
{
String recID = [SELECT Id FROM RecordType WHERE Name='yourrecordtypegoeshere' and sObjectType='Lead'].Id;
for(Integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].RecordTypeId == recID )
Trigger.new[i].Company = 'Individual';
}
}
Ah, thanks! but not quite... still get the "company is required" type message.
trigger insertLeadCompany2 on Lead (before insert)
{
String recID = [SELECT Id FROM RecordType WHERE Name='012R0000000CrGL' and sObjectType='Lead'].Id;
for(Integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].RecordTypeId == recID )
Trigger.new[i].Company = 'Individual';
}
}
man, thanks for your help on this.
OK, just ran it again and now it gets the error when the company is blank but will convert a dummy company name to "Individual."