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
Sakthi Ganesh RSakthi Ganesh R 

ERROR: Incompatible element type SOBJECT:Billing_MVA__c for collection of SOBJECT:Opportunity

I have written a trigger and i got this error Incompatible element type SOBJECT:Billing_MVA__c for collection of SOBJECT:Opportunity.

My trigger:

trigger totalBillinghrs on Billing_MVA__c (before insert)
{
    //date startdate;
    //date enddate;
    list<opportunity> opp = new list<opportunity>();
    Billing_MVA__c Bill = trigger.new[0];
    integer counter = [select count() from opportunity where BCRNo__c != null];
    if(counter >0)
    {
        string sdate = Bill.Billing_cycle_from__c;
        string edate = bill.Billing_cycle_from__c;
        string[] stdate = sdate.split('/');
        string[] endate = edate.split('/');
       // if(stdate !=null)
        
            string sday = stdate[2];
            string smonth = stdate[0];
            string syear = stdate[1];
            date startdate = date.parse(smonth+'/'+sday+'/'+syear);
            system.debug('*************'+startdate);
       // if(endate != null)
        
            string eday = endate[2];
            string emonth = endate[0];
            string eyear = endate[1];
            date enddate = date.parse(emonth+'/'+eday+'/'+eyear);
            system.debug('*************'+enddate);
        
        list<opportunity> opplist = [SELECT id,Billing_cycle__c,totalhrs__c FROM opportunity WHERE (Billing_cycle__c>:startdate) and (Billing_cycle__c<:enddate)];
         system.debug('*************'+opplist);
        for(opportunity oppnew : opplist)
        {
            Bill.Total_MVA_billing_hrs__c = oppnew.totalhrs__c;
            opp.add(Bill);
        }
        upsert opp;
        
    }
}

can any one help me?

Thanks in advance

Sakthi
Best Answer chosen by Sakthi Ganesh R
BalajiRanganathanBalajiRanganathan

if you are trying to set total billing hour from Billing_MVA__c to each Opp then you need to have below code
oppnew.totalhrs__c = Bill.Total_MVA_billing_hrs__c;
opp.add(oppnew);

else if you are trying set total billing hour from Opportunities into Billing_MVA__c 
then dont add Billing_MVA__c to list and don't do upsert/

also you should not be using trigger.new[0]. you have to write builk trigger. if multiple Billing_MVA__c is inserted using import then your code
will work only for first record.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm