0

I am unable to assign the lookup value of the newly created record to a lookup value field. The record is inserted properly and I am able to retrieve the id, but I cannot assign it for some reason. Please note that I am using a button. Thanks in advance.

global class Otg_Interaction_Buttons {

  global Otg_Interaction_Buttons() { }

    webservice static void createReqFromInt(Id currentRecordId){       

    Set<Id> contId = new Set<Id>();                           
    List<Request__c> reqToInsert = new List<Request__c>();

    List<Interaction__c> intLst = [select Id,name,Contact__c from Interaction__c where Id = :currentRecordId];    

    for(Interaction__c i : intLst) {
         if(i.Contact__c != null){                       
            contId.Add(i.Contact__c);
         }                        

    List<Contact> contLst = [select Id,name,FirstName,LastName from Contact where Id = :contId];

         Request__c req = new Request__c();
         req.Contact__c = contLst[0].Id;                                               
         reqToInsert.add(req);

         if(reqToInsert.size()>0){
            insert reqToInsert;                             
         }  
         System.debug('The Req inserted equal:'+ reqToInsert.Size());

      i.Request__c = req.Id; //This is not getting assigned
      }         
   }                                                                                                  
}
  • 1
    How do you knw its not getting assigned? you are not doing any dml on Interaction__c list – Pranay Jaiswal Jul 12 '19 at 16:48
  • I thought I didn't need anything other than i.Request__c = req.Id since interaction__c is the current object record. The button is on interaction and it creates a request, but I want to update the interaction with the request id. Any other guidance you could provide? My apologies I am a little lost on this one. – Snights Jul 12 '19 at 17:58
  • As I can see its a webservice call(Not trigger context), and you are querring interaction records. After you have assigned the value you have update the intLst – Pranay Jaiswal Jul 12 '19 at 18:36
  • Thanks for the guidance, I got it to work. I'll post my solution in a second. – Snights Jul 12 '19 at 19:02
0

EDITED:

global class Otg_Interaction_Buttons {

  global Otg_Interaction_Buttons() { }

    webservice static void createReqFromInt(Id currentRecordId){       

    Set<Id> contId = new Set<Id>();                           
    List<Request__c> reqToInsert = new List<Request__c>();
    Id reqId;

    List<Interaction__c> intLst = [select Id,name,Contact__c from Interaction__c where Id = :currentRecordId];    

    for(Interaction__c i : intLst) {
         if(i.Contact__c != null){                       
            contId.Add(i.Contact__c);
         } 
    }              

    List<Contact> contLst = [select Id,name,FirstName,LastName from Contact where Id = :contId];

         Request__c req = new Request__c();
         req.Contact__c = contLst[0].Id;                                               
         reqToInsert.add(req);

         if(reqToInsert.size()>0){
            insert reqToInsert;                             
         }  
         System.debug('The Req inserted equal:'+ reqToInsert.Size());

         reqId = req.Id;

    for(Interaction__c i : intLst) {
         if(i.Contact__c != null){                       
            i.Request__c = reqId;
         }
      update intLst;
    }
 }  

}

| improve this answer | |
  • Yes the code works, but its not bulkified, Thus its prone to errors. Can you remove SOQL and DML from for loop? SF being a multitenant cloud platform, there are limits on it. You should try to bulkify your code. – Pranay Jaiswal Jul 12 '19 at 19:11
  • if(reqToInsert.size()>0) is not required – cropredy Jul 12 '19 at 19:29

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.