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
BobBob 

Save error: illegal assignment from Ext_Warranty__c to Unit__c

Hello, i'm sucking up my pride a little bit, but the code below appears to correct but I get a Save error: illegal assignment from Ext_Warranty__c to Unit__c when I try to compile it. If anyone could help with this I would appreciate it as I am going out my mind trying to figure out why  this code is failing.
 
trigger UpdateUnitExtWarranty on Unit__c (before insert, before update) {

 // Step 1: Create a set of all emails of Users to query
  Set<String> allpc = new Set<String>();
  for (Unit__c newUnit :Trigger.new) {
    if (newUnit.Product_Code__c != null) {
      allpc.add(newUnit.Product_Code__c);    }
  }

  // Step 2: Query for all the extended warranties in Step 1
  List<Extended_Warranty__c> potentialextwrnty = [SELECT Id, Product_Code__c FROM Extended_Warranty__c
                                 WHERE Product_Code__c IN :allpc];

  
  // 
  Map<String, Extended_Warranty__c> extwrntyMap = new Map<String, Extended_Warranty__c>();
  for (Extended_Warranty__c e : potentialextwrnty) {
    extwrntyMap.put(e.Product_Code__c, e);
  }
  
  
  for (Unit__c newUnit : Trigger.new) {
    if (newUnit.Product_Code__c == null) {
      Unit__c extendedwarranty = extwrntyMap.get(newUnit.Product_Code__c );
      if (extendedwarranty != null) {
      	//rename extended_warranty fieldbelow on unit page
        newUnit.Ext_Warranty__c = extendedwarranty.Id;
      }
    }
  }
}

 
Best Answer chosen by Bob
R Z KhanR Z Khan
Line 24 should be
 Extended_Warranty__c extendedwarranty = extwrntyMap.get(newUnit.Product_Code__c );

All Answers

R Z KhanR Z Khan
Line 24 should be
 Extended_Warranty__c extendedwarranty = extwrntyMap.get(newUnit.Product_Code__c );
This was selected as the best answer
R Z KhanR Z Khan
Hi Please odnt forget ot mark this question as resolved if this answered your question
BobBob
RZ, thank you for your help! I updated the trigger to the code below. The order number on the unit object needed to match the extended warranty name and also the product code. You helped me figure out the biggest piece though. 


 
trigger TriggerUpdateUnitExtWarranty on Unit__c (before insert, before update) {
// Step 1: Create a set of all extended warranties of Units to query
  Set<String> extwrnty = new Set<String>();
  
for (Unit__c newUnit : Trigger.new) {
    if (newUnit.Order_No__c != null) {
      extwrnty.add(newUnit.Order_No__c);    }
  }

  // Step 2: Query for all the Units in Step 1
  List<Extended_Warranty__c> potentialproduct = [SELECT Id, Name,Product_Code__c FROM Extended_Warranty__c
                                 WHERE Order_No__c IN :extwrnty];

  
   // Step 3: Make a Map that lets you search for units by extended warranty
  Map<String, Extended_Warranty__c> ewrntyMap = new Map<String, Extended_Warranty__c>();
  for (Extended_Warranty__c w : potentialproduct) {
    ewrntyMap.put(w.Product_Code__c, w);
  }
 // Step 4: Get the matching extended warranty in the Map by extended warranty!
  for (Unit__c newUnit : Trigger.new) {
    
    if (newUnit.Ext_Warranty__c == null) {
      Extended_Warranty__c ordnm = ewrntyMap.get(newUnit.Product_Code__c);

 //ordnm is the name of the extended warranty record that is associated with the unit.      
    if (ordnm != null) {
      newUnit.Ext_Warranty__c  = ordnm.Id;
      }
     
    }
  }

}