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
Ramanjot SidhuRamanjot Sidhu 

Error: Compile Error: Incompatible element type String for collection of Integer at line 5 column 9

trigger SetContactOnOrderItem on Line_item__c (before update, before insert) {
    Set<Integer> lineitemnumbers = new Set<Integer>();

    for (Line_item__c collectNumFromLineitem : Trigger.new) {
        lineitemnumbers.add(collectNumFromLineitem.Name);
    }

    List<Opportunity> oppList = [SELECT id, Related_Sponsorship_Offer__c
 FROM Opportunity WHERE Related_Sponsorship_Offer__c IN :lineitemnumbers];

    Map<Integer, Opportunity> lineitemNumToOppMap = new Map<Integer, Contact>();

    for (Opportunity c : oppList) {
        lineitemNumToOppMap.put(c.Related_Sponsorship_Offer__c, c);
    }

    for (Line_item__c o : Trigger.new) {
        if (o.Name != null) {
            o.Opportunity__c = lineitemNumToOppMap.get(o.Name).id;
        }
        else {
            o.Opportunity__c = null;
        }
    }
}


I have a custom object named: Line_item__c and a standard object called opportunity. The custom object has a lookup field for an opportunity. The opportunity page also has a lookup field for the custom object. When a opportunity look up field is populated on the custom object, it should autopopulate the related Line item on the look up field on the opportunity. I tried this trigger but I get the following error. I am new the apex and any help would be appreciated.

Thank you

Vivek DeshmaneVivek Deshmane
Hi,
Try below code and let me know.
trigger SetContactOnOrderItem on Line_item__c (before update, before insert) {
    Set<String> lineitemnumbers = new Set<String>();

    for (Line_item__c collectNumFromLineitem : Trigger.new) {
        lineitemnumbers.add(collectNumFromLineitem.Name);
    }

    List<Opportunity> oppList = [SELECT id, Related_Sponsorship_Offer__c
 FROM Opportunity WHERE Related_Sponsorship_Offer__c IN :lineitemnumbers];

    Map<Integer, Opportunity> lineitemNumToOppMap = new Map<Integer, Contact>();

    for (Opportunity c : oppList) {
        lineitemNumToOppMap.put(c.Related_Sponsorship_Offer__c, c);
    }

    for (Line_item__c o : Trigger.new) {
        if (o.Name != null) {
            o.Opportunity__c = lineitemNumToOppMap.get(o.Name).id;
        }
        else {
            o.Opportunity__c = null;
        }
    }
}

Best Regards,
-Vivek
Ramanjot SidhuRamanjot Sidhu
This does not work. However I have used the process builder in salesforce to autopopulate the field and no longer need this trigger.