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
civiccivic 

Trigger problem

Hi All,

 

Currently i have a  Trigger that removes line break in Mailing Address then copies cleaned values to custom text fields.

It works fine with a single record but when i want to update more records at a time with dataloader it doesn't work.

I think i need to bulkify it.. can anybody pls send me the code which should work in all scenarios....

Here is the code..

 

trigger contactRemoveLineBreakTrigger on Contact (before insert, before update) 
{
  List <String> addClean = new List<String>();
  
  for (Contact a: Trigger.new)
  {
    if(a.MailingStreet!=null)
    {
      for(String s: a.MailingStreet.split('\n',0))
      {
        addClean.add(s);
        system.debug('DEBUG: '+ addClean.size());
      }
      
      if(addClean.size()!=null)
      {
        if(addClean.size()==1)
        {
          a.Shipping_Street_1_Clean__c = addClean[0];
          a.Shipping_Street_2_Clean__c = '';
          a.Shipping_Street_3_Clean__c = '';
        }
        else if(addClean.size()==2)
        {
          a.Shipping_Street_1_Clean__c = addClean[0];
          a.Shipping_Street_2_Clean__c = addClean[1];
          a.Shipping_Street_3_Clean__c = '';
        }
        else if (addClean.size()>=3)
        {
          a.Shipping_Street_1_Clean__c = addClean[0];
          a.Shipping_Street_2_Clean__c = addClean[1];
          a.Shipping_Street_3_Clean__c = addClean[2];
        }
      }
    }
    else
    {
      a.Shipping_Street_1_Clean__c = '';
      a.Shipping_Street_2_Clean__c = '';
      a.Shipping_Street_3_Clean__c = '';
    }
  }
}

zachelrathzachelrath
Move List<String> addClean = new List<String>(); inside the for loop.
trigger contactRemoveLineBreakTrigger on Contact (before insert, before update) 
{
  for (Contact a: Trigger.new)
  {
    if(a.MailingStreet!=null)
    {
      List <String> addClean = new List<String>();

      for(String s: a.MailingStreet.split('\n',0))
      {
        addClean.add(s);
        system.debug('DEBUG: '+ addClean.size());
      }
      
      if(addClean.size()!=null)
      {
        if(addClean.size()==1)
        {
          a.Shipping_Street_1_Clean__c = addClean[0];
          a.Shipping_Street_2_Clean__c = '';
          a.Shipping_Street_3_Clean__c = '';
        }
        else if(addClean.size()==2)
        {
          a.Shipping_Street_1_Clean__c = addClean[0];
          a.Shipping_Street_2_Clean__c = addClean[1];
          a.Shipping_Street_3_Clean__c = '';
        }
        else if (addClean.size()>=3)
        {
          a.Shipping_Street_1_Clean__c = addClean[0];
          a.Shipping_Street_2_Clean__c = addClean[1];
          a.Shipping_Street_3_Clean__c = addClean[2];
        }
      }
    }
    else
    {
      a.Shipping_Street_1_Clean__c = '';
      a.Shipping_Street_2_Clean__c = '';
      a.Shipping_Street_3_Clean__c = '';
    }
  }
}

 

civiccivic

Thats ok. but how do we bulkify the same trigger....

Thx for the response

zachelrathzachelrath

Nothing more to do --- because you're iterating over all Contacts in Trigger.new, and operating on each one individually, your trigger is bulkified.

civiccivic

When i changed as per ur suggestion it is updating the values, but only the first line is updated and not the 2nd and 3rd.

How should i achieve this functionality.

Thx in advance.

 

zachelrathzachelrath

By "only the first line is updated", do you mean that Shipping_Street_2__c and Shipping_Street_3__c are never getting populated?

 

I haven't heard of people having much success separating the Street Address fields into separate lines by using split('\n'). Apparently Salesforce actually stores Street Addresses as 3 separate lines (Address Line 1, Address Line 2, Address Line 3), and then compiles them into 1 text string whenever the field is requested (see this thread). However, you CAN get at these individual Address Lines through Reports. So, you could do an export on all of your Contacts, exporting the 3 separate Address Lines, and then copy those values into your Custom Fields.

zachelrathzachelrath

Actually, I take that back --- I created a Contact with a 4-line MailingStreet, and was able to use split('\n') on it to divide it into 4 parts no problem. 

civiccivic

I am sorry if i was wrong earlier.

 

Here is my Requirement.

 

I have Mailing address field which has mailing street,mailing city,mailing state/province,zipcode,Country.

If i want to update the shipping streeet 1 with mailing street and shipping street 2 with mailing city and state, shipping street 3 with Zipcode and country.

What should be the code....

Pls help...

zachelrathzachelrath
trigger contactRemoveLineBreakTrigger on Contact (before insert, before update) 
{
  for (Contact a: Trigger.new)
  {
  		// Populate Shipping Street 1 with the value of Mailing Street
		if (a.MailingStreet != null) 
			a.Shipping_Street_1_Clean__c = a.MailingStreet;
		else a.Shipping_Street_1_Clean__c = '';
		
		// Populate Shipping Street 2 with Mailing City and Mailing State,
		// but only if BOTH are non-null (I'm not sure if this is what you want)
		if (a.MailingCity != null && a.MailingState != null) 
			a.Shipping_Street_2_Clean__c = a.MailingCity + ', ' + a.MailingState;
		else a.Shipping_Street_2_Clean__c = '';
		
		// Populate Shipping Street 3 with Mailing Postal Code and Mailing Country,
		// but only if BOTH are non-null (I'm not sure if this is what you want)
		if (a.MailingPostalCode != null && a.MailingCountry != null) 
			a.Shipping_Street_3_Clean__c = a.MailingPostalCode + ' ' + a.MailingCountry;
		else a.Shipping_Street_3_Clean__c = '';
  }
}

 

civiccivic

Thanks a lot...