0

I have to 2 object(Case,Asset) and if in Case Object SerialNumber Equals to Asset SerialNumber then i'ill get ProductCode(field) from Asset inside to Case Object in Subject(field) (why subject because this is trial also fields have Text type)

Note: Just i want to update List GetSerial from the Case its'a Possible Or Not

trigger GetDataFromAsset on Case (before insert,before update) {

        List<String> GetSerial = new List<String>();
        List<Asset> GetAssetData = new List<Asset>();


         for(Case case_i :Trigger.new)
        {
              GetSerial.add(case_i.Arizali_Urun_Seri_Numarasi__c);
            GetSerial.add(case_i.Subject);
        }
        system.debug(GetSerial);

        GetAssetData = [Select ProductCode From Asset Where SerialNumber IN: GetSerial];

        system.debug(GetAssetData[0].ProductCode);

        GetSerial[1] = GetAssetData[0].ProductCode;

        System.debug(GetSerial);


        /*Schema.SObjectType.Case.fields.Subject.getLabel() = GetAssetData[0].ProductCode;*/
    }
  • Can you explain your question as It doesn't seem clear. So that you can get help. – RKM Jul 30 at 12:41
  • i want to list<string> to update from case object fields – Furkan Kuyular Jul 30 at 12:53
1

Please find the below code snippet. You will not be able to update case by changing the value in the list of string, instead you need to change the value in the Case instance.

trigger GetDataFromAsset on Case (before insert,before update) {

    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        Map<String, Case> serialVsCaseMap = new Map<String, Case>();
        List<Asset> assetList = new List<Asset>();

        //Construct a map of the Serial number and its associated Case record
        for(Case caseObj:Trigger.new) {
            serialVsCaseMap.put(caseObj.Arizali_Urun_Seri_Numarasi__c, caseObj);
        }

        //List of asset with same serial Number
        assetList = [Select ProductCode, SerialNumber From Asset Where SerialNumber IN: serialVsCaseMap.keySet()];

        for( Asset asset : assetList ) {
            if(serialVsCaseMap.containsKey(asset.SerialNumber)) {
                serialVsCaseMap.get(asset.SerialNumber).Subject = asset.ProductCode;
            }
        }
        System.debug('...serialVsCaseMap: ' + serialVsCaseMap);
    }
}

Please keep a practice of adding the Trigger context variable checks like isBefore, isInsert to ensure your logic is getting fired on the specific event only.

  • Just pointing out the code above wouldn't compile. – Drew Kennedy Jul 30 at 13:09
  • Hey Drew, I haven't compiled it, it's just more on pseudo-code or reference. – Madhavi Bhattad Jul 30 at 13:18
  • Sure i'ill check but also i getting just a 1 record in the field then i couldnt use loop – Furkan Kuyular Jul 30 at 13:35
  • Am not sure I get you Furkan, however its best practice to have a bulk safe code. @Drew thanks, I updated reserved keyword case. – Madhavi Bhattad Jul 30 at 13:40
  • 1
    Yes! Thanks worked :) – Furkan Kuyular Jul 30 at 13:53

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.