function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Surendra123Surendra123 

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

souvik9086souvik9086

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

hitesh90hitesh90

Hi surendra,

 

I have updated your trigger code as per your requirement.

 

Try to use following trigger code.


Apex 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[sstr.size()-1]);
			}
			else
				p.Pieces_in_Pack__c = 1;
		}
	}
}

 

 

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/

Surendra123Surendra123
The trigger is running bt unable to fetch the last value see there are 2 cases where the value of prod description will be like management/1234---> 1234 i need to fetch the value there are some products like management/1234(testing/123--->123 i need to fetch 123 for the records which are clubed any help thanks
souvik9086souvik9086

Is the above code not working?

Do let me know if you face any issue on that.

 

Surendra123Surendra123
@souvik

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
souvik9086souvik9086

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;

}

Surendra123Surendra123
Error: Compile Error: Illegal assignment from String to Decimal at line 6 column 13
was getting this error
souvik9086souvik9086

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

}

Surendra123Surendra123
i tried but it is not support integer value of
souvik9086souvik9086

See it is returning string.

What is the datatype of Pieces_in_Pack__c?

 

type cast it accordingly.

Surendra123Surendra123
That is working but i have a query if there is no value something like Management/123 and if it only management it must take default value as 1 for tht i have included the code

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;
}
}
}
}
Surendra123Surendra123
That is working but i have a query if there is no value something like Management/123 and if it only management it must take default value as 1 for tht i have included the code

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;
}
}
}
}
souvik9086souvik9086

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.

Surendra123Surendra123
Hi Thanks i have exported all the records using data loader and when i try to update them i ws getting the following error iam unable to insert the records any help when im entering the data trigger is firing correctly
ProductFieldUpdate: execution of BeforeUpdate
Surendra123Surendra123
This was the error iam facing any help
ProductFieldUpdate: execution of BeforeUpdate

caused by: System.TypeException: Invalid decimal: STEEL 520X4.6MM

Trigger.ProductFieldUpdate: line 18, column 1