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
Ankit Arora 29Ankit Arora 29 

Semi-Colon Separated Field, Store into multiple fields

Hi Everyone,
 
There is a Text field called Scholarship Name that has data in the form of SemiColon like aaaa;bbbb;cccc;dddd. 
This above length is not fixed, it can have one more value in it.
 
I am new in terms of coding and I tried to create a Trigger which is working in Sandbox. Can someone help me to optimize the code and also how to write a test class so that it can pass the validation test. This data in Scholarship name is going to be split and then compare it with the data in the Scholarship Object and fetch the descriptions and stored it in the Scholarship1, Scholarship2, Scholarship3 and Schorship4 fields. Please find below the code I have written. 
 
APEX TRIGGER: 
 
trigger OpportunityTrigger on Opportunity (before insert, before update) {
if(Trigger.isBefore){
if(Trigger.isInsert){
OpportunityScholarshipHandler.updateScholarCodesToNames(Trigger.New);
}
if(Trigger.isUpdate){
OpportunityScholarshipHandler.updateScholarCodesToNames(Trigger.New);
}
}
}

 
 -----------
 
APEX CLASS:
 
public class OpportunityScholarshipHandler {
public static void updateScholarCodesToNames(List<Opportunity> newOpp){
List<String> scholarCode = new List<String>();
List<Scholarship__c> scholarName = new List<Scholarship__c>();
for (Opportunity opp : newOpp){
if(opp.Scholarship_Name__c != NUll){
scholarCode = opp.Scholarship_Name__c.split(';');
}
scholarName = [Select Scholarship_Description__c from Scholarship__c where Scholarship_Code__c IN :scholarCode];
if(scholarName.size() == 4){
opp.Scholarships1__c = String.valueOf(scholarName[0].Scholarship_Description__c);
opp.Scholarships2__c = String.valueOf(scholarName[1].Scholarship_Description__c);
opp.Scholarships3__c = String.valueOf(scholarName[2].Scholarship_Description__c);
opp.Scholarships4__c = String.valueOf(scholarName[3].Scholarship_Description__c);
}
if(scholarName.size() == 3){
opp.Scholarships1__c = String.valueOf(scholarName[0].Scholarship_Description__c);
opp.Scholarships2__c = String.valueOf(scholarName[1].Scholarship_Description__c);
opp.Scholarships3__c = String.valueOf(scholarName[2].Scholarship_Description__c);
}
if(scholarName.size() == 2){
opp.Scholarships1__c = String.valueOf(scholarName[0].Scholarship_Description__c);
opp.Scholarships2__c = String.valueOf(scholarName[1].Scholarship_Description__c);
}
if(scholarName.size() == 1){
opp.Scholarships1__c = String.valueOf(scholarName[0].Scholarship_Description__c);
}
scholarCode.clear();
scholarName.clear();
}
}
}

 
FYI: There is no relationship between Scholarship and Opportunity objects.
 
Thank you
 
Regards,
Ankit
PriyaPriya (Salesforce Developers) 

Hey Ankit,

You are kindly requested to provide the attempted test class for the above code and highlight which part is not getting covered.

Thanks & regards,

Priya Ranjan

Ankit Arora 29Ankit Arora 29
@Priya
Do you know if this code that I have mentioned above will work if I push 100's of thousands of records? I mean I want to make sure I am not crossing governor limits but this coding is working without putting an DML operation, so I am kinda confused. I tink maybe because I am doing before Insert and update, that's why I don;'t need to write DML operation like Insert or Update. Do you know if I will SOQL Limit since I am using SOQL inside Opportunity Loop or can you suggest something else?
Thank you for your time.

Regards,
Ankit