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 

Concatenation through SObject

Scenario: Concatenating four custom fields into one custom field, all fields belongs to the same custom Object called 'Claim__c'   




4. Claim_Line_Item__c  these four shoul be concatenate and putting into 'Invoice_Con__c'.I am getting an error in the below mentioned code


   Error Message: Illegal assignment from SObject:Claim__c to String

   This error message is giving in if{---}

    		Map<String,Claim__c> clmap = new Map<String,Claim__c>();  
    		for(Claim__c Invclm :[select ID,Claim_Invoice_Number__c,Claim_Invoice_Position__c
                        ,Claim_Document_Line_Item_Number__c,Claim_Line_Item__c from Claim__c ])
                         Invclm.Claim_Document_Line_Item_Number__c +''+ Invclm.Claim_Line_Item__c,Invclm);
            	System.debug('step 6:'+ Invclm.Claim_Invoice_Number__c+''+Invclm.Claim_Invoice_Position__c+''+
                         Invclm.Claim_Document_Line_Item_Number__c +''+ Invclm.Claim_Line_Item__c);
            for(SObject obj:
            	Claim__c cl = (Claim__c)obj;
                         cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c))
                         cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c);



Thanks in advance



Your issue lies in the following:

cl.Invoice_Con__c=clmap.get(cl.Claim_Invoice_Number__c+''+cl.Claim_Invoice_Position__c+''+cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c);

 The map returns a Claim__c and you are assigning it to a String value (Possibly Id? which is actually a String).  I think you are trying to assign the Id of the Claim__c.  If so you will add '.Id' to the end.

cl.Invoice_Con__c=clmap.get(cl.Claim_Invoice_Number__c+''+cl.Claim_Invoice_Position__c + cl.Claim_Document_Line_Item_Number__c + cl.Claim_Line_Item__c).Id;

No, I want to assign a comibation of four values into the Invoice_Con__c
                         cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c);

 Could I do like this. Is it correct way to getting the concatenation value??



Yep, pretty much, except you can remove the ().


cl.Invoice_Con__c = cl.Claim_Invoice_Number__c + '' + cl.Claim_Invoice_Position__c + cl.Claim_Document_Line_Item_Number__c +  cl.Claim_Line_Item__c;



The problem is that clmap is a Map<String,Claim__c>.  So your code within the "if"...

cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c);

 gets the value at your specified key, returning a Claim__c object.  Then you're assigning that Claim__c to cl.InvoiceCon__c, which I assume is a String field on the Claim__c object.  Hence the error.


Maybe what you want to do is this (replace the above lines):

cl.Invoice_Con__c = cl.Claim_Invoice_Number__c+''+cl.Claim_Invoice_Position__c+''+
cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c;

 That would assign your concatenated string to the Invoice_Con__c field.  If this is the case, are you sure you need a map at all?  Maybe you want a set?


If I'm wrong and that Invoice_Con__c field is a lookup to a Claim__c object, then you probably just want the Id of the Claim__c object the map is returning.  So do:

cl.Claim_Document_Line_Item_Number__c +''+ cl.Claim_Line_Item__c).Id;