+ Start a Discussion
bpl3792bpl3792 

Trigger to transfer ID to another field

This is my first real "project" regarding salesforce and apex so I'm not too familiar with it. What I would like to do is have a trigger use the asset's ID as a asset name if one isn't given. After doing some reading I think I have a good idea on what needs to be done but I keep getting a "DescriptionResourcePathLocationTypeSave error: Invalid initial type LIST<IT_Asset__c> for MAP<Name,String>" returned when I save it. Any ideas?

 

trigger AssetNamePopulate on IT_Asset__c (before insert, before update) 
{
	
	Set<Id> aids = new Set<Id> ();
	for (IT_Asset__c asset : Trigger.new) 
	{
		aids.add(asset.name); 
		
	}
    
	Map<Id, string> m = new Map<Id, string>([Select name, asset_name__c from IT_Asset__c where name in :aids]);

	for (IT_Asset__c asset : Trigger.new) 
	{
        if (asset.asset_name__c == null) 
        {
        	if (m.containsKey(asset.name)) 
        	{
        		asset.asset_name__c = m.get(asset.name);
        	}
        }
     }
	
	
}

 

Best Answer chosen by Admin (Salesforce Developers) 
BigSmokeBigSmoke

I only see one table here.   It looks like there's a field called IT_Asset__c.Name and IT_Asset__c.asset_name__c.   Where is this Asset ID coming from?  I don't see the need for a map here at all, or even a trigger.  Just use a workflow rule.

 

Also in your first "for" loop, is the field IT_Asset__c.Name an ID field?  I can see it being an Autonumber but not a field of type ID.  You'd need "string" in place of  "ID".

 

If all my assumptions are right, (if you don't decide on a workflow update instead) why not this?

 

for (IT_Asset__c asset : Trigger.new) {
       if (asset.asset_name__c == null){
         asset.asset_name__c = asset.name;
       }
}

 

 

All Answers

WizradWizrad

I'm slightly confused by what your code is doing as opposed to what you say you want it to do, but the error you see results from the line:

 

Map<Id, string> m = new Map<Id, string>([Select name, asset_name__c from IT_Asset__c where name in :aids]);

 

This type of shorthand map initialization would expect a map of type Map<Id, IT_Asset__c>, essentially mapping the IT_Asset__c record's id to the record itself.  To build out the map the way you want it you would need to do something similar to the following:

 

Map<Id, string> m = new Map<Id, string>();
for(IT_Asset__c currentRecord : [Select name, asset_name__c from IT_Asset__c where Id in :Trigger.new]) {
        m.put(currentRecord.name, currentRecord.asset_name);
}

 

 

 

 

BigSmokeBigSmoke

I only see one table here.   It looks like there's a field called IT_Asset__c.Name and IT_Asset__c.asset_name__c.   Where is this Asset ID coming from?  I don't see the need for a map here at all, or even a trigger.  Just use a workflow rule.

 

Also in your first "for" loop, is the field IT_Asset__c.Name an ID field?  I can see it being an Autonumber but not a field of type ID.  You'd need "string" in place of  "ID".

 

If all my assumptions are right, (if you don't decide on a workflow update instead) why not this?

 

for (IT_Asset__c asset : Trigger.new) {
       if (asset.asset_name__c == null){
         asset.asset_name__c = asset.name;
       }
}

 

 

This was selected as the best answer
bpl3792bpl3792

Right, there is only 1 table but that solution worked great. I'm just trying to get familiar with the triggers and I tihnk I made it a little too difficult. Thanks!