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
tulasi ram 1tulasi ram 1 

upserting task is not working properly

@RemoteAction global static String createTask(String subject, String callID, String contactID, String phoneNumber) {
        try{
            if(!isTaskAccessible()) return 'Insufficient access to create Task, please contact system admin for more information';
            List<Task> lstTask = [SELECT Id FROM Task WHERE Call_Id__c =:callID AND OwnerId =:UserInfo.getUserId() LIMIT 1];
            Task objTask = lstTask.isEmpty() ? new Task(Subject = subject, Call_Id__c = callID, Nextiva_Call_Start__c = system.now(), 
                                                        Nextiva_Phone_Number1__c = phoneNumber, TaskSubtype = 'Call')
                                            : lstTask[0];
        /*    Id objId = contactID;
            String sObjName = objId.getSObjectType().getDescribe().getName();
            system.debug('=========Object name ======='+sObjName);
            if(sObjName == 'Contact' || sObjName == 'Lead'){
            objTask.WhoId = String.isEmpty(contactID) ? objTask.WhoId : contactID;
            } 
            else if(sObjName == 'Account') {
            objTask.WhatId = String.isEmpty(contactID) ? objTask.WhatId : contactID;
            List<account> acc = [select id, (select id from contacts) from account where id=:objId];
            for(account ac :acc){
               for(contact con: ac.contacts){              
               objTask.whoid = con.id;
               
               }                      
            }           
            }   else     objTask.WhatId = String.isEmpty(contactID) ? objTask.WhatId : contactID; */
            upsert objTask;
            return objTask.Id;
        }catch(Exception ex) { return '' + ex.getMessage(); }  
    }

If there is no contact id is found then it has to create a new task. If any contactId found it has to create a task with some WhatIds. Here if if condition fails then total try block is not working why i dont know. if no contactid found then it has to create an empty task. But it is not working. Please help me on this.


 
Raj VakatiRaj Vakati
Try this by passing external Id to upsert 
 
@RemoteAction global static String createTask(String subject, String callID, String contactID, String phoneNumber) {
        try{
            if(!isTaskAccessible()) return 'Insufficient access to create Task, please contact system admin for more information';
            List<Task> lstTask = [SELECT Id FROM Task WHERE Call_Id__c =:callID AND OwnerId =:UserInfo.getUserId() LIMIT 1];
			
			
            Task objTask = lstTask.isEmpty() ? new Task(Subject = subject, Call_Id__c = callID, Nextiva_Call_Start__c = system.now(), 
                                                        Nextiva_Phone_Number1__c = phoneNumber, TaskSubtype = 'Call')
                                            : lstTask[0];
        
            upsert objTask Call_Id__c;
            return objTask.Id;
        }catch(Exception ex) { return '' + ex.getMessage(); }  
    }

 
tulasi ram 1tulasi ram 1
Hi Raj,

 Id objId = contactID;
            String sObjName = objId.getSObjectType().getDescribe().getName();
            system.debug('=========Object name ======='+sObjName);
            if(sObjName == 'Contact' || sObjName == 'Lead'){
            objTask.WhoId = String.isEmpty(contactID) ? objTask.WhoId : contactID;
            } 
            else if(sObjName == 'Account') {
            objTask.WhatId = String.isEmpty(contactID) ? objTask.WhatId : contactID;
            List<account> acc = [select id, (select id from contacts) from account where id=:objId];
            for(account ac :acc){
               for(contact con: ac.contacts){              
               objTask.whoid = con.id;
               
               }                      
            }           
            }   else     objTask.WhatId = String.isEmpty(contactID) ? objTask.WhatId : contactID; 

this code has to be executed, because we dont know wether ContactId is an id of Contact or Account or Lead. I have to find the object name from contactId and assign watid and Whoid on Task. Please consider that code also