0

I am writing a trigger on opportunity that updates the ServiceAppointment object's WorkType value when the Opportunity's Program Type value changes. ServiceAppointment is related to opportunity. ServiceAppointment has a field called Work Type that is a lookup to the Work Type object. I am trying to set the value of this Work Type lookup field to the value of the Program Type in Opportunity. So if the Program type changes to "FA" in opportunity, then the ServiceAppointment's Work Type should place the "FA" lookup value in there. I have the following code but I am getting "Field not writeable: serviceappointment.workTypeId. Can you please let me know how I can put the worktype id in this serviceappointment field? Thank you

static List<WorkType> servicetypelst = [select id,name from WorkType];
    Public static void setFieldServiceType (List<Opportunity> opptyupdatelst) {
        Map<String, WorkType> servicetypemap = new Map<String,WorkType>();
        List<Opportunity> updopplst = new List<Opportunity>();
        Set<Id> oppIds = new Set<Id>();
        for(WorkType sertyp : servicetypelst) {
            servicetypemap.put(sertyp.name.toUpperCase(),sertyp);
        }
        for(Opportunity opp : opptyupdatelst) {
            oppIds.add(opp.Id);
            Opportunity opt  = new Opportunity(Id = opp.Id); 
            opt.Program_Type__c = opp.Program_Type__c;
            if(opt.Program_Type__c == null || opt.Program_Type__c == '') {
                opt.WorkType__c = null;
                updopplst.add(opt);
            }
            else if(servicetypemap.containsKey(opt.Program_Type__c.toUpperCase())){
                opt.WorkType__c = servicetypemap.get(opt.Program_Type__c.toUpperCase()).id;
                updopplst.add(opt);
            }            
        }
        if(!updopplst.isEmpty()) {
            Database.update(updopplst,false);
        }        
        List<ServiceAppointment> servicelst = [select Id, WorkTypeId, Opportunity__r.WorkType__c from ServiceAppointment where opportunity__c in :oppIds];
        List<ServiceAppointment> updsrvlst = new List<ServiceAppointment>();
        for(ServiceAppointment srv : servicelst) {
            if (srv.WorkTypeId != srv.Opportunity__r.WorkType__c) {
                //srv.WorkTypeId = srv.Opportunity__r.WorkType__c;
                //this commented line is where error is occuring
                updsrvlst.add(srv);
            }
        }
        if(!updsrvlst.isEmpty()) {
            try {
                update updsrvlst;
            } catch (DMLException e) {                 
                Trigger.New[0].addError('Error updating Price Book: ' + e.getDMLMessage(0));
            }   
        }       
    }  

1 Answer 1

0

Reverse the assignment if I understand you correctly

Can you please let me know how I can put the worktype id in this serviceappointment field?

srv.Opportunity__r.WorkType__c  = srv.WorkTypeId;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .