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
Nkosilathi NcubeNkosilathi Ncube 

I am trying to extract first four or three characters in a string eg in UK postcodes "LS13 2ST or L10 2ST" to use in a trigger to lookup Counties by Postcodes. So I would be wanting only LS13 or L10 of the postcode to lookup county

Best Answer chosen by Nkosilathi Ncube
VineetKumarVineetKumar
String pincode;
String first3Char = pincode.substring(0, 4);
you can make the count (4) dynamic by containing the value in some variable and assigning value to it conditionally.

This would give you the required digits. For more methods on String refer the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

Let me know if that helped.

All Answers

VineetKumarVineetKumar
String pincode;
String first3Char = pincode.substring(0, 4);
you can make the count (4) dynamic by containing the value in some variable and assigning value to it conditionally.

This would give you the required digits. For more methods on String refer the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

Let me know if that helped.
This was selected as the best answer
Nkosilathi NcubeNkosilathi Ncube
Thanks Vineet, My question was for the following trigger which is not working for one of the substrings, eg it only works for the (0,4) and not the other, any help would be greatly appreciated
trigger TriggerUpdateContact on Contact(before insert, before update) {

    // querying all the name and Zipcode from County object
    List<PostCodes__c> countyList = [select Id, PostCode__c, County__c from PostCodes__c]; 
    Map<String,String > PostCodeMap = new Map<string,String>();
    
        //POPULATING THE MAP
    for(PostCodes__c pcode : countyList ){
        PostCodeMap.put(pcode.PostCode__c,pcode.County__c);

    }  
     for (Contact c :Trigger.new){ 
     
      string postcode2;
            if (c.MailingPostalCode.length()>= 4 )
                   postcode2 = c.MailingPostalCode.substring(0,4);
            else
                   postcode2= c.MailingPostalCode.substring(0,3);
     
       if(c.MailingPostalCode!=NULL && PostCodeMap.containsKey(postcode2)) {
      c.County__c = PostCodeMap.get(postcode2);
VineetKumarVineetKumar
I don't see any reason why it should not work unless on line 18 the mailing postal code length is < 2 or something.
Can you let me know what is output you are getting for the second one?
Nkosilathi NcubeNkosilathi Ncube
if (c.MailingPostalCode.length()>= 4 )

It works I had to change the length to >= 8 which is the full postcode as in LS13 2ST, 
VineetKumarVineetKumar
Cool.. Glad that it helped.
Do remember to help the community by marking the answer as the best answer, right below the comment.
Should it help anyone facing similar issues.