0

In Account Sobject i have added two check-box's 1.Administrative Office and 2.Data Center

when user enter name and check anyone of it then Account Name is like Sam (A)/(D).

which i did with trigger using before insert event, Thats fine. Now when client edit the Account and change the checkbox it should update the Name again like Sam (D)/(A) according. But not updating with this code

here is my Trigger code for reference

trigger accName on Account (before insert, before update) {
    List<Account> acc;

    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            for(Account a:Trigger.New) {
                if(a.Administrative_Office__c == True) {
                    a.Name = a.Name + ' ( A ) ' ;
                }
                else if(a.Data_Center__c == True) {
                    a.Name = a.Name + ' ( D ) ' ;
                }
        else {
            if (Trigger.isUpdate) {
                List<Account> accn = new List<Account>();
                accn = [SELECT Id, Name 
                        FROM Account 
                        WHERE Id IN: Trigger.oldMap.keySet() 
                        LIMIT 1];

               for (Account au : accn) {
                   if(au.Administrative_Office__c == True) {
                       au.Name = au.Name + ' ( A ) ' ;
                   }
                   else if(au.Data_Center__c == True) {
                       au.Name = au.Name + ' ( D ) ' ;
                       //   au.Name = au.Name;
                   }
                   update accn;   
               } 
            }
        }
        insert a;   
        } 
      }
   }
}
0

2 Answers 2

3

trigger accName on Account (before insert, before update) {

if (Trigger.isBefore && Trigger.isInsert) {  
    for(Account acc:Trigger.New) { 
        if(acc.Administrative_Office__c == True) { 
        acc.Name = acc.Name + '(A)' ; 
        } 
        else if(acc.Data_Center__c == True) { 
        acc.Name = acc.Name + '(D)' ; 
        } 
    } 
} 

if (Trigger.isBefore && Trigger.isUpdate) { 
    for(Account acc:Trigger.New) { 
        if(acc.Administrative_Office__c == False && acc.Data_Center__c == True) {
            if(acc.Name.contains('(A)')) 
                acc.Name = acc.Name.replace('(A)', ' (D) ');
            else
                acc.Name = acc.Name + '(D)';
        }

        else if(acc.Data_Center__c == False && acc.Administrative_Office__c == True) {
            if(acc.Name.contains('(D)'))  
                acc.Name = acc.Name.replace('(D)', '(A)');
            else
                acc.Name = acc.Name + '(A)';
        }  
    } 
} 

}

0

trigger accName on Account (before insert, before update)
{

if (Trigger.isBefore) {
if (Trigger.isInsert) {

for(Account a:Trigger.New)
{
if(a.Administrative_Office__c == True)
{
a.Name = a.Name + ' ( A ) ' ;
}
else if(a.Data_Center__c == True)
{
a.Name = a.Name + ' ( D ) ' ;
}
else
{
try
{
insert a;
}
catch(Exception e)
{
String error = e.getMessage();
ApexPages.addMessage(new >ApexPages.Message(ApexPages.Severity.Info,error)); }
}

}
}
}

if (Trigger.isUpdate)
{

for(Account aa:Trigger.New)
{
if(aa.Administrative_Office__c == False && aa.Data_Center__c == True)
{
aa.Name = aa.Name.removeEnd('( A )');
aa.Name = aa.Name + ' ( D ) ' ;
}
else if(aa.Data_Center__c == False && aa.Administrative_Office__c == True)
{
aa.Name = aa.Name.removeEnd('( D )');
aa.Name = aa.Name + ' ( A ) ' ;
}
else
{
try
{
update aa;
}
catch(Exception ex)
{
String error = ex.getMessage();
ApexPages.addMessage(new >ApexPages.Message(ApexPages.Severity.Info,error));
}
}
}
}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .