Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.3 kB
2
Indexable
Never
// classe di test
@isTest
private class Test_UpdateOpp {
    @testsetup
    static void createdates()
    {
        contact c = new contact(lastname ='ContactCase');
        account a = new Account(name ='Antonio il Bello');
        insert c;
        insert a;
        list<opportunity> opp = new list<opportunity>();
        for(integer i=0;i<5;i++)
        {
            opportunity op = new Opportunity(name = 'opp'+i, stagename ='Value Proposition', closeDate= system.today(), accountid =a.id);
            
            if(i==2 || i==4)
            {
                  op.opp_contact__c=c.id;
            }
              opp.add(op);
        }
        insert opp;
   
    }
    @istest
    static void m1()
    {
        test.startTest();
        list <opportunity> listacc = [select name, stagename from opportunity where account.name ='Antonio il Bello'];
        for(opportunity opp:listacc)
        {
            opp.stagename = 'closed won';
        }
        Database.update(listacc,false) ;
        test.stopTest();
    }

}





// Helper class
public class HelperClassUpdateOpp {
		public static void updateOppTrigger(list<Opportunity> listOppnew, list<opportunity> listOppold)
        {
            list <account> listacc = [select name, id,(select id, name from opportunities) from account];
           		for(integer i=0;i<listOppnew.size();i++)
                {
                    if(listOppold.get(i).stagename!= listOppnew.get(i).stagename && listOppnew.get(i).stagename == 'closed won')
                    {
                        for(account acc :listacc)
                        {
                            if(listOppnew.get(i).accountID == acc.id && acc.opportunities.size()>5)
                            {
                                listOppnew.get(i).stagename.addError('Il contact ha più di 5 opportunità. Non puoi fare aggiornare');
                            }
                        }
                    }
                        
                }
        }
    
    public static void insertCase(list<Opportunity> listOppnew, list<opportunity> listOppold)
    {
		list<case> caselist = new list<case>();
        list<contact> contactlist = [select id, lastname ,(select id from cases) from contact];
           		for(integer i=0;i<listOppnew.size();i++)
                {
                    if(listOppold.get(i).stagename!= listOppnew.get(i).stagename && listOppnew.get(i).stagename == 'closed won')
                    {
                        for(contact acc :contactlist)
                        {
                            if(listOppnew.get(i).opp_contact__c == acc.id && acc.cases.size()== 0)
                            {
                               case c = new case (status ='Working', origin ='phone', contactid=listOppnew.get(i).opp_contact__c);
                                caselist.add(c);
                            }
                        }
                      
                    }
                        
                }
        	insert caselist;

    }
}




//trigger
trigger UpdateOppTrigger on Opportunity (before update, after update) {
   if(trigger.isbefore)
        HelperClassUpdateOpp.updateOppTrigger(trigger.new, trigger.old);
    else
        HelperClassUpdateOpp.insertCase(trigger.new, trigger.old);
		
}