0

I have a question regarding adding appriopriate object.

After user created a CAR__c object trigger should create a new object TYPE__c with specific data. During creation CAR__c object user have to choose one subtype for eg. ASTRA. When he save the record object TYPE__c should be created with ER Type = ASTRA

Set<String> erTypes = new Set<String>{'ASTRA', 'FOCUS', 'CHR'};

for(CAR__c car: carList) {
    if(erTypes .contains(car.esType__c) {
        Type__c type = new Type__c (
            Name = car.abc__c,
            erType__c = <How to get appriopriate value when user choose eg. FOCUS? > This is a lookup field. We have a another object called VEHICLE__c with the same values like in Set erTypes. So here should be object VEHICLE__c.Id with value which user selected during creation a car object.
        );
        insert type;
    }
}

}

0

What you need to do is query for that value. For instance:

VEHICLE__c theVehicle = [SELECT Id FROM VEHICLE__c WHERE Name=:car.esType__c];

Now that you have the instance of theVehicle that you were looking for you can reference its Id like so erType__c = theVehicle.Id;

However the bigger problem you hjave is that you are doing a DML statement inside a for loop. Here's how you can refine your code to avoid hitting any governor limits.

Set<String> esTypes = new Set<String>();
for(CAR__c car: carList) {
    esTypes.add(car.esType__c);
}

Map<String, Id> vehiclesMap = new Map<String, Id>();
for (VEHICLE__c theVehicle: [SELECT Id, Name FROM VEHICLE__c WHERE Name=:esTypes]) {
    vehiclesMap.put(theVehicle.Name, theVehicle.Id);
}

List<Type__c> theTypes = new List<Type__c>();
for(CAR__c car: carList) {
    if(erTypes.contains(car.esType__c) {
        Type__c carType = new Type__c (
            Name = car.abc__c,
            erType__c = vehiclesMap.get(car.esType__c);
        );
        theTypes.add(carType);
    }
}
insert theTypes;
0

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.