+ Start a Discussion
steve456steve456 

Auto Update Name after saving

I have three fields  on the object "Building"

 

1)Building Name                                       Api Name          "Name"

2)Location                                                   Api Name          "Location__c"       It is a lookup field to the obj "Location"

3)Date                                                          Api Name          "Date__c"

 

 

When a user enters the building name ,location ,date and saves the record the Building Name should be autoupdated to

 

Building Name.Location.Date

 

 

date should be of the format "yyyy-mm-dd"

 

 

Can anybody shred light on to this and help me

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Hi Steve,

 

Below is sample code for you, check if it helps:

 

trigger updateName on Building__c(before update)
{
	Map<ID, Location__c> mLocation = New Map<ID, Location__c>([Select Name From Location__c Where ID IN (select Location__c From Building__c Where ID In :trigger.new)]);
	String PreviousName = '';
	for(Building__c b :trigger.new)
	{
		if(b.Name != null && mLocation.containsKey(b.Location__c) && mLocation.get(b.Location__c).Name != null && Date__c != null){
			if(Trigger.isUpdate){
				PreviousName = '';
				if(PreviousName.contains('.'))
					PreviousName = b.name.split('.')[0];  
				b.Name = (PreviousName == '') ? b.Name : PreviousName + '.' + 
						mLocation.get(b.Location__c).Name + '.' + 
						Date__c.format('yyyy-MM-dd');
			}
			else if(Trigger.isInsert){
				b.Name = b.Name + '.' + 
						mLocation.get(b.Location__c).Name + '.' + 
						Date__c.format('yyyy-MM-dd');
			}
		}
	}
}

 

All Answers

craigmhcraigmh

Something like this:

 

trigger BuildingSaving on Building (before insert, before update) {
	for(integer i = 0; i < trigger.new.size(); i++) {
		if(trigger.isInsert() || trigger.old[i].Name == null || trigger.old[i].Location__c == null || trigger.old[i].Date__c == null) {
			if(trigger.new[i].Name != null && trigger.new[i].Location__c != null && trigger.new[i].Date__c != null) {
				trigger.new[i].Name = trigger.new[i].Name + '.' + trigger.new[i].Location__c + '.' + trigger.new[i].Date__c.format();
			}
		}
	}
}

 

 

You may need to play with the date format piece, though.

 

 

EDIT:
It's trigger.isInsert, not trigger.isInser(). It's a property, not a method.

steve456steve456

I am getting the location Id .How do i get the  location Name

craigmhcraigmh

From which part of the code?

steve456steve456

When  i am  saving the record the formula is coming

 

As   Buildingname.595959jgkgffjf.2/12/2012

 

I need the name .Iamgetting the id

craigmhcraigmh

Oh, got it. Forgot that it was a lookup field.  So you'd need to use the relationship name/name field, which will be similar to this:

 

trigger.new[i].Name = trigger.new[i].Name + '.' + trigger.new[i].Location__r.Name + '.' + trigger.new[i].Date__c.format();

 

steve456steve456

When i give this way

 

the middle part i.e the location name is coming as null

craigmhcraigmh

Oh yeah, you'll have to change the if statements as well:

 

trigger BuildingSaving on Building (before insert, before update) {
	for(integer i = 0; i < trigger.new.size(); i++) {
		if(trigger.isInsert() || trigger.old[i].Name == null || trigger.old[i].Location__r.Name == null || trigger.old[i].Date__c == null) {
			if(trigger.new[i].Name != null && trigger.new[i].Location__r.Name != null && trigger.new[i].Date__c != null) {
				trigger.new[i].Name = trigger.new[i].Name + '.' + trigger.new[i].Location__c + '.' + trigger.new[i].Date__c.format();
			}
		}
	}
}

 

Starz26Starz26

try

 

trigger updateName on Building__c(before insert, before update){


Map<ID, Location__c> mLocation = New Map<ID, Location__c>([Select Name From Location__c Where ID IN (select Location__c From Building__c Where ID In :trigger.new)]);

for(Building__c b :trigger.new)
	b.Name = b.Name + '.' + mLocation.get(b.Location__c).Name + '.' + Date__c.format('yyyy-mmmm-dd');



}

 

steve456steve456

Its not working

Starz26Starz26

How about:

 

**For insert you will either have to make up the Name or do it in an after insert

 

trigger updateName on Building__c(before update){


Map<ID, Location__c> mLocation = New Map<ID, Location__c>([Select Name From Location__c Where ID IN (select Location__c From Building__c Where ID In :trigger.new)]);

String[] tmpString = New String[]{};

for(Building__c b :trigger.new){
   tmpString = b.name.split('.');  
	b.Name = tmpString[0] + '.' + mLocation.get(b.Location__c).Name + '.' + Date__c.Year + '-' + Date__c.Month() + '-' + Date__c.day();
}


}

 

If this does not work, be more specific as to what you need please

steve456steve456

When I am trying to update the location after insert i am getting eror as

 

Review all error messages below to correct your data.
Apex trigger updateName caused an unexpected exception, contact your administrator: updateName: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateName: line 8, column 1

Rahul SharmaRahul Sharma

Hi Steve,

 

Below is sample code for you, check if it helps:

 

trigger updateName on Building__c(before update)
{
	Map<ID, Location__c> mLocation = New Map<ID, Location__c>([Select Name From Location__c Where ID IN (select Location__c From Building__c Where ID In :trigger.new)]);
	String PreviousName = '';
	for(Building__c b :trigger.new)
	{
		if(b.Name != null && mLocation.containsKey(b.Location__c) && mLocation.get(b.Location__c).Name != null && Date__c != null){
			if(Trigger.isUpdate){
				PreviousName = '';
				if(PreviousName.contains('.'))
					PreviousName = b.name.split('.')[0];  
				b.Name = (PreviousName == '') ? b.Name : PreviousName + '.' + 
						mLocation.get(b.Location__c).Name + '.' + 
						Date__c.format('yyyy-MM-dd');
			}
			else if(Trigger.isInsert){
				b.Name = b.Name + '.' + 
						mLocation.get(b.Location__c).Name + '.' + 
						Date__c.format('yyyy-MM-dd');
			}
		}
	}
}

 

This was selected as the best answer
anurajanuraj

Please use this code it may help you

 

trigger UpdateTriggerObject on objname (after insert)
{
set<id> nameId = new set<id>();
list<objname > trgList = new list<objname >();

for(objname t: trigger.new)
{


nameId.add(t.id);

}

trgList = [Select fieldname1, fieldname2, fieldname3 from objname where id = :nameId ];
for(integer i = 0; i < trgList.size(); i++)
{
trgList[i].fieldname1= trgList[i].fieldname2+'.'+trgList[i].fieldname3+'.'+date.today();

}
insert trgList;

}

steve456steve456

took the logic from i t and had tweak somethings and also format of date doesnt work.Had used different approach.Thanks a lot