You need to sign in to do that
Don't have an account?
I have a trigger on product object
Where i need to split the value of one custom field and place it in the other custom field i have a developed a trigger
trigger ProductFieldUpdate on Product2 (Before insert, before update)
{
for(Product2 p : trigger.new) {
if(p.Ampics_PL__c != null && p.Ampics_PL__c == 'UTILUX') {
if(p.description != null && p.description.contains('/'))
{
String[] sstr = p.description.split('/');
p.Pieces_in_Pack__c = integer.valueof(sstr[1]);
}
else
p.Pieces_in_Pack__c = 1;
}
}
}
if i have a Management/12345 i was getting 12345 there are some other values like Management/12345(testing/123 i need to retrve the 2nd value in that case can any body help me
You can try like this
Integer lastIndx = p.description.lastIndexOf('/');
p.Pieces_in_Pack__c = p.description.subString(lastIndx);
#Note: If nothing is present after the number in the last
Hi surendra,
I have updated your trigger code as per your requirement.
Try to use following trigger code.
Apex trigger:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
Is the above code not working?
Do let me know if you face any issue on that.
it is not working if we doesnt hv any value it must take 1 and if we have any value it must take the value after '/' and if the record is similar like management/1234(testing/234 it must shows the o/p as 234 any help thanks there are 2 different kinds of recs where some vl be like management/1234 and management/1234(testing/231 we need to take the 231 value in the 2nd case any help
Change it like this
if(p.description.lastIndexOf('/') != NULL) {
Integer lastIndx = p.description.lastIndexOf('/');
p.Pieces_in_Pack__c = p.description.subString(lastIndx + 1);
}
else {
p.Pieces_in_Pack__c = 1;
}
was getting this error
Type cast it
if(p.description.lastIndexOf('/') != -1) {
Integer lastIndx = p.description.lastIndexOf('/');
p.Pieces_in_Pack__c = Decimal.valueOf(p.description.subString(lastIndx + 1));
}
else {
p.Pieces_in_Pack__c = Decimal.valueOf('1');
}
See it is returning string.
What is the datatype of Pieces_in_Pack__c?
type cast it accordingly.
trigger ProductFieldUpdate on Product2 (Before insert, before update){
for(Product2 p : trigger.new){
if(p.Ampics_PL__c != null && p.Ampics_PL__c == 'UTILUX') {
if(p.description.lastIndexOf('/') != NULL) {
Integer lastIndx = p.description.lastIndexOf('/');
p.Pieces_in_Pack__c = Decimal.valueOf(p.description.subString(lastIndx + 1));
}
else {
p.Pieces_in_Pack__c = 1;
}
}
}
}
trigger ProductFieldUpdate on Product2 (Before insert, before update){
for(Product2 p : trigger.new){
if(p.Ampics_PL__c != null && p.Ampics_PL__c == 'UTILUX') {
if(p.description.lastIndexOf('/') != NULL) {
Integer lastIndx = p.description.lastIndexOf('/');
p.Pieces_in_Pack__c = Decimal.valueOf(p.description.subString(lastIndx + 1));
}
else {
p.Pieces_in_Pack__c = 1;
}
}
}
}
Change it like this
trigger ProductFieldUpdate on Product2 (Before insert, before update){
for(Product2 p : trigger.new){
if(p.Ampics_PL__c != null && p.Ampics_PL__c == 'UTILUX') {
if(p.description.lastIndexOf('/') != -1) {
Integer lastIndx = p.description.lastIndexOf('/');
p.Pieces_in_Pack__c = Decimal.valueOf(p.description.subString(lastIndx + 1));
}
else {
p.Pieces_in_Pack__c = 1;
}
}
}
}
If the post helps you throw KUDOS. If this answers your question please mark it as solution.
ProductFieldUpdate: execution of BeforeUpdate
ProductFieldUpdate: execution of BeforeUpdate
caused by: System.TypeException: Invalid decimal: STEEL 520X4.6MM
Trigger.ProductFieldUpdate: line 18, column 1