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
PRADEEP YADAV 5PRADEEP YADAV 5 

Trigger Based Question

When an opportunity is inserted check for it’s duplicacy on the basis of its name and the account to which it is related to i.e. If it has same name and it’s linked to same Account then append “Duplicate Opportunity” in the name
PRADEEP YADAV 5PRADEEP YADAV 5
Using Helper Class
 
MoonpieMoonpie
Hi Pradeep,

Please share any code that you have already tried.
PRADEEP YADAV 5PRADEEP YADAV 5
trigger OpportunityInsert on Account(after insert)
{
    
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            classname.methodname(Trigger.new, Trigger.oldMap);
        }
    }
}

class ClassName
{
    public static void methodname(List<Account> acclist, Map<Id, Account> accmap)
    {
        List<Opportunity> opp = new List<Opportunity>();
        
        Map<Id, Account> mapacc = new Map<Id, Account>
        ([Select Id, Name,(Select Name From Opportunities) From Account Where 
        Id In : acclist]);
        
        for(Account a : acclist)
        {
            if(mapacc.get(a.Id).Opportunity.size()==0)
            {
                if(a.Name!=accmap.get(a.Id).Name)
                {
                    opp.add(new Opportunity(AccountId = a.Name, Name='shubham saini'+'FirstOpportunity',StageName ='Prospecting',
                    CloseDate=System.date()));
                }
                else
                {
                    opp.add(new Opportunity(AccountId = a.Name+'DuplicateOpportunity', Name='shubham saini',StageName ='Prospecting',
                    CloseDate=System.date()));
                }
            }
        }
    }
}
MoonpieMoonpie
Pradeep,

Can you please re-post that code by doing the following:

In your reply, click the <> button (highlighted below) at the top of your reply box.
User-added image

Then paste your code inside the popup text box.
User-added image

That will make it much easier to read and follow, and it will give your code line numbers that we use for reference.
PRADEEP YADAV 5PRADEEP YADAV 5
trigger TestingTriggerOnAccount on Account(after Insert)
{
    
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            TestingClass.TestingMethod(Trigger.new);
        }
    }
}

//Helper Class  Write
public class TestingClass
{
    public static void TestingMethod(List<Account> acclist)
    {
        List<Opportunity> opp = new List<Opportunity>();
        
        Set<String> setacc = new Set<String>();
        for(Account accloop : acclist)
        {
            setacc.add(accloop.Name);
        }
        List<Account>acc = [Select Id, Name,(Select Name From Opportunities) From Account Where Name In : setacc];
        Map<Id, Account> mapacc = new Map<Id, Account>();
        
        for(Account a : acc)
        {
            mapacc.put(a.Name, a);
        }
for(Account accl : acclist)
		{
			if(mapacc.containsKey(accl.name))
			{
                opp.add(new Opportunity(AccountId = accl.Id, Name ='Duplicate Opportunity'+accl.Name, StageName ='prospecting',CloseDate =System.today()));

			}
		}
    }
}

 
MoonpieMoonpie
FIrstly, my "gut" is telling me that this would be doable in Process Builder.  But I am new to a lot of this, so it might not be.  I would have to try it there first and then not be able to get it to work there before coding a trigger.  (Which you may have done.)

Secondly, I'm seeing what appears at first glance to be a lot of extra, non-needed code, but that is not what is not working, I guess.

Thirdly I see that in line 26 you create a...
Map<Id, Account>
...but in line 30 you use the Name and not the Id as the Map key.

Fourthly, you are comparing Account names and then inserting an Opportunity with 'Duplicate Opportunity' as part of the Opportunity name.
My understanding from your original question is that you were trying to compare Opportunity names to see if they are duplicates.

Lastly, you still haven't said exactly what issue(s) you have - is it just not working, or are there errors (if so, share them)?