+ Start a Discussion
prasanth puvvada 4prasanth puvvada 4 

trigger for changing values after edit

hi freinds,  my senario is in account object if we click of "new" then it will show form,  in that one,  i put  a condition called "if gender == male" then add "Mr." to the front of the name and if gender==felmale then add "Mrs." to the front of the name.    So i created gender field on page. And i wrote a before insert trigger. ( well this is working fine).  

Problem: after saving the record then, if i change the gender value then the name NAME also should update correctly. (example, if i change value from male to female then is should change as "Mrs."    please help me what to add in this code or i need to add another code for this problem.

|Before insert code:-
trigger beforetriggergender on Account (before insert) {
    for(account a:trigger.new)
    {
        if(a.gender_of_owner__c=='male')
            a.name='Mr. '+a.name;
        if(a.gender_of_owner__c=='female')
            a.name='Mrs. '+a.name;
    }

}


 
screen shot for edit page.
User-added image
Best Answer chosen by prasanth puvvada 4
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger beforetriggergender on Account ( before insert , before update ) 
{
	
	if(Trigger.isInsert)
	{
		for(account a:trigger.new)
		{
			if(a.gender_of_owner__c=='male')
				a.name='Mr. '+a.name;
			if(a.gender_of_owner__c=='female')
				a.name='Mrs. '+a.name;
		}
	}	
	else if(Trigger.isUpdate)
	{
		for(account a:trigger.new)
		{
			Account oldAcc = Trigger.oldMap.get(a.id);
			if(a.gender_of_owner__c != oldAcc.gender_of_owner__c)
			{
				if(a.gender_of_owner__c=='male')
				{
					if( (a.Name).contains('Mr.'))
					{
						a.Name = (a.Name).remove('Mr.');
					}
					else if( (a.Name).contains('Mrs.'))
					{
						a.Name = (a.Name).remove('Mrs.');
					}

					a.name='Mr. '+a.name;
				}
				if(a.gender_of_owner__c=='female')
				{	
					if( (a.Name).contains('Mr.'))
					{
						a.Name = (a.Name).remove('Mr.');
					}
					else if( (a.Name).contains('Mrs.'))
					{
						a.Name = (a.Name).remove('Mrs.');
					}
					a.name='Mrs. '+a.name;
				}
			}	
		}
	}	
}

Please let us know if this will help you


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi 

Please try below code:-
trigger beforetriggergender on Account ( before insert , before update ) 
{

    for(account a:trigger.new)
    {
        if(a.gender_of_owner__c=='male')
            a.name='Mr. '+a.name;
        if(a.gender_of_owner__c=='female')
            a.name='Mrs. '+a.name;
    }

}
NOTE:- You Just need to before Update event in your same trigger.

Please mark this as solution if this will help you. So That if any one has same problem then this post can help.

Thanks
Amit Chaudhary
Ajay K DubediAjay K Dubedi
Hi Prasanth

I think the first parameter which you have used is correct i.e after insert but the next time when you are editing the field value than you are not acctually inserting anythin this time you are updating so in the trigger also use after update.
 
trigger beforetriggergender on Account (before insert,after Update) {
     for(account a:trigger.new)
     {
         if(a.gender_of_owner__c=='male')
             a.name='Mr. '+a.name;
         if(a.gender_of_owner__c=='female')
             a.name='Mrs. '+a.name;     }
 }

 
prasanth puvvada 4prasanth puvvada 4
AMITH, thankyou very much for your reply.    if i do this, then my value is appending.  eg:-  i edited the gender field and saved it. I did this process for 4 times then all the values are appended to the name.  

could u tell me the logic for removing the Mr. or Mrs checking if they are in name ?   Thanks in advance.    please see the screenshot.

User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger beforetriggergender on Account ( before insert , before update ) 
{
	
	if(Trigger.isInsert)
	{
		for(account a:trigger.new)
		{
			if(a.gender_of_owner__c=='male')
				a.name='Mr. '+a.name;
			if(a.gender_of_owner__c=='female')
				a.name='Mrs. '+a.name;
		}
	}	
	else if(Trigger.isUpdate)
	{
		for(account a:trigger.new)
		{
			Account oldAcc = Trigger.oldMap.get(a.id);
			if(a.gender_of_owner__c != oldAcc.gender_of_owner__c)
			{
				if(a.gender_of_owner__c=='male')
				{
					if( (a.Name).contains('Mr.'))
					{
						a.Name = (a.Name).remove('Mr.');
					}
					else if( (a.Name).contains('Mrs.'))
					{
						a.Name = (a.Name).remove('Mrs.');
					}

					a.name='Mr. '+a.name;
				}
				if(a.gender_of_owner__c=='female')
				{	
					if( (a.Name).contains('Mr.'))
					{
						a.Name = (a.Name).remove('Mr.');
					}
					else if( (a.Name).contains('Mrs.'))
					{
						a.Name = (a.Name).remove('Mrs.');
					}
					a.name='Mrs. '+a.name;
				}
			}	
		}
	}	
}

Please let us know if this will help you


 
This was selected as the best answer
prasanth puvvada 4prasanth puvvada 4
Thank you very much dude................. 
Amit Chaudhary 8Amit Chaudhary 8
Please mark the above solution as best solution if that will help you. So that some one has same issue this post can help.