+ Start a Discussion
neha pandey 25neha pandey 25 

write a trigger on contact to update the mobile number with the country isd code. like if a new contact is insert or update and the contact country value or mobile number changed then trigger should run and it will check the country of contact record.

Best Answer chosen by neha pandey 25
goabhigogoabhigo
You can achieve this using workflow too. Advantage over trigger is that you dont have to need a coder/developer in case you need to do minor changes. Also you need not write test class to migrate it to production.

Here is what you need to do in a workflow:
  • Create a workflow rule on Contact object
  • Set the Evaluation Criteria to Evaluate the rule when a record is created, and every time it’s edited
  • Set the rule criteria to:
AND (
 OR (
  NOT( ISPICKVAL(leadtherace__CountryISDCode__c, '')),
  I
SCHANGED(leadtherace__CountryISDCode__c)
 
),
 NOT( ISBLANK (MobilePhone) )
)
  • ​Now add a Field Update on Mobile field
  • The formula would look like:
  • IF( BEGINS( MobilePhone, '+'),
     IF( ISPICKVAL (CountryISDCode__c, 'singapore'), 
      ('+65' & ( RIGHT (MobilePhone, LEN (MobilePhone)-3))),
      IF( ISPICKVAL (CountryISDCode__c, 'india'), 
       ('+91' & ( RIGHT (MobilePhone, LEN (MobilePhone)-3))),
       IF( ISPICKVAL (CountryISDCode__c, 'us'), 
        ('+01' & ( RIGHT (MobilePhone, LEN (MobilePhone)-3))),
        MobilePhone
       )
      )
     ),
     IF( ISPICKVAL (CountryISDCode__c, 'singapore'), 
      ('+65' & MobilePhone),
      IF( ISPICKVAL (CountryISDCode__c, 'india'), 
       ('+91' & MobilePhone),
       IF( ISPICKVAL (CountryISDCode__c, 'us'), 
        ('+01' & MobilePhone),
        MobilePhone
       )
      )
     )  
    )
I have tested this. It works fine. Please let me know if you need clarification on anything I have mentioned. If you want trigger, please do the similar logic in the trigger code.

--
Abhi

All Answers

goabhigogoabhigo
What do you want us to do Neha?
Post the code snippet. Show us what have you tried to do.

--
Abhi
neha pandey 25neha pandey 25
trigger ContactCountryISDCode on Contact (before insert, before update) {
    
    set<String> CountryISDCode = new Set<String>();
    set<id> accid=new Set<id>();
    if(trigger.isInsert || trigger.isUpdate)
    {
        
        List<Contact> updateContact= new List<Contact>();
        for(Contact con : Trigger.new)
        {
          if(con.CountryISDCode__c != null && con.MobilePhone != null &&con.MobilePhone.length() < 12)
              {
            if(con.CountryISDCode__c=='singapore')
                {
              con.MobilePhone = '+65'+ con.MobilePhone;  
             }
            else if(con.CountryISDCode__c=='india')
            {
          con.MobilePhone ='+91'+ con.MobilePhone;  
            }
        else if(con.CountryISDCode__c=='us')
            {
          con.MobilePhone ='+01'+ con.MobilePhone;  
            }
      }
    }
     
  }          
}
neha pandey 25neha pandey 25
now i want to update it using split method then how can i do it..
goabhigogoabhigo
Can you elaborate please? What do you mean by split?
neha pandey 25neha pandey 25
when i update the custom field countryIsdCode with other country then it is not updated to the new value .its also maintain its previous value.i am getting result in this way which is not correct.+01+91+65+65+65+91(457) 896-8744. i want only one isd code which must be updated then how colud i solve it.
goabhigogoabhigo
You can achieve this using workflow too. Advantage over trigger is that you dont have to need a coder/developer in case you need to do minor changes. Also you need not write test class to migrate it to production.

Here is what you need to do in a workflow:
  • Create a workflow rule on Contact object
  • Set the Evaluation Criteria to Evaluate the rule when a record is created, and every time it’s edited
  • Set the rule criteria to:
AND (
 OR (
  NOT( ISPICKVAL(leadtherace__CountryISDCode__c, '')),
  I
SCHANGED(leadtherace__CountryISDCode__c)
 
),
 NOT( ISBLANK (MobilePhone) )
)
  • ​Now add a Field Update on Mobile field
  • The formula would look like:
  • IF( BEGINS( MobilePhone, '+'),
     IF( ISPICKVAL (CountryISDCode__c, 'singapore'), 
      ('+65' & ( RIGHT (MobilePhone, LEN (MobilePhone)-3))),
      IF( ISPICKVAL (CountryISDCode__c, 'india'), 
       ('+91' & ( RIGHT (MobilePhone, LEN (MobilePhone)-3))),
       IF( ISPICKVAL (CountryISDCode__c, 'us'), 
        ('+01' & ( RIGHT (MobilePhone, LEN (MobilePhone)-3))),
        MobilePhone
       )
      )
     ),
     IF( ISPICKVAL (CountryISDCode__c, 'singapore'), 
      ('+65' & MobilePhone),
      IF( ISPICKVAL (CountryISDCode__c, 'india'), 
       ('+91' & MobilePhone),
       IF( ISPICKVAL (CountryISDCode__c, 'us'), 
        ('+01' & MobilePhone),
        MobilePhone
       )
      )
     )  
    )
I have tested this. It works fine. Please let me know if you need clarification on anything I have mentioned. If you want trigger, please do the similar logic in the trigger code.

--
Abhi
This was selected as the best answer
neha pandey 25neha pandey 25
Thanks.. it works fine using workflow but i tried it using trigger but its not working can you please give me idea that how to resolve it for update using Trigger.
goabhigogoabhigo
What is your latest trigger code? This time I will just guide you, won't give out the code ;)