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
Sushant Sharma 9Sushant Sharma 9 

Made trigger to copy field value, not working.

This is for my project. I created a field that would fetch a formula field's value and store it in. It was working fine until I changed the API name of one field, which was used in the formula of the formula field. I did update it, but it isn't working anymore. Help?

Error:
Assembly Number is a fomula field which is generated based on Assembly Digit 1, Assembly Digit 2,3 and Assembly Digit 4,5,6&7. It is working fine. And then i have added one trigger to copy the Assembly Number (or any of the four total such fields, any one of which would have any value) and store it in Part Number field. But it is copying only partially. On updating the record, it takes the other numeric half of the Assembly Number as it's value.
User-added image

Formula Code in Assembly Code formula field to generate Assembly Code:
IF(ISPICKVAL(Assembly_Digit_1__c, ""),'', CASE(Assembly_Digit_1__c,
"M - MECHANICAL",'M',
"E - ELECTRICAL",'E',
'X')) +

IF(ISPICKVAL(Assembly_Digit_2_3__c, ""),'', CASE(Assembly_Digit_2_3__c,
"01 - Guide Bracket Assy-Car",'01',
"02 - Guide Bracket Assy- Cwt/Combined",'02',
"03 - Tee Guide Assy-Car",'03',
"04 - Tee Guide Assy-Cwt",'04',
"05 - Guide Base Assy-Car",'05',
"06 - Guide Base Assy-Cwt",'06',
"07 - Landing Sill Assy",'07',
"08 - Landing Entrance Assy",'08',
"09 - Landing Door Assy",'09',
"11 - Emergency key",'11',
"12 - Counterweight Frame Assy",'12',
"13 - Concrete Fillers",'13',
"14 - Cast Iron Fillers",'14',
"15 - CWT Guide Shoe Assy",'15',
"16 - Eye bolt Assy - Cwt",'16',
"17 - Cwt Divertor Assy",'17',
"18 - CWT Guard Assy",'18',
"19 - Machinery Unit Assy",'19',
"20 - Isolation Rubber",'20',
"21 - Machine Room Divertor Assy",'21',
"22 - Car Sling Assy",'22',
"23 - Car Guide Shoe Assy",'23',
"24 - Car Suspension Assy",'24',
"25 - Car Divertor Assy",'25',
"26 - Suspension Rope Assy",'26',
"27 - Car Body Assy",'27',
"28 - Car gate contact fixing bracket Assy",'28',
"29 - Car gate contact Assy",'29',
"30 - Car door/Cabin Assy",'30',
"31 - Toe Guard",'31',
"32 - OSG Assy",'32',
"34 - OSG Rope Assy",'34',
"35 - Controller Assy",'35',
"36 - Wiring Materials",'36',
"37 - Buffer Spring-Car",'37',
"38 - Buffer Spring-Cwt",'38',
"39 - COP Assy",'39',
"40 - Retiring Cam & Bracket Assy",'40',
"41 - TLS Assy",'41',
"42 - TLS Cam Assy",'42',
"43 - Name Plate Assy",'43',
"44 - Cabin Light Assy",'44',
"45 - Inspection Panel Assy",'45',
"46 - Oscillator Switch",'46',
"47 - LPB Assy",'47',
"48 - Reed Switch Assy",'48',
"49 - Voice announciator",'49',
"50 - Machine Beams",'50',
"54 - D&P Indicator Assy",'54',
"55 - Fireman Switch Assy",'55',
"57 - Car Balance Weight & Fixing Assy",'57',
"58 - Automatic Rescue Device Assy",'58',
"59 - Floor Announciator",'59',
"62 - Seperator Channel",'62',
"63 - Power Door Safety Assy",'63',
"65 - Emergency Light Assy",'65',
"66 - Instructions Display Boards",'66',
"67 - Emergency Alarm Box",'67',
"68 - Consumables",'68',
"69 - Tools",'69',
"70 - Fasteners",'70',
"71 - PPE items",'71',
"72 - Pit",'72',
"73 - Compensating Chain",'73',
"74 - Packing Materials",'74',
"75 - Machine Room",'75',
"76 - Flame Proof or Explosion Proof",'76',
"77 - Scrap Boughtout",'77',
"78 - Scrap Manufacturing",'78',
"79 - Structure",'79',
'00')) +

IF(ISBLANK(Assembly_Digit_4_5_6_7__c),'', Assembly_Digit_4_5_6_7__c)
Trigger to update Part Number field:
trigger UpdatePartNumber on Part_Number_Creation__c (before insert, before update) {
    for(Part_Number_Creation__c PartList: trigger.new){
        if(PartList.IndividualPartNumber__c!=null){
            PartList.Part_Number__c=PartList.IndividualPartNumber__c;
        } else if(PartList.AssemblyNumber__c!=null){
            PartList.Part_Number__c=PartList.AssemblyNumber__c;
        }else if(PartList.KitNumber__c!=null){
            PartList.Part_Number__c=PartList.KitNumber__c;
        }else if(PartList.RawMaterialNumber__c!=null){
            PartList.Part_Number__c=PartList.RawMaterialNumber__c;
        }else{
            PartList.Part_Number__c='';
        }
    }
}

i am confused as to why is this kind of error is happening. This seem to be logically right. Help anyone?

 
karthikeyan perumalkarthikeyan perumal
Hello sushant, 

try this code.  meanwhile try to provide the values for 
IndividualPartNumber__c
AssemblyNumber__c----No needed. 
KitNumber__c
RawMaterialNumber__c

also make system.debug  in every  if condition. and let me know the value.
 
trigger UpdatePartNumber on Part_Number_Creation__c (before insert, before update) {
    for(Part_Number_Creation__c PartList: trigger.new){
        if(PartList.IndividualPartNumber__c!=null){
            PartList.Part_Number__c=PartList.IndividualPartNumber__c;
        }  
		if(PartList.AssemblyNumber__c!=null){
            PartList.Part_Number__c=PartList.AssemblyNumber__c;
        } 
		if(PartList.KitNumber__c!=null){
            PartList.Part_Number__c=PartList.KitNumber__c;
        } 
		if(PartList.RawMaterialNumber__c!=null){
            PartList.Part_Number__c=PartList.RawMaterialNumber__c;
        } esle {
            PartList.Part_Number__c='';
        }
    }
}

hope this will give some idea to resolve this issue. 

Thanks
karthik
​​​​​​​
 
karthikeyan perumalkarthikeyan perumal
hello sushant, 

sorry i forget to edit the else line code.  please use this code to  please look at the else part to set the condition to set null value. 
 
trigger UpdatePartNumber on Part_Number_Creation__c (before insert, before update) {
    for(Part_Number_Creation__c PartList: trigger.new){
        if(PartList.IndividualPartNumber__c!=null){
            PartList.Part_Number__c=PartList.IndividualPartNumber__c;
        }  
		if(PartList.AssemblyNumber__c!=null){
            PartList.Part_Number__c=PartList.AssemblyNumber__c;
        } 
		if(PartList.KitNumber__c!=null){
            PartList.Part_Number__c=PartList.KitNumber__c;
        } 
		if(PartList.RawMaterialNumber__c!=null){
            PartList.Part_Number__c=PartList.RawMaterialNumber__c;
        } 
		
		if( condition to check to set null value ) {  (----- Please set this condition first)
            PartList.Part_Number__c='';
        }
    }
}