You need to sign in to do that
Don't have an account?
Jaynandan Prasad 8
trigger to change the record type
Wrote a trigger to change the record type after a field getting populated in the same object. Its getting saved, but not working.
trigger RecordTypeMap on Case (before insert) {
Map<Id, String> typeMap = New Map<Id, String>();
for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Case']) {
typeMap.put(rt.Id,rt.DeveloperName);
}
for (Case cc : trigger.new) {
if (cc.Master_Record_Type__c != null) {
id recid = typeMap.get(cc.Master_Record_Type__c);
recordtype rectype = [select id, developername from recordtype where id=:recid];
cc.RecordTypeid = rectype.id;
}
}
}
trigger RecordTypeMap on Case (before insert) {
Map<Id, String> typeMap = New Map<Id, String>();
for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Case']) {
typeMap.put(rt.Id,rt.DeveloperName);
}
for (Case cc : trigger.new) {
if (cc.Master_Record_Type__c != null) {
id recid = typeMap.get(cc.Master_Record_Type__c);
recordtype rectype = [select id, developername from recordtype where id=:recid];
cc.RecordTypeid = rectype.id;
}
}
}
Thanks
Manoj
All Answers
What is Master_Record_Type__c in case what is value you are storing in that field ?
I am doing a salesforce to saleforce connection on Case object. Now as Record type cannot be synched in S2S connection, I created a custom text field - Master_Record_Type__c in the target org which will hold the record type name from the source Org. Once this field is populated with the String value i.e record type name from the source Org..it should change the current record type in the Target org accordingly..
Try with below it may help but still you have query in for loop it wil be issue .
Let me know if it helps
Thanks
Manoj
Please try below code :-
If you are record Type Name in "Master_Record_Type__c" field then please add below code in above code
NOTE :- Never use SOQL inside FOR loop like below :-
Please let me know if this will help you.
Normally, We can get the Recordtype names for any sObject by using SOQL
[SELECT ID,Name, SobjectType from Recordtype WHERE SobjectType='MyObject__c' AND Name = 'MyRecordTypeName']
But it will count against your SOQL limit:
Below method will bypass the need of SOQL Query Governing LIMIT.
So you can use below complete code for trigger:
Regards
Mohit Bansal
In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.