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
Anik soni 7Anik soni 7 

How to make a trigger for this situation, i am stuck on this need help, i am learner.

When an opportunity is inserted check for it’s a delicacy on the basis of its name and the account to which it is related to i.e. If it has the same name and it’s linked to the same Account then append “Duplicate Opportunity” in the name.
Best Answer chosen by Anik soni 7
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Anik,
Try this code.I tested this in my org it is working fine.
trigger duplicateOpportunity on Opportunity (before insert) {
    Map<String ,id> oppmap = new Map<String ,id>();
    list<opportunity> opplist = new list<opportunity>();
    for(Opportunity o : trigger.new)
    {
        oppmap.put(o.name,o.accountid);
    }
    
    if(oppmap.size() > 0 )
    {
        List<opportunity> lstopp = [select name ,id,accountId from opportunity where name in :oppmap.keySet() ];
        
        Map<String ,id> mapNameAccount = new Map<String,id>();
        for(opportunity opp: lstopp)
        {
            mapNameAccount.put(opp.name ,opp.accountId);
        }
        
        for(opportunity op : trigger.new)
        {
            if(mapNameAccount.containsKey(op.name)&&mapNameAccount.get(op.name) == op.accountId)
            {
                op.Name = op.Name + ' Duplicate Opportunity ';
            }
        }
        
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Anik,
Try this code.I tested this in my org it is working fine.
trigger duplicateOpportunity on Opportunity (before insert) {
    Map<String ,id> oppmap = new Map<String ,id>();
    list<opportunity> opplist = new list<opportunity>();
    for(Opportunity o : trigger.new)
    {
        oppmap.put(o.name,o.accountid);
    }
    
    if(oppmap.size() > 0 )
    {
        List<opportunity> lstopp = [select name ,id,accountId from opportunity where name in :oppmap.keySet() ];
        
        Map<String ,id> mapNameAccount = new Map<String,id>();
        for(opportunity opp: lstopp)
        {
            mapNameAccount.put(opp.name ,opp.accountId);
        }
        
        for(opportunity op : trigger.new)
        {
            if(mapNameAccount.containsKey(op.name)&&mapNameAccount.get(op.name) == op.accountId)
            {
                op.Name = op.Name + ' Duplicate Opportunity ';
            }
        }
        
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Rishabh Bansal 23Rishabh Bansal 23
Hi Anik,

Try the following approach:-

1.Query on all existing opportunities.
2.Using the existing opportunities, create a Map of Opportunity Name and accountId i.e. Map<String,Id>
3.Iterate over trigger.new and check if Map.KeySet() contains new opportunity name and if it does then get the accountId using the same Map.get(newOppName) and compare it with new accountId.
4.If it matches then, change name to duplicate opp.

Thanks,
Rishabh Bansal
Anik soni 7Anik soni 7
Hi Chandrika,
Thank you for the help and i appreciate it. can you please check my code is it ok i am not sure.

trigger Secondtrigger on Opportunity (before insert) 
{
for(Opportunity op : trigger.new)
    {
        List<Opportunity> op1 = [Select Name,AccountId from Opportunity Where AccountId = :op.AccountId AND Name = :op.Name LIMIT 1];            
        If(op1.isEmpty()) 
        {
           op.Name = op.Name + ' Duplicate Opprotunity';
        }   
    }