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
Carter85Carter85 

Need help truncating an ID properly

I'm working with this at the moment:

List<RecordType> id = [select ID from RecordType where Name = 'Quote'];
    if (id.isEmpty()){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No ID found.'));
    return null;    
    }
    rectypeid = String.valueOf(id[0].ID);

cncl = new Cancelations__c();
    cncl.Id = rectypeid;
    cncl.Date__c = cDate;
    cncl.Contract_Holder__c = name;
    cncl.Customer_Pay__c = custPrice;
    cncl.Cancelation_Fee__c = cFee;
    cncl.Cx_Method__c = method;
    cncl.Deduction__c = deduction;

 I'm trying to get it to return the proper ID value, but it keeps returning the 18 digit version instead of the 15 digit one so when I try and pass it back to salesforce it says it's an invalid ID for the object and I can't figure out either ther best way to truncate it or pass it back so it will accept it.  I've tried typcasting it as an ID, various string methods to remove the extra characters, but without success, and it's wearing a little thin so any advice on perhaps a better way to do it, or a correction if I'm doing it completly wrong would be great.

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi Carter ,

 

There might be a possiblity that that a recordType by the name 'Quote' can be present for another object as well. in the SOQL query can you add another condition like this:

 

List<RecordType> id = [select ID from RecordType where Name = 'Quote' AND sObjectType='Cancelations__c'];

 Also, you can assign the recordType directly.

cncl.recordTypeId = id[0].id;

 -ಸಮಿರ್

All Answers

Sean TanSean Tan

I don't think the problem has anything to do with truncation of the Id.

 

The cancelation object is not a RecordType therefore you are assigning an incorrect Id to the object. Shouldn't you be assigning the RecordTypeId field not the Id field?

ForcepowerForcepower
Carter, I don't see why you need to do a String.valueOf on id[0].ID. Just try assigning it directly. SF will work with the 18 char id just as well as the 15 char id.
Ram
ForcepowerForcepower
Good point, Sean.
Carter85Carter85

I had tried that, but for some reason when I make the attempt with the RecordTypeId field I get an error when trying to insert stating that I'm attempting to de-reference a null object, but given that I know I have a value that was confusing me as well.

sfdcfoxsfdcfox
Is the error coming from the DML line? If so, your problem is most likely in a trigger somewhere, not in the code you've posted. Your code should, as originally posted, complain about the Id value being of the incorrect type (because you assigned a RecordType Id to a non-RecordType SObject). However, a null-pointer exception shouldn't happen anywhere in the code you've posted.
jungleeejungleee

Hi Carter ,

 

There might be a possiblity that that a recordType by the name 'Quote' can be present for another object as well. in the SOQL query can you add another condition like this:

 

List<RecordType> id = [select ID from RecordType where Name = 'Quote' AND sObjectType='Cancelations__c'];

 Also, you can assign the recordType directly.

cncl.recordTypeId = id[0].id;

 -ಸಮಿರ್

This was selected as the best answer
Carter85Carter85

The suggested adjustments worked, and sfdcfox was correct, the insert was caused by an error elsewhere.