0

I have created an Activity Custom field called "Status" could we link that with the Opportunity Custom field called "Status" ? with the help of Workflow or Formula field or is there any work around for this ?

0

I do not believe that you can do this with a workflow or formula field, as Activities is a special object. You would need to create a trigger that would then update the linked Opportunity.

A trigger like this should get that. This trigger is assuming that your Opp Status field and Task Status field have the same values, and that you want to blindly override any existing Opp Status from a previous task. If that is not what you want, then you will need to modify it to match your needs.

trigger UpdateOpportunity on Task (after insert, after update) {

    Set<Id> OppID = new Set<Id>();
    List<Opportunity> OppsToUpdate = New List<Opportunity>();

    for (Task t : Trigger.new){
        if (t.WhatID != null && String.valueOf(t.WhatId).subString(0,3) == '006'){
            OppID.add(t.WhatId);
        }
    }

    Map<Id,Opportunity> OppMap = New Map<Id,Opportunity>([select id, Status__c from Opportunity where id in :OppID]);


        for (Task t : Trigger.new) {
          if (String.valueOf(t.WhatId).subString(0,3) == '006'){
           if (t.Status__c != null) {
            Opportunity o = OppMap.get(t.WhatID);
            o.Status__c = t.Status__c;
            OppsToUpdate.add(o);
           }
          }
        }


    if (OppsToUpdate.size() > 0){
        update OppsToUpdate;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.