+ Start a Discussion
JAW99JAW99 

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.

 

Srinivas_V2Srinivas_V2

 

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.

Message Edited by Srinivas_V2 on 04-03-2009 12:42 AM
JAW99JAW99

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;
for(Integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].RecordTypeId == recID )

Trigger.new[i].Company = 'Individual';
}

}
Srinivas_V2Srinivas_V2

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';

}

}

JAW99JAW99
yes! thanks.
JAW99JAW99

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?

Srinivas_V2Srinivas_V2

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';

}

}

JAW99JAW99

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."

Message Edited by JAW99 on 04-06-2009 10:59 AM