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
Mary Ardelyn NabongMary Ardelyn Nabong 

Illegal assignment from Set<Id> to Id


Hi Guro,
I have the above error. Please help!!!.

trigger OpportunityBillingSchedule on OpportunityLineItem (after insert) {
    list<Schedule__c> AddBS = new list<Schedule__c>();
    for( OpportunityLineItem olitems : Trigger.new)
    {
        //populate the account
        List<opportunity> oppl = [Select Id, AccountId
                                 from Opportunity
                                 where Id =:olitems.OpportunityId] ;
       
        set<id> opAcc = new set<id>();
        for( Opportunity opp: oppl )
         {
             opAcc.add( opp.AccountId);
         }  
         if (olitems.term__c)
         {
            Schedule__c BS       = new Schedule__c();
             BS.name                   = olitems.name;
             BS.Account__c        = opAcc; 
             BS.opportunity__C    = olitems.opportunityid;
             BS.Billing_amount__c = olitems.unitprice;
             BS.Date__c           = Date.today();  // olitems.Maintenance_start_date__c;
             BS.Status__c         = 'Open';
             AddBS.add(BS);
              }
        }
 
       insert AddBS;   
  }
-----------------------------------------
i did try:     BS.Account__c        =  oppl[1].id ; 
but got another error upon saving : System.ListException: List index out of bounds: 1: Trigger.OpportunityBillingSchedule: line 25, column 1   ​


Thank in Advance.

Mary



 

Best Answer chosen by Mary Ardelyn Nabong
KapilCKapilC
Update the line #11
for([opportunity opp :[Select Id, AccountId from Opportunity where Id in: opportunityIdSet])

Excuse me for typos.
 

All Answers

KapilCKapilC
Hi Mary,

Please find the code below and then try.
trigger OpportunityBillingSchedule on OpportunityLineItem (after insert) {
    set<id> opportunityIdSet = new set<id>();
	
	for( OpportunityLineItem olitems : Trigger.new){
		opportunityIdSet.add(olitems.OpportunityId);
	}
	
	//populate the account
        Map<id,opportunity> oppIdMap = new Map<id,opportunity>();
		
		for(<opportunity opp :[Select Id, AccountId from Opportunity where Id in: opportunityIdSet]){
		  oppIdMap.put(opp.id,opp);
		}
    
        list<Schedule__c> addBsList = new list<Schedule__c>();
		for( OpportunityLineItem olitems : Trigger.new){
			if(olitems.term__c){
				Schedule__c BS       = new Schedule__c();
				 BS.name              = olitems.name;
				 BS.Account__c        = oppIdMap.get(olitems.OpportunityId).AccountId; 
				 BS.opportunity__C    = olitems.opportunityid;
				 BS.Billing_amount__c = olitems.unitprice;
				 BS.Date__c           = Date.today();  // olitems.Maintenance_start_date__c;
				 BS.Status__c         = 'Open';
				 addBsList.add(BS);
			}
         
		}
        
		if(!addBsList.isEmpty()){
			insert addBsList;
		}			
  }



[If you got answer from my post please mark it as solution.]


Thanks,
Kapil
Email (mailto:forcecube@gmail.com)
KapilCKapilC
Update the line #11
for([opportunity opp :[Select Id, AccountId from Opportunity where Id in: opportunityIdSet])

Excuse me for typos.
 
This was selected as the best answer
Mary Ardelyn NabongMary Ardelyn Nabong
Hi KapilC...Yes it works amazingly thank you.