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
suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com 

generic SObject


Hello all

 

Scenario: I need to check the Invoice number in an Custom Object and if it is not unique it displays an error. But I am getting an error during in an execution. Could any one help on this??

 

 

Map<String, Claim__c> sclmap = new Map<String, Claim__c>();
//////////////////////////////////
//4 Unique Invoice number/////////
//////////////////////////////////
for(SObject obj: Trigger.new)
{
Claim__c cl =(Claim__c)obj;

cl.Unique__c=cl.Claim_Invoice_Number__c+cl.Claim_Invoice_Position__c
+cl.Claim_Document_Line_Item_Number__c+cl.Claim_Line_Item__c;



//////////////////////////////////////////////////
//Make sure we don't treat an Unique__c that isn't
//changing during an update as a duplicate////////
//////////////////////////////////////////////////
if( (System.Trigger.isInsert ||
(cl.Unique__c != System.Trigger.oldMap.get(cl.id).Unique__c)))

{
if (sclMap.containsKey(cl.Unique__c))
{
cl.InvoiceMessage__c = 'already Invoice is present';
}
else{
sclMap.put(cl.Unique__c, cl);
cl.InvoiceMessage__c = NULL;
}
}
}
//////////////////////////////////////////////////////////////////////////
//Using a single database query, find all Invoice nos in the claim table
//that have the same number as any of other claims being inserted or updated
////////////////////////////////////////////////////////////////////////////
for(Claim__c c : [select Unique__c from Claim__c
where Unique__c IN : sclMap.KeySet()] )
{
Claim__c newscl = sclMap.get(c.Unique__c);
//newscl.Error__c.addError('already Invoice is present');
newscl.InvoiceMessage__c = 'Invoice is already present';
}

 

 

 

I am getting this error 'Field expression not allowed for generic Sobject'

Vinit_KumarVinit_Kumar

You cannot use direct reference or assignment when working with sObjects. Instead you must use their get and put methods.

 

So instead of

 

 

String strObjectName = mySObject.Name;

 

 

 

you need to do:

 

 

String.strObjectName = String.valueOf( mySObject.get('Name') );

 

 

 

 

Same applies to assigning values to sObjects. Instead of:

 

 

mySObject.Name = 'MyName';

 

 

 

you need to do:

 

 

mySObject.put('Name', 'MyName');