-2

This question already has an answer here:

In Child Object(Incident) i have a field called Client ID(BMCServiceDesk__FKClient__c) this is a lookup field relates User .I need to populate Case owner field value in to Client ID.

While creation of incident record only i need to populate Case Owner Value , I have tried somehow with maps but Case Owner value is coming null in debug.

Case : Parent Object Incident : Child Object

please find my code snippet below and suggest me :

public class supportform {

public static List<Attachment> attachments;
public static final Integer DEFAULT_ATTACHMENT_NO=1;
public String sobjId {get; set;}
public String Appname{get; set;}
public static Map<String, BMCServiceDesk__Category__c> catMap ;

@AuraEnabled
public static id Savesupportform(Case casedata, String fileName, String base64Data, String contentType){

    //List<user> ownerid=[SELECT Id FROM User WHERE Name LIKE '%Neel%' AND IsActive = true];
     //Casedata.ownerid=ownerid[0].id;

    id RcrdTypeid= Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Agent Helpdesk').getRecordTypeId();
    System.debug('Casedata@@@@@@@@@@@@@@'+RcrdTypeid);
    Casedata.recordtypeid=RcrdTypeid;//'0120U0000000Aju';



    System.debug('Casedata@@@@@@@@@@@@@@'+Casedata.recordtypeid);
    insert Casedata; 
    System.debug('Casedata@@@@@@@@@@@@@@'+Casedata);
    List<BMCServiceDesk__Category__c> lstcat = [Select Id,Name from BMCServiceDesk__Category__c limit 50000];

    catMap = new Map<String, BMCServiceDesk__Category__c>();

    for (BMCServiceDesk__Category__c c : lstcat ){
        catMap.put(c.Name, c);

    }  
    System.debug('catMap@@@@@@@@@@@@@@'+catMap);  

    List<BMCServiceDesk__Impact__c> lstimp = [Select Id,Name from BMCServiceDesk__Impact__c limit 4];

    Map<String, BMCServiceDesk__Impact__c> impmap = new Map<String, BMCServiceDesk__Impact__c>();

    for (BMCServiceDesk__Impact__c i : lstimp){
        impmap.put(i.Name, i);

    }  
    System.debug('impmap@@@@@@@@@@@@@@'+impmap);    


    List<BMCServiceDesk__Urgency__c> lsturg = [Select Id,Name from BMCServiceDesk__Urgency__c limit 4];

    Map<String, BMCServiceDesk__Urgency__c> urgmap = new Map<String, BMCServiceDesk__Urgency__c>();

    for (BMCServiceDesk__Urgency__c u : lsturg){
        urgmap.put(u.Name, u);

    }


    List<User> lstuser = [Select Id,Name from user limit 50000];

    Map<String, User> usermap = new Map<String, User>();

    for (User u1 : lstuser){
        usermap.put(u1.Name, u1);

    }     

    System.debug('urgmap@@@@@@@@@@@@@@'+usermap);

    System.debug('@@@@@@@@@@@@@@@@'+Casedata);
    base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
    System.debug('@@@@@@@@@@@@@@@@'+base64Data );
    //base64Data = 'Test';
    Attachment oAttachment = new Attachment();
    oAttachment.parentId = casedata.id;
    oAttachment.Body = EncodingUtil.base64Decode(base64Data);        
    oAttachment.Name = fileName;
    oAttachment.ContentType = contentType;
    insert oAttachment; 

    BMCServiceDesk__Incident__c inc = new BMCServiceDesk__Incident__c();
    inc.Cases__c = casedata.id;
    inc.BMCServiceDesk__incidentDescription__c = casedata.Type_Of_Ticket__c;

    string newlowerstringcategory = (casedata.Applications__c).trim();
    System.debug('newlowerstringcategory@@@'+ newlowerstringcategory);
    system.debug('catMap.get(newlowerstringcategory)   '+catMap.get(newlowerstringcategory));
    if(catMap.get(newlowerstringcategory)!= null) {       
        inc.BMCServiceDesk__FKCategory__c  = catMap.get(newlowerstringcategory).id;
        System.debug('1111111111111111'+ catMap.get(newlowerstringcategory));
        System.debug('11111111111111111'+ inc.BMCServiceDesk__FKCategory__c);
    }

    string newlowerstringuser = casedata.Owner.Name;
      System.debug('11111111111111111'+ newlowerstringuser);
        if(usermap.get(newlowerstringuser)!= null) {       
        inc.BMCServiceDesk__FKClient__c= usermap.get(newlowerstringuser).id;
        System.debug('1111111111111111'+ usermap.get(newlowerstringuser));
        System.debug('11111111111111111'+ inc.BMCServiceDesk__FKClient__c);
    }

    inc.BMCServiceDesk__Service_Request_Title__c = casedata.Subject;
    System.debug('11111111111111111'+ inc.BMCServiceDesk__Service_Request_Title__c);

    string newlowerstringImpact = casedata.Impact__c.touppercase();
    System.debug('newlowerstringImpact '+ newlowerstringImpact );

    if(impmap.get(newlowerstringImpact) != null)        
        inc.BMCServiceDesk__FKImpact__c = impmap.get(newlowerstringImpact).id;
    System.debug('impmap_getnewlowerstringImpact_id'+ impmap.get(newlowerstringImpact).id );
    System.debug('11111111111111111'+ inc.BMCServiceDesk__FKImpact__c);


    string newupperstringUrgency = casedata.Urgency__c.touppercase();
    System.debug('newupperstringUrgency '+ newupperstringUrgency );
    if(urgmap.get(newupperstringUrgency) != null)
        inc.BMCServiceDesk__FKUrgency__c= urgmap.get(newupperstringUrgency).id;
    System.debug('11111111111111111'+ inc.BMCServiceDesk__FKUrgency__c);

    inc.BMCServiceDesk__incidentDescription__c= casedata.Description +' '+casedata.Type_Of_Ticket__c+' '+casedata.Justification__c;
    //inc.BMCServiceDesk__incidentDescription__c= casedata.Justification__c ;
    insert inc;
    System.debug('@@@@@@@@@@@@@@@@@@@@'+inc);

    Attachment oAttachment1 = new Attachment();
    oAttachment1.parentId = inc.id;
    oAttachment1.Body = EncodingUtil.base64Decode(base64Data);        
    oAttachment1.Name = fileName;
    oAttachment1.ContentType = contentType;
    insert oAttachment1; 
    System.debug('@@@@@@@@@@@@@@@@@@@@'+oAttachment1); 

    return casedata.id;
}

}

marked as duplicate by Derek F, battery.cord, crmprogdev, Martin Lezer, glls Apr 25 '18 at 15:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

You should not be using the Owner Name, much less a Map keyed on Owner Name, for this workflow. User names are not unique, and I would be surprised if your incoming Case data in this Lightning controller method had Owner.Name populated anyway.

Just assign

inc.BMCServiceDesk__FKClient__c = casedata.OwnerId;

after ensuring, of course, that the Case has a non-null OwnerId.

You should remove the extremely inefficient query on User as well.

  • Thanks for your response...I have tried with this : inc.BMCServiceDesk__FKClient__c = casedata.OwnerId; still OwnerId coming null in debug. – kumar G Apr 25 '18 at 11:38
  • 1
    Then you need to either re-query to get the Case's OwnerId or change the code that provides casedata so that field is populated. – David Reed Apr 25 '18 at 11:40

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