You need to sign in to do that
Don't have an account?
Abhinav_SFDC
whats wrong in my trigger ?? i hv create an auto number to every record type using text field...??
trigger Sample on Record_Type__c(before insert){
String[] names = new String[]{'First Record','Second Record','Third Record'};
Map<Id, RecordType> regions = new Map<Id,RecordType>([SELECT Name from RecordType WHERE SobjectType = 'Record_Type__c' AND Name IN :names]);
AggregateResult res = [SELECT MAX(Record_Number__c) max1, MAX(Record_Number1__c) max2,MAX(Record_Number2__c) max3 FROM Record_Type__c WHERE RecordType.Name IN :names group by RecordTypeId];
Integer max1 = Integer.valueOf(((String)res.get('max1')).right(3) );
Integer max2 = Integer.valueOf(((String)res.get('max2')).right(3) );
Integer max3 = Integer.valueOf(((String)res.get('max3')).right(3) );
for(Record_Type__c theRT: trigger.new){
if( regions.get(theRT.RecordTypeId).Name == 'First Record'){ //region corresponds to 'US'
String num1 = String.valueOf( (max1 != null) ? ++max1 : 1 );//will use the incremented value of max unless max was null (else use 1 instead)
for(Integer i=0; i< (3 - num1.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num1 = '0' + num1;
}
theRT.Record_Number__c= 'A - ' + num1;
}
else if( regions.get(theRT.RecordTypeId).Name == 'Second Record'){ //region corresponds to 'ROW'
String num2 = String.valueOf( (max2 != null) ? ++max2 : 1 );
for(Integer i=0; i< (3 - num2.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num2 = '0' + num2;
}
theRT.Record_Number1__c= 'S - ' + num2;
}
else if( regions.get(theRT.RecordTypeId).Name == 'Third Record'){ //region corresponds to 'ROW'
String num3 = String.valueOf( (max3 != null) ? ++max3 : 1 );
for(Integer i=0; i< (3 - num3.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num3 = '0' + num3;
}
theRT.Record_Number2__c= 'T - ' + num3;
}
}
}
==========================================================================================
when i create an record it shows an error====
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Sample caused an unexpected exception, contact your administrator: Sample: execution of BeforeInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.Sample: line 4, column 1
String[] names = new String[]{'First Record','Second Record','Third Record'};
Map<Id, RecordType> regions = new Map<Id,RecordType>([SELECT Name from RecordType WHERE SobjectType = 'Record_Type__c' AND Name IN :names]);
AggregateResult res = [SELECT MAX(Record_Number__c) max1, MAX(Record_Number1__c) max2,MAX(Record_Number2__c) max3 FROM Record_Type__c WHERE RecordType.Name IN :names group by RecordTypeId];
Integer max1 = Integer.valueOf(((String)res.get('max1')).right(3) );
Integer max2 = Integer.valueOf(((String)res.get('max2')).right(3) );
Integer max3 = Integer.valueOf(((String)res.get('max3')).right(3) );
for(Record_Type__c theRT: trigger.new){
if( regions.get(theRT.RecordTypeId).Name == 'First Record'){ //region corresponds to 'US'
String num1 = String.valueOf( (max1 != null) ? ++max1 : 1 );//will use the incremented value of max unless max was null (else use 1 instead)
for(Integer i=0; i< (3 - num1.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num1 = '0' + num1;
}
theRT.Record_Number__c= 'A - ' + num1;
}
else if( regions.get(theRT.RecordTypeId).Name == 'Second Record'){ //region corresponds to 'ROW'
String num2 = String.valueOf( (max2 != null) ? ++max2 : 1 );
for(Integer i=0; i< (3 - num2.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num2 = '0' + num2;
}
theRT.Record_Number1__c= 'S - ' + num2;
}
else if( regions.get(theRT.RecordTypeId).Name == 'Third Record'){ //region corresponds to 'ROW'
String num3 = String.valueOf( (max3 != null) ? ++max3 : 1 );
for(Integer i=0; i< (3 - num3.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num3 = '0' + num3;
}
theRT.Record_Number2__c= 'T - ' + num3;
}
}
}
==========================================================================================
when i create an record it shows an error====
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Sample caused an unexpected exception, contact your administrator: Sample: execution of BeforeInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.Sample: line 4, column 1
In the line number 4 you are getting more than one record,please make it a list and assign it as
list<AggregateResult> res = [SELECT MAX(Record_Number__c) max1, MAX(Record_Number1__c) max2,MAX(Record_Number2__c) max3 FROM Record_Type__c WHERE RecordType.Name IN :names group by RecordTypeId];
Thankyou