You need to sign in to do that
Don't have an account?
Insert and Update according to Record Type name
Hi,
I am trying to code down Apex script for the case when the account is of particular record type, insert and update some fields. I have done via hard coding record-type id field value and it succeeded in inserting and updating field values but still I want to generalise the code so that the same can be used in all sandboxes and organisations (I may be wrong but from what I have heard and learned, recrod type ids vary in sandbox and production environment for same record type, please let me know if I am wrong).
I have attempted to write the code as shown below. But still it does not work as required(does not insert/update records as per condition of Record Type name).
trigger AccRecTypeMatchAutoPopulateFieldValues on Account (before insert, before update)
{
List<Id> AccIds = new List<Id>();
String Flag;
if(Trigger.isInsert)
{
Id RecId = [Select Id, Name from RecordType where name = 'rec_type1' limit 1].Id;
Account[] newAccs = Trigger.new;
for(Integer i=0; i < newAccs.size(); i++)
{
if(Trigger.new[i].RecordTypeId == RecId)
//for(Account newAccs : Trigger.new)
{
// if(newAccs.RecordTypeId == '01290000000ChWo')
//
newAccs[i].Website = 'http://www.tcs.com';
newAccs[i].BillingStreet = 'Yantra Park';
//a.BillingCity = 'Mumbai';
newAccs[i].BillingState = 'Maharashtra';
newAccs[i].BillingCountry = 'India';
newAccs[i].Fax = '223120';
newAccs[i].Phone = '2230175';
Flag = 'Y';
AccIds.add(newAccs[i].Id);
}
}
if(Flag == 'Y')
{
Account[] accs = [select Id, name from Account where Id in :AccIds];
insert accs;
}
}
if(Trigger.isUpdate)
{
Id RecId = [Select Id, Name from RecordType where name = 'rec_type1' limit 1].Id;
Account[] oldAccs = Trigger.new;
for(Integer i=0; i < oldAccs.size(); i++)
{
if(Trigger.new[i].RecordTypeId == RecId)
//for(Account newAccs : Trigger.new)
{
// if(newAccs.RecordTypeId == '01290000000ChWo')
//
oldAccs[i].Website = 'http://www.tcs.com';
oldAccs[i].BillingStreet = 'Yantra Park';
//a.BillingCity = 'Mumbai';
oldAccs[i].BillingState = 'Maharashtra';
oldAccs[i].BillingCountry = 'India';
oldAccs[i].Fax = '223120';
oldAccs[i].Phone = '2230175';
Flag = 'Y';
AccIds.add(oldAccs[i].Id);
}
}
if(Flag == 'Y')
{
Account[] accs = [select Id, name from Account where Id in :AccIds];
update accs;
}}
}
Looks like I have finally solved the problem by generalising it too!
Thanks for all your help Kirtesh and Mak!
This is the code I wrote and fortunately this time it tasted success after attempting 7-8 times of recoding!
Do let me know if it is perfect in all aspects(Governing rules, etc)
Finally I am atleast becoming Apex Coder in real sense(atleast achieved 10% of its knowledge if not more.. lols..)
All Answers
Hi,
You are on right track. SalesForce returns 18 character ids.
You can replace it with 18 character or truncate it to 15, you code will work.
Thanks Naishadh for your response and clearing my dilemma regarding recordtype id value cloning in production environment too.
However, was just curious to know if there is any way out to generalise this irrespective of taking record type id value into consideration.
Thanks again!
-Vimal
Why are you comparing Id ..Write method to retreve Id associated with RecordType Name
Thanks
Kirtesh
Hi Kirtesh,
That is what I want to know exactly how can we achieve it? :)
Thanks,
-Vimal
I think Kirtesh is right here. When you need to do specific tasks based on record type, you can use record type names.
for the new record, get the record name with this query:
Select r.Name, r.Id From RecordType r where id = :newAccs.RecordTypeId
if( r.Name == 'something')
//do your operations here
As you are not hardcoding any ids, this code won't have problems when it's deployed to production.
Hi Mak,
Thanks for your response!
Just a quick question: Whom should I assign this query to?
Select r.Name, r.Id From RecordType r where id = :newAccs.RecordTypeId
I know its a stupid question, but please bear it as I am still new to Apex Triggers.... :))
Thanks!
-Vimal
Looks like I have finally solved the problem by generalising it too!
Thanks for all your help Kirtesh and Mak!
This is the code I wrote and fortunately this time it tasted success after attempting 7-8 times of recoding!
Do let me know if it is perfect in all aspects(Governing rules, etc)
Finally I am atleast becoming Apex Coder in real sense(atleast achieved 10% of its knowledge if not more.. lols..)