• SFDC_shubh
  • NEWBIE
  • 30 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
Write a trigger to add Mr. and Mrs. as a prefix on Account Name on the basis of sex checkbox & radio button. ?
trigger accNamePrefix on Account (before insert, before update) {
for (Account acc : trigger.new){
    String name = acc.Name;
    if(acc.Sex__c == 'Male' && !name.contains('Mr.')){
        acc.Name = 'Mr. '+acc.Name;
    }
    else if(!name.contains('Mrs.')){
        acc.Name = 'Mrs. '+acc.Name;
    }
   }
}

and its test class--
@istest
public class namePrefixTest {
    @istest
    public static void method(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Male';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        System.assert(acc.Name.contains('Mr.'));
    }
}

assertion failed in this methd, So what to do in this class, assert is pass
Write a trigger to add Mr. and Mrs. as a prefix on Account Name on the basis of sex checkbox & radio button. ?
trigger accNamePrefix on Account (before insert, before update) {
for (Account acc : trigger.new){
    String name = acc.Name;
    if(acc.Sex__c == 'Male' && !name.contains('Mr.')){
        acc.Name = 'Mr. '+acc.Name;
    }
    else if(!name.contains('Mrs.')){
        acc.Name = 'Mrs. '+acc.Name;
    }
   }
}

and its test class--
@istest
public class namePrefixTest {
    @istest
    public static void method(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Male';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        System.assert(acc.Name.contains('Mr.'));
    }
}

assertion failed in this methd, So what to do in this class, assert is pass