function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
colin Berrouardcolin Berrouard 

Error: Compile Error: Method does not exist or incorrect signature:

Hi

I have this error "Error: Compile Error: Method does not exist or incorrect signature: [Schema.SObjectField].addError(String) at line 15 column 17"

trigger NameDuplicatePrevent on Team__C
                               (before insert, before update) {

    Map<String, Team__c> teamMap = new Map<String, Team__c>();
    for (Team__c team : System.Trigger.new) {
                   
        if ((Team__c.Name != null) &&
                (System.Trigger.isInsert ||
                (Team__c.Name != 
                    System.Trigger.oldMap.get(Team__c.Id).Name))) {
                
            // Make sure another new Team isn't also a duplicate  
    
            if (teamMap.containsKey(Team__c.Name)) {
                Team__c.Name.addError('Another Team Name exist');  <--- error line
            } else {
                teamMap.put(Team__c.Name, Team);
            }
       }
    }
        
    for (Team__c team : [SELECT Name FROM Team__c
                      WHERE Name IN :teamMap.KeySet()]) {
        Team__c newTeam = teamMap.get(Team__c.Name);
        newTeam.Name.addError('A Name already exist. ');
    }
}

Here is the TEAM object description
User-added image

Thamks

colin
 
Best Answer chosen by colin Berrouard
Amit Chaudhary 8Amit Chaudhary 8
PLease update your code like below
trigger NameDuplicatePrevent on Team__C(before insert, before update) 
{

    Map<String, Team__c> teamMap = new Map<String, Team__c>();
    for (Team__c team : System.Trigger.new) 
	{
        if ((team.Name != null) &&    (System.Trigger.isInsert ||  (team.Name !=  System.Trigger.oldMap.get(team.Id).Name))) 
		{
            // Make sure another new Team isn't also a duplicate  
            if (teamMap.containsKey(team.Name)) 
			{
                team.Name.addError('Another Team Name exist');
            } 
			else 
			{
                teamMap.put(team.Name, Team);
            }
       }
    }
        
    for (Team__c team : [SELECT Name FROM Team__c WHERE Name IN :teamMap.KeySet()]) 
	{
        Team__c newTeam = teamMap.get(Team__c.Name);
        newTeam.Name.addError('A Name already exist. ');
    }
}
Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
PLease update your code like below
trigger NameDuplicatePrevent on Team__C(before insert, before update) 
{

    Map<String, Team__c> teamMap = new Map<String, Team__c>();
    for (Team__c team : System.Trigger.new) 
	{
        if ((team.Name != null) &&    (System.Trigger.isInsert ||  (team.Name !=  System.Trigger.oldMap.get(team.Id).Name))) 
		{
            // Make sure another new Team isn't also a duplicate  
            if (teamMap.containsKey(team.Name)) 
			{
                team.Name.addError('Another Team Name exist');
            } 
			else 
			{
                teamMap.put(team.Name, Team);
            }
       }
    }
        
    for (Team__c team : [SELECT Name FROM Team__c WHERE Name IN :teamMap.KeySet()]) 
	{
        Team__c newTeam = teamMap.get(Team__c.Name);
        newTeam.Name.addError('A Name already exist. ');
    }
}
Let us know if this will help you
 
This was selected as the best answer
colin Berrouardcolin Berrouard
thanks